Commit e71f75c6 authored by Loparev's avatar Loparev
Browse files

Merge branch 'DRUP-215-VBO-download-by-job-items' into 8.x-4.x

parents ec084660 c021693d
langcode: en
status: true
dependencies:
module:
- tmgmt
id: tmgmt_smartling_download_by_job_items_job_action
label: 'Download Translation (by job items)'
type: tmgmt_job
plugin: tmgmt_smartling_download_by_job_items_job_action
configuration: { }
......@@ -78,3 +78,7 @@ tmgmt.translator.settings.smartling:
action.configuration.tmgmt_smartling_send_context_action:
type: action_configuration_default
label: 'Send context action'
action.configuration.tmgmt_smartling_download_by_job_items_job_action:
type: action_configuration_default
label: 'Download Translation (by job items)'
<?php
namespace Drupal\tmgmt_smartling\Form;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\tmgmt_extension_suit\ExtendedTranslatorPluginInterface;
use Drupal\tmgmt_extension_suit\Form\BaseTmgmtActionApproveForm;
/**
* Provides a confirmation form for sending multiple content entities.
*/
class DownloadByJobItemsTmgmtActionApproveForm extends BaseTmgmtActionApproveForm {
/**
* Temp storage name we are saving entity_ids to.
*
* @var string
*/
protected $tempStorageName = 'tmgmt_smartling_tmgmt_job_operations_download_by_job_items';
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'tmgmt_smartling_download_by_job_items_form_confirm';
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Download Translation (by job items)');
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to download translations for these jobs?');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Downloading can take some time, do not close the browser');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$current_user_id = $this->currentUser()->id();
$temp_storage_name = $this->getTempStorageName();
// Clear out the accounts from the temp store.
$this->tempStoreFactory->get($temp_storage_name)->delete($current_user_id);
if (!$form_state->getValue('confirm')) {
return;
}
$operations = [];
foreach ($this->entityIds as $id => $entity_type) {
$job = Job::load($id);
if (empty($job)) {
continue;
}
foreach ($job->getItems() as $item) {
$batch_item_data = [
'tjid' => $job->id(),
'tjiid' => $item->id()
];
$operations[] = [
[get_class($this), 'processBatch'],
[$batch_item_data],
];
}
}
if (!empty($operations)) {
$batch = [
'title' => $this->getConfirmText(),
'operations' => $operations,
'finished' => [get_class($this), 'finishBatch'],
];
batch_set($batch);
}
else {
$form_state->setRedirect('system.admin_content');
}
}
/**
* {@inheritdoc}
*/
public static function processBatch(array $data, array &$context) {
if (!isset($context['results']['errors'])) {
$context['results']['errors'] = [];
$context['results']['count'] = 0;
}
$job = Job::load($data["tjid"]);
$job_item = JobItem::load($data["tjiid"]);
if (!$job) {
$context['results']['errors'][] = t('TMGMT Job @id not found', [
'@id' => $data["tjid"]
]);
}
if (!$job_item) {
$context['results']['errors'][] = t('TMGMT Job Item @id not found', [
'@id' => $data["tjiid"]
]);
}
if ($translator = $job->getTranslator()) {
$plugin = $translator->getPlugin();
if (
$plugin instanceof ExtendedTranslatorPluginInterface &&
$plugin->downloadTranslation($job, $job_item)
) {
$context['results']['count']++;
}
else {
$context['results']['errors'][] = new FormattableMarkup('Error downloading %name', [
'%name' => $job->label(),
]);
return;
}
$context['message'] = new FormattableMarkup('Processed %name.', [
'%name' => $job->label(),
]);
}
else {
$context['message'] = new FormattableMarkup('Skipped %name.', [
'%name' => $job->label(),
]);
}
}
}
<?php
namespace Drupal\tmgmt_smartling\Plugin\Action;
use Drupal\tmgmt_extension_suit\Plugin\Action\BaseJobAction;
/**
* Translate entity.
*
* @Action(
* id = "tmgmt_smartling_download_by_job_items_job_action",
* label = @Translation("Download Translation (by job items)"),
* type = "tmgmt_job",
* confirm_form_route_name = "tmgmt_smartling.download_by_job_items_approve_action"
* )
*/
class DowloadByJobItemsJobAction extends BaseJobAction {
/**
* Returns temp storage name.
*
* @inheritdoc
*/
protected function getTempStoreName($entity_type = '') {
return 'tmgmt_smartling_' . $entity_type . '_operations_download_by_job_items';
}
}
......@@ -4,6 +4,8 @@
* @file
* Update function for the tmgmt_smartling module.
*/
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Utility\UpdateException;
......@@ -188,3 +190,35 @@ function tmgmt_smartling_update_8403() {
}
}
}
/**
* Create download by job items action plugin./
*/
function tmgmt_smartling_update_8804() {
try {
$plugin_id = 'tmgmt_smartling_download_by_job_items_job_action';
$plugin_name = 'system.action.tmgmt_smartling_download_by_job_items_job_action';
$entity_type_manager = \Drupal::entityTypeManager();
$module_handler = \Drupal::moduleHandler();
$config_install_path = $module_handler->getModule('tmgmt_smartling')
->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
$action_storage = $entity_type_manager->getStorage('action');
$action = $action_storage->load($plugin_id);
// Create action if it doesn't exist.
if (!$action) {
$storage = new FileStorage($config_install_path);
$read_file = $storage->read($plugin_name);
if ($read_file) {
$entity_type_manager
->getStorage('action')
->create($read_file)
->save();
}
}
}
catch (Exception $e) {
throw new UpdateException('Unable to create "tmgmt_smartling_download_by_job_items_job_action" action.');
}
}
......@@ -20,3 +20,11 @@ tmgmt_smartling.send_context_action:
_form: 'Drupal\tmgmt_smartling\Form\SendContextActionApproveForm'
requirements:
_permission: 'send context smartling'
tmgmt_smartling.download_by_job_items_approve_action:
path: '/admin/tmgmt/approve-action-download-by-job-items'
defaults:
_title: 'TMGMT Smartling approve download by job items action'
_form: '\Drupal\tmgmt_smartling\Form\DownloadByJobItemsTmgmtActionApproveForm'
requirements:
_permission: 'administer tmgmt'
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment