TermTranslationHandler.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Drupal\taxonomy;
  3. use Drupal\Core\Entity\EntityInterface;
  4. use Drupal\content_translation\ContentTranslationHandler;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * Defines the translation handler for terms.
  8. */
  9. class TermTranslationHandler extends ContentTranslationHandler {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
  14. parent::entityFormAlter($form, $form_state, $entity);
  15. $form['actions']['submit']['#submit'][] = [$this, 'entityFormSave'];
  16. }
  17. /**
  18. * Form submission handler for TermTranslationHandler::entityFormAlter().
  19. *
  20. * This handles the save action.
  21. *
  22. * @see \Drupal\Core\Entity\EntityForm::build()
  23. */
  24. public function entityFormSave(array $form, FormStateInterface $form_state) {
  25. if ($this->getSourceLangcode($form_state)) {
  26. $entity = $form_state->getFormObject()->getEntity();
  27. // We need a redirect here, otherwise we would get an access denied page,
  28. // since the current URL would be preserved and we would try to add a
  29. // translation for a language that already has a translation.
  30. $form_state->setRedirectUrl($entity->urlInfo('edit-form'));
  31. }
  32. }
  33. }