NodeTranslationHandler.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\node;
  3. use Drupal\content_translation\ContentTranslationHandler;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * Defines the translation handler for nodes.
  8. */
  9. class NodeTranslationHandler 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 node forms: they inherit the
  17. // basic node 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. $form_object = $form_state->getFormObject();
  23. $form_langcode = $form_object->getFormLangcode($form_state);
  24. $translations = $entity->getTranslationLanguages();
  25. $status_translatable = NULL;
  26. // Change the submit button labels if there was a status field they affect
  27. // in which case their publishing / unpublishing may or may not apply
  28. // to all translations.
  29. if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) {
  30. foreach ($entity->getFieldDefinitions() as $property_name => $definition) {
  31. if ($property_name == 'status') {
  32. $status_translatable = $definition->isTranslatable();
  33. }
  34. }
  35. if (isset($status_translatable)) {
  36. if (isset($form['actions']['submit'])) {
  37. $form['actions']['submit']['#value'] .= ' ' . ($status_translatable ? t('(this translation)') : t('(all translations)'));
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function entityFormTitle(EntityInterface $entity) {
  46. $type_name = node_get_type_label($entity);
  47. return t('<em>Edit @type</em> @title', ['@type' => $type_name, '@title' => $entity->label()]);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
  53. if ($form_state->hasValue('content_translation')) {
  54. $translation = &$form_state->getValue('content_translation');
  55. $translation['status'] = $entity->isPublished();
  56. $account = $entity->uid->entity;
  57. $translation['uid'] = $account ? $account->id() : 0;
  58. $translation['created'] = format_date($entity->created->value, 'custom', 'Y-m-d H:i:s O');
  59. }
  60. parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
  61. }
  62. }