class TermTranslationHandler

Same name and namespace in other branches
  1. 11.x core/modules/taxonomy/src/TermTranslationHandler.php \Drupal\taxonomy\TermTranslationHandler
  2. 10 core/modules/taxonomy/src/TermTranslationHandler.php \Drupal\taxonomy\TermTranslationHandler
  3. 8.9.x core/modules/taxonomy/src/TermTranslationHandler.php \Drupal\taxonomy\TermTranslationHandler

Defines the translation handler for terms.

Hierarchy

Expanded class hierarchy of TermTranslationHandler

File

core/modules/taxonomy/src/TermTranslationHandler.php, line 12

Namespace

Drupal\taxonomy
View source
class TermTranslationHandler extends ContentTranslationHandler {
  
  /**
   * {@inheritdoc}
   */
  public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
    parent::entityFormAlter($form, $form_state, $entity);
    $form['content_translation']['status']['#access'] = !isset($form['content_translation']);
    $form['actions']['submit']['#submit'][] = [
      $this,
      'entityFormSave',
    ];
  }
  
  /**
   * Form submission handler for TermTranslationHandler::entityFormAlter().
   *
   * This handles the save action.
   *
   * @see \Drupal\Core\Entity\EntityForm::build()
   */
  public function entityFormSave(array $form, FormStateInterface $form_state) {
    if ($this->getSourceLangcode($form_state)) {
      $entity = $form_state->getFormObject()
        ->getEntity();
      // We need a redirect here, otherwise we would get an access denied page,
      // since the current URL would be preserved and we would try to add a
      // translation for a language that already has a translation.
      $form_state->setRedirectUrl($entity->toUrl('edit-form'));
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
    if ($form_state->hasValue('content_translation')) {
      $translation =& $form_state->getValue('content_translation');
      $translation['status'] = $entity->isPublished();
    }
    parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.