NodeRevisionRevertTranslationForm.php in Zircon Profile 8.0
File
core/modules/node/src/Form/NodeRevisionRevertTranslationForm.php
View source
<?php
namespace Drupal\node\Form;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeRevisionRevertTranslationForm extends NodeRevisionRevertForm {
protected $langcode;
protected $languageManager;
public function __construct(EntityStorageInterface $node_storage, DateFormatter $date_formatter, LanguageManagerInterface $language_manager) {
parent::__construct($node_storage, $date_formatter);
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager')
->getStorage('node'), $container
->get('date.formatter'), $container
->get('language_manager'));
}
public function getFormId() {
return 'node_revision_revert_translation_confirm';
}
public function getQuestion() {
return t('Are you sure you want to revert @language translation to the revision from %revision-date?', [
'@language' => $this->languageManager
->getLanguageName($this->langcode),
'%revision-date' => $this->dateFormatter
->format($this->revision
->getRevisionCreationTime()),
]);
}
public function getDescription() {
return '';
}
public function buildForm(array $form, FormStateInterface $form_state, $node_revision = NULL, $langcode = NULL) {
$this->langcode = $langcode;
$form = parent::buildForm($form, $form_state, $node_revision);
$form['revert_untranslated_fields'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Revert content shared among translations'),
'#default_value' => FALSE,
);
return $form;
}
protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
$revert_untranslated_fields = $form_state
->getValue('revert_untranslated_fields');
$latest_revision = $this->nodeStorage
->load($revision
->id());
$latest_revision_translation = $latest_revision
->getTranslation($this->langcode);
$revision_translation = $revision
->getTranslation($this->langcode);
foreach ($latest_revision_translation
->getFieldDefinitions() as $field_name => $definition) {
if ($definition
->isTranslatable() || $revert_untranslated_fields) {
$latest_revision_translation
->set($field_name, $revision_translation
->get($field_name)
->getValue());
}
}
$latest_revision_translation
->setNewRevision();
$latest_revision_translation
->isDefaultRevision(TRUE);
return $latest_revision_translation;
}
}