NodeTranslationHandler.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. foreach (['publish', 'unpublish', 'submit'] as $button) {
  37. if (isset($form['actions'][$button])) {
  38. $form['actions'][$button]['#value'] .= ' ' . ($status_translatable ? t('(this translation)') : t('(all translations)'));
  39. }
  40. }
  41. }
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function entityFormTitle(EntityInterface $entity) {
  48. $type_name = node_get_type_label($entity);
  49. return t('<em>Edit @type</em> @title', ['@type' => $type_name, '@title' => $entity->label()]);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
  55. if ($form_state->hasValue('content_translation')) {
  56. $translation = &$form_state->getValue('content_translation');
  57. $translation['status'] = $entity->isPublished();
  58. $account = $entity->uid->entity;
  59. $translation['uid'] = $account ? $account->id() : 0;
  60. $translation['created'] = format_date($entity->created->value, 'custom', 'Y-m-d H:i:s O');
  61. }
  62. parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
  63. }
  64. }