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