Пример #1
0
 /**
  * Overrides Drupal\Core\Entity\EntityForm::save().
  */
 public function save(array $form, FormStateInterface $form_state)
 {
     parent::save($form, $form_state);
     // Everything below this line is only invoked if the 'Submit to provider'
     // button was clicked.
     if (isset($form['actions']['submit']) && $form_state->getTriggeringElement()['#value'] == $form['actions']['submit']['#value']) {
         // Delete conflicting items.
         if ($existing_items_ids = $form_state->get('existing_item_ids')) {
             $storage = \Drupal::entityTypeManager()->getStorage('tmgmt_job_item');
             $storage->delete($storage->loadMultiple($existing_items_ids));
             $num_of_items = count($existing_items_ids);
             drupal_set_message(\Drupal::translation()->formatPlural($num_of_items, '1 conflicting item has been dropped.', '@count conflicting items have been dropped.'), 'warning');
         }
         if (!tmgmt_job_request_translation($this->entity)) {
             // Don't redirect the user if the translation request failed but retain
             // existing destination parameters so we can redirect once the request
             // finished successfully.
             unset($_GET['destination']);
         } elseif ($redirect = tmgmt_redirect_queue_dequeue()) {
             // Proceed to the next redirect queue item, if there is one.
             $form_state->setRedirectUrl(Url::fromUri('base:' . $redirect));
         } elseif ($destination = tmgmt_redirect_queue_destination()) {
             // Proceed to the defined destination if there is one.
             $form_state->setRedirectUrl(Url::fromUri('base:' . $destination));
         } else {
             // Per default we want to redirect the user to the overview.
             $form_state->setRedirect('view.tmgmt_job_overview.page_1');
         }
     } else {
         // Per default we want to redirect the user to the overview.
         $form_state->setRedirect('view.tmgmt_job_overview.page_1');
     }
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function overviewFormSubmit(array $form, FormStateInterface $form_state, $type)
 {
     // Handle search redirect.
     if ($this->overviewSearchFormRedirect($form, $form_state, $type)) {
         return;
     }
     $jobs = array();
     $items = array();
     if ($type == ConfigSource::SIMPLE_CONFIG) {
         foreach (array_filter($form_state->getValue('items')) as $item) {
             $definition = \Drupal::service('plugin.manager.config_translation.mapper')->getDefinition($item);
             $item_id = $definition['id'];
             $items[$item_id]['label'] = $definition['title'];
             $items[$item_id]['langcode'] = \Drupal::config($definition['names'][0])->get('langcode') ?: 'en';
             $items[$item_id]['type'] = $type;
         }
     } else {
         $entity_type = \Drupal::entityManager()->getDefinition($type);
         $entity_ids = str_replace($entity_type->getConfigPrefix() . '.', '', array_filter($form_state->getValue('items')));
         $entities = entity_load_multiple($type, $entity_ids);
         foreach ($entities as $entity) {
             /* @var $entity \Drupal\Core\Entity\EntityInterface */
             $item_id = $entity->getConfigDependencyName();
             $items[$item_id]['label'] = $entity->label();
             $items[$item_id]['langcode'] = $entity->language()->getId();
             // The type cannot be field_config, should be the id of the
             // fieldable entity type.
             if ($type == 'field_config') {
                 $items[$item_id]['type'] = $entity->get('entity_type') . '_fields';
             } else {
                 $items[$item_id]['type'] = $type;
             }
         }
     }
     $source_lang_registry = array();
     // Loop through entities and create individual jobs for each source language.
     foreach ($items as $id => $extra) {
         $source_lang = $extra['langcode'];
         try {
             // For given source lang no job exists yet.
             if (!isset($source_lang_registry[$source_lang])) {
                 // Create new job.
                 $job = tmgmt_job_create($source_lang, LanguageInterface::LANGCODE_NOT_SPECIFIED, \Drupal::currentUser()->id());
                 // Add initial job item.
                 $job->addItem('config', $extra['type'], $id);
                 // Add job identifier into registry
                 $source_lang_registry[$source_lang] = $job->id();
                 // Add newly created job into jobs queue.
                 $jobs[$job->id()] = $job;
             } else {
                 $jobs[$source_lang_registry[$source_lang]]->addItem('config', $extra['type'], $id);
             }
         } catch (TMGMTException $e) {
             watchdog_exception('tmgmt', $e);
             drupal_set_message($this->t('Unable to add job item for entity %name: %error.', array('%name' => $extra['label'], '%error' => $e->getMessage())), 'error');
         }
     }
     // If necessary, do a redirect.
     $redirects = tmgmt_job_checkout_multiple($jobs);
     if ($redirects) {
         tmgmt_redirect_queue_set($redirects, Url::fromRoute('<current>')->getInternalPath());
         $form_state->setRedirectUrl(Url::fromUri('base:' . tmgmt_redirect_queue_dequeue()));
         drupal_set_message(\Drupal::translation()->formatPlural(count($redirects), $this->t('One job needs to be checked out.'), $this->t('@count jobs need to be checked out.')));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function overviewFormSubmit(array $form, FormStateInterface $form_state, $type)
 {
     // Handle search redirect.
     if ($this->overviewSearchFormRedirect($form, $form_state, $type)) {
         return;
     }
     $jobs = array();
     $entities = entity_load_multiple($type, $form_state->getValue('items'));
     $source_lang_registry = array();
     // Loop through entities and create individual jobs for each source language.
     foreach ($entities as $entity) {
         /* @var $entity \Drupal\Core\Entity\EntityInterface */
         $source_lang = $entity->language()->getId();
         try {
             // For given source lang no job exists yet.
             if (!isset($source_lang_registry[$source_lang])) {
                 // Create new job.
                 $job = tmgmt_job_create($source_lang, LanguageInterface::LANGCODE_NOT_SPECIFIED, \Drupal::currentUser()->id());
                 // Add initial job item.
                 $job->addItem('content', $type, $entity->id());
                 // Add job identifier into registry
                 $source_lang_registry[$source_lang] = $job->id();
                 // Add newly created job into jobs queue.
                 $jobs[$job->id()] = $job;
             } else {
                 $jobs[$source_lang_registry[$source_lang]]->addItem('content', $type, $entity->id());
             }
         } catch (TMGMTException $e) {
             watchdog_exception('tmgmt', $e);
             drupal_set_message($this->t('Unable to add job item for entity %name: %error.', array('%name' => $entity->label(), '%error' => $e->getMessage())), 'error');
         }
     }
     // If necessary, do a redirect.
     $redirects = tmgmt_job_checkout_multiple($jobs);
     if ($redirects) {
         tmgmt_redirect_queue_set($redirects, Url::fromRoute('<current>')->getInternalPath());
         $form_state->setRedirectUrl(Url::fromUri('base:' . tmgmt_redirect_queue_dequeue()));
         drupal_set_message(\Drupal::translation()->formatPlural(count($redirects), $this->t('One job needs to be checked out.'), $this->t('@count jobs need to be checked out.')));
     }
 }