TermTranslationHandler.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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['content_translation']['status']['#access'] = !isset($form['content_translation']);
  16. $form['actions']['submit']['#submit'][] = [$this, 'entityFormSave'];
  17. }
  18. /**
  19. * Form submission handler for TermTranslationHandler::entityFormAlter().
  20. *
  21. * This handles the save action.
  22. *
  23. * @see \Drupal\Core\Entity\EntityForm::build()
  24. */
  25. public function entityFormSave(array $form, FormStateInterface $form_state) {
  26. if ($this->getSourceLangcode($form_state)) {
  27. $entity = $form_state->getFormObject()->getEntity();
  28. // We need a redirect here, otherwise we would get an access denied page,
  29. // since the current URL would be preserved and we would try to add a
  30. // translation for a language that already has a translation.
  31. $form_state->setRedirectUrl($entity->toUrl('edit-form'));
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
  38. if ($form_state->hasValue('content_translation')) {
  39. $translation = &$form_state->getValue('content_translation');
  40. $translation['status'] = $entity->isPublished();
  41. }
  42. parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
  43. }
  44. }