ead0d5d5fafb37f68bbcd95ed226f1e88ab42753
[yaffs-website] / web / core / modules / node / src / NodeTranslationHandler.php
1 <?php
2
3 namespace Drupal\node;
4
5 use Drupal\content_translation\ContentTranslationHandler;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Defines the translation handler for nodes.
11  */
12 class NodeTranslationHandler extends ContentTranslationHandler {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
18     parent::entityFormAlter($form, $form_state, $entity);
19
20     if (isset($form['content_translation'])) {
21       // We do not need to show these values on node forms: they inherit the
22       // basic node property values.
23       $form['content_translation']['status']['#access'] = FALSE;
24       $form['content_translation']['name']['#access'] = FALSE;
25       $form['content_translation']['created']['#access'] = FALSE;
26     }
27
28     $form_object = $form_state->getFormObject();
29     $form_langcode = $form_object->getFormLangcode($form_state);
30     $translations = $entity->getTranslationLanguages();
31     $status_translatable = NULL;
32     // Change the submit button labels if there was a status field they affect
33     // in which case their publishing / unpublishing may or may not apply
34     // to all translations.
35     if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) {
36       foreach ($entity->getFieldDefinitions() as $property_name => $definition) {
37         if ($property_name == 'status') {
38           $status_translatable = $definition->isTranslatable();
39         }
40       }
41       if (isset($status_translatable)) {
42         foreach (['publish', 'unpublish', 'submit'] as $button) {
43           if (isset($form['actions'][$button])) {
44             $form['actions'][$button]['#value'] .= ' ' . ($status_translatable ? t('(this translation)') : t('(all translations)'));
45           }
46         }
47       }
48     }
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function entityFormTitle(EntityInterface $entity) {
55     $type_name = node_get_type_label($entity);
56     return t('<em>Edit @type</em> @title', ['@type' => $type_name, '@title' => $entity->label()]);
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {
63     if ($form_state->hasValue('content_translation')) {
64       $translation = &$form_state->getValue('content_translation');
65       $translation['status'] = $entity->isPublished();
66       $account = $entity->uid->entity;
67       $translation['uid'] = $account ? $account->id() : 0;
68       $translation['created'] = format_date($entity->created->value, 'custom', 'Y-m-d H:i:s O');
69     }
70     parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
71   }
72
73 }