Newer
Older

Angie Byron
committed
<?php
namespace Drupal\node;
use Drupal\content_translation\ContentTranslationHandler;

catch
committed
use Drupal\Core\Entity\EntityInterface;

Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;

Angie Byron
committed
/**
* Defines the translation handler for nodes.

Angie Byron
committed
*/
class NodeTranslationHandler extends ContentTranslationHandler {

Angie Byron
committed
/**
* {@inheritdoc}

Angie Byron
committed
*/

Dries Buytaert
committed
public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {

Angie Byron
committed
parent::entityFormAlter($form, $form_state, $entity);

Alex Pott
committed
if (isset($form['content_translation'])) {
// We do not need to show these values on node forms: they inherit the
// basic node property values.

Alex Pott
committed
$form['content_translation']['status']['#access'] = FALSE;
$form['content_translation']['name']['#access'] = FALSE;
$form['content_translation']['created']['#access'] = FALSE;

Angie Byron
committed
}
$form_object = $form_state->getFormObject();
$form_langcode = $form_object->getFormLangcode($form_state);
$translations = $entity->getTranslationLanguages();
$status_translatable = NULL;
// Change the submit button labels if there was a status field they affect
// in which case their publishing / unpublishing may or may not apply
// to all translations.
if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) {
foreach ($entity->getFieldDefinitions() as $property_name => $definition) {
if ($property_name == 'status') {
$status_translatable = $definition->isTranslatable();
}
}
if (isset($status_translatable)) {

catch
committed
if (isset($form['actions']['submit'])) {
$form['actions']['submit']['#value'] .= ' ' . ($status_translatable ? t('(this translation)') : t('(all translations)'));
}
}
}

Angie Byron
committed
}
/**
* {@inheritdoc}

Angie Byron
committed
*/
protected function entityFormTitle(EntityInterface $entity) {
$type_name = node_get_type_label($entity);
return t('<em>Edit @type</em> @title', ['@type' => $type_name, '@title' => $entity->label()]);

Angie Byron
committed
}
/**
* {@inheritdoc}

Dries Buytaert
committed
public function entityFormEntityBuild($entity_type, EntityInterface $entity, array $form, FormStateInterface $form_state) {

Alex Pott
committed
if ($form_state->hasValue('content_translation')) {
$translation = &$form_state->getValue('content_translation');

catch
committed
$translation['status'] = $entity->isPublished();
$account = $entity->uid->entity;

Angie Byron
committed
$translation['uid'] = $account ? $account->id() : 0;
$translation['created'] = $this->dateFormatter->format($entity->created->value, 'custom', 'Y-m-d H:i:s O');
}
parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
}