5 use Drupal\content_translation\ContentTranslationHandler;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * Defines the translation handler for nodes.
12 class NodeTranslationHandler extends ContentTranslationHandler {
17 public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
18 parent::entityFormAlter($form, $form_state, $entity);
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;
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();
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)'));
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()]);
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');
70 parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);