CommentTranslationHandler.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Drupal\comment;
  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 comments.
  8. */
  9. class CommentTranslationHandler 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. if (isset($form['content_translation'])) {
  16. // We do not need to show these values on comment forms: they inherit the
  17. // basic comment property values.
  18. $form['content_translation']['status']['#access'] = FALSE;
  19. $form['content_translation']['name']['#access'] = FALSE;
  20. $form['content_translation']['created']['#access'] = FALSE;
  21. }
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function entityFormTitle(EntityInterface $entity) {
  27. return t('Edit comment @subject', ['@subject' => $entity->label()]);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
  33. if ($form_state->hasValue('content_translation')) {
  34. $translation = &$form_state->getValue('content_translation');
  35. /** @var \Drupal\comment\CommentInterface $entity */
  36. $translation['status'] = $entity->isPublished();
  37. $translation['name'] = $entity->getAuthorName();
  38. }
  39. parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
  40. }
  41. }