You are here

class ContentTranslateForm in Translation Management Tool 8

Hierarchy

Expanded class hierarchy of ContentTranslateForm

File

sources/content/src/Form/ContentTranslateForm.php, line 13

Namespace

Drupal\tmgmt_content\Form
View source
class ContentTranslateForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'tmgmt_content_translate_form';
  }

  /**
   * {@inheritdoc}
   */
  function buildForm(array $form, FormStateInterface $form_state, array $build = NULL) {

    // Store the entity in the form state so we can easily create the job in the
    // submit handler.
    $form_state
      ->set('entity', $build['#entity']);
    $overview = $build['content_translation_overview'];
    $form['#title'] = $this
      ->t('Translations of @title', array(
      '@title' => $build['#entity']
        ->label(),
    ));
    $form['actions'] = array(
      '#type' => 'details',
      '#title' => t('Operations'),
      '#open' => TRUE,
      '#attributes' => array(
        'class' => array(
          'tmgmt-source-operations-wrapper',
        ),
      ),
    );
    $form['actions']['request'] = array(
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this
        ->t('Request translation'),
      '#submit' => array(
        '::submitForm',
      ),
    );
    tmgmt_add_cart_form($form['actions'], $form_state, 'content', $form_state
      ->get('entity')
      ->getEntityTypeId(), $form_state
      ->get('entity')
      ->id());

    // Inject our additional column into the header.
    array_splice($overview['#header'], -1, 0, array(
      t('Pending Translations'),
    ));

    // Make this a tableselect form.
    $form['languages'] = array(
      '#type' => 'tableselect',
      '#header' => $overview['#header'],
      '#options' => array(),
    );
    $languages = \Drupal::languageManager()
      ->getLanguages();

    // Check if there is a job / job item that references this translation.
    $entity_langcode = $form_state
      ->get('entity')
      ->language()
      ->getId();
    $items = tmgmt_job_item_load_latest('content', $form_state
      ->get('entity')
      ->getEntityTypeId(), $form_state
      ->get('entity')
      ->id(), $entity_langcode);
    foreach ($languages as $langcode => $language) {
      if ($langcode == LanguageInterface::LANGCODE_DEFAULT) {

        // Never show language neutral on the overview.
        continue;
      }

      // Since the keys are numeric and in the same order we can shift one element
      // after the other from the original non-form rows.
      $option = array_shift($overview['#rows']);
      if ($langcode == $entity_langcode) {
        $additional = array(
          'data' => array(
            '#markup' => '<strong>' . t('Source') . '</strong>',
          ),
        );

        // This is the source object so we disable the checkbox for this row.
        $form['languages'][$langcode] = array(
          '#type' => 'checkbox',
          '#disabled' => TRUE,
        );
      }
      elseif (isset($items[$langcode])) {
        $item = $items[$langcode];
        $states = JobItem::getStates();
        $path = \Drupal::routeMatch()
          ->getRouteName() ? Url::fromRouteMatch(\Drupal::routeMatch())
          ->getInternalPath() : '';
        $destination = array(
          'destination' => $path,
        );
        $additional = Link::fromTextAndUrl($item
          ->getStateIcon() ?: $states[$item
          ->getState()], $item
          ->toUrl()
          ->setOption('query', $destination))
          ->toString();

        // Disable the checkbox for this row since there is already a translation
        // in progress that has not yet been finished. This way we make sure that
        // we don't stack multiple active translations for the same item on top
        // of each other.
        $form['languages'][$langcode] = array(
          '#type' => 'checkbox',
          '#disabled' => TRUE,
        );
      }
      else {

        // There is no translation job / job item for this target language.
        $additional = t('None');
      }

      // Inject the additional column into the array.
      // The generated form structure has changed, support both an additional
      // 'data' key (that is not supported by tableselect) and the old version
      // without.
      if (isset($option['data'])) {
        array_splice($option['data'], -1, 0, array(
          $additional,
        ));

        // Append the current option array to the form.
        $form['languages']['#options'][$langcode] = $option['data'];
      }
      else {
        array_splice($option, -1, 0, array(
          $additional,
        ));

        // Append the current option array to the form.
        $form['languages']['#options'][$langcode] = $option;
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  function validateForm(array &$form, FormStateInterface $form_state) {
    $selected = array_filter($form_state
      ->getValue('languages'));
    if (empty($selected)) {
      $form_state
        ->setErrorByName('languages', $this
        ->t('You have to select at least one language for requesting a translation.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  function submitForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_state
      ->get('entity');
    $values = $form_state
      ->getValues();
    $jobs = array();
    foreach (array_keys(array_filter($values['languages'])) as $langcode) {

      // Create the job object.
      $job = tmgmt_job_create($entity
        ->language()
        ->getId(), $langcode, \Drupal::currentUser()
        ->id());
      try {

        // Add the job item.
        $job
          ->addItem('content', $entity
          ->getEntityTypeId(), $entity
          ->id());

        // Append this job to the array of created jobs so we can redirect the user
        // to a multistep checkout form if necessary.
        $jobs[$job
          ->id()] = $job;
      } catch (TMGMTException $e) {
        watchdog_exception('tmgmt', $e);
        $languages = \Drupal::languageManager()
          ->getLanguages();
        $target_lang_name = $languages[$langcode]->language;
        $this
          ->messenger()
          ->addError(t('Unable to add job item for target language %name. Make sure the source content is not empty.', array(
          '%name' => $target_lang_name,
        )));
      }
    }
    \Drupal::service('tmgmt.job_checkout_manager')
      ->checkoutAndRedirect($form_state, $jobs);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTranslateForm::buildForm function Form constructor. Overrides FormInterface::buildForm
ContentTranslateForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ContentTranslateForm::submitForm function Form submission handler. Overrides FormInterface::submitForm
ContentTranslateForm::validateForm function Form validation handler. Overrides FormBase::validateForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.