/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $plugin = NULL, $item_type = NULL) { // Look for the first source overview local task and make the parent use the // same route arguments. // @todo: Find a nicer way to get the main source (usually entity:node), also, // this looses the trail because the parent points directly to the first // sub-task. Use a different route that doesn't require the arguments? if (!isset($plugin)) { $definitions = $this->sourceManager->getDefinitions(); if (empty($definitions)) { return array('#markup' => 'No sources enabled.'); } if (isset($definitions['content'])) { $plugin = 'content'; } else { $plugin = key($definitions); } } $source = $this->sourceManager->createInstance($plugin); if (!isset($item_type)) { $item_types = $source->getItemTypes(); if (empty($item_types)) { return array('#markup' => 'No sources enabled.'); } if (isset($item_types['node'])) { $item_type = 'node'; } else { $item_type = key($item_types); } } $definition = $this->sourceManager->getDefinition($plugin); $form['#title'] = $this->t('@type overview (@plugin)', array('@type' => $source->getItemTypeLabel($item_type), '@plugin' => $definition['label'])); $options = array(); foreach ($this->sourceManager->getDefinitions() as $type => $definition) { $plugin_type = $this->sourceManager->createInstance($type); $item_types = $plugin_type->getItemTypes(); asort($item_types); foreach ($item_types as $item_types => $item_label) { $options[(string) $definition['label']][$type . ':' . $item_types] = $item_label; } } $form['source_type'] = array('#type' => 'container', '#open' => TRUE, '#attributes' => array('class' => array('tmgmt-source-operations-wrapper')), '#weight' => -100); $form['source_type']['source'] = array('#type' => 'select', '#options' => $options, '#default_value' => $plugin . ':' . $item_type, '#title' => t('Choose source'), '#ajax' => array('callback' => array($this, 'ajaxCallback'))); $form['source_type']['choose'] = array('#type' => 'submit', '#value' => t('Choose'), '#submit' => array('::sourceSelectSubmit'), '#attributes' => array('class' => array('js-hide'))); $form['actions'] = array('#type' => 'details', '#title' => t('Operations'), '#open' => TRUE, '#attributes' => array('class' => array('tmgmt-source-operations-wrapper'))); $form['actions']['submit'] = array('#type' => 'submit', '#button_type' => 'primary', '#validate' => array('::validateItemsSelected'), '#value' => t('Request translation'), '#submit' => array('::submitForm')); tmgmt_add_cart_form($form['actions'], $form_state, $plugin, $item_type); if ($source instanceof ContinuousSourceInterface && $this->continuousManager->hasContinuousJobs()) { $form['actions']['add_to_continuous_jobs'] = array('#type' => 'submit', '#validate' => array('::validateItemsSelected'), '#value' => t('Check for continuous jobs'), '#submit' => array('::submitToContinuousJobs')); $form['actions']['add_all_to_continuous_jobs'] = array('#type' => 'checkbox', '#title' => 'All (continuous check only)', '#default_value' => FALSE); } $source_ui = $this->sourceManager->createUIInstance($plugin); $form_state->set('plugin', $plugin); $form_state->set('item_type', $item_type); $form = $source_ui->overviewForm($form, $form_state, $item_type); $form['legend'] = tmgmt_color_legend(); return $form; }
/** * Creates job item and submits according to the configured settings. * * The job item will only be created if the given source plugin for the job is * configured to accept this source. * * The job item will be immediately submitted to the translator unless * this happens on cron runs. * * @param \Drupal\tmgmt\Entity\Job $job * Continuous job. * @param string $plugin * The plugin name. * @param string $item_type * The source item type. * @param string $item_id * The source item id. * * @return \Drupal\tmgmt\Entity\JobItem * Continuous job item. */ public function addItem(Job $job, $plugin, $item_type, $item_id) { // Check if a job item should be created. $most_recent_job_item = $job->getMostRecentItem($plugin, $item_type, $item_id); $should_create_item = $this->sourcePluginManager->createInstance($plugin)->shouldCreateContinuousItem($job, $plugin, $item_type, $item_id); if ($should_create_item) { if ($most_recent_job_item) { // If the most recent job item is active do nothing. if (!$most_recent_job_item->isAborted() && !$most_recent_job_item->isAccepted()) { $most_recent_job_item->addMessage('Source was updated, changes were ignored as job item is still active.'); return NULL; } } // If there are no job items or it's finished/aborted create new one. $job_item = $job->addItem($plugin, $item_type, $item_id); $job_item->addMessage('Continuous job item created'); // Only submit the item if cron submission is disabled. if (!$this->configFactory->get('tmgmt.settings')->get('submit_job_item_on_cron')) { $translator = $job->getTranslatorPlugin(); $translator->requestJobItemsTranslation([$job_item]); } return $job_item; } return NULL; }