Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / templates / module / src / Entity / Form / entity-content-revision-revert-translation.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{module}}\Form\{{ entity_class }}RevisionRevertTranslationForm.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{module}}\Form;
9 {% endblock %}
10
11 {% block use_class %}
12 use Drupal\Core\Datetime\DateFormatterInterface;
13 use Drupal\Core\Entity\EntityStorageInterface;
14 use Drupal\Core\Form\FormStateInterface;
15 use Drupal\Core\Language\LanguageManagerInterface;
16 use Drupal\{{module}}\Entity\{{ entity_class }}Interface;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18 {% endblock %}
19
20 {% block class_declaration %}
21 /**
22  * Provides a form for reverting a {{ label }} revision for a single translation.
23  *
24  * @ingroup {{module}}
25  */
26 class {{ entity_class }}RevisionRevertTranslationForm extends {{ entity_class }}RevisionRevertForm {% endblock %}
27 {% block class_methods %}
28
29   /**
30    * The language to be reverted.
31    *
32    * @var string
33    */
34   protected $langcode;
35
36   /**
37    * The language manager.
38    *
39    * @var \Drupal\Core\Language\LanguageManagerInterface
40    */
41   protected $languageManager;
42
43   /**
44    * Constructs a new {{ entity_class }}RevisionRevertTranslationForm.
45    *
46    * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
47    *   The {{ label }} storage.
48    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
49    *   The date formatter service.
50    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
51    *   The language manager.
52    */
53   public function __construct(EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager) {
54     parent::__construct($entity_storage, $date_formatter);
55     $this->languageManager = $language_manager;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container) {
62     return new static(
63       $container->get('entity.manager')->getStorage('{{ entity_name }}'),
64       $container->get('date.formatter'),
65       $container->get('language_manager')
66     );
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getFormId() {
73     return '{{ entity_name }}_revision_revert_translation_confirm';
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getQuestion() {
80     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())]);
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function buildForm(array $form, FormStateInterface $form_state, ${{ entity_name }}_revision = NULL, $langcode = NULL) {
87     $this->langcode = $langcode;
88     $form = parent::buildForm($form, $form_state, ${{ entity_name }}_revision);
89
90     $form['revert_untranslated_fields'] = [
91       '#type' => 'checkbox',
92       '#title' => $this->t('Revert content shared among translations'),
93       '#default_value' => FALSE,
94     ];
95
96     return $form;
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   protected function prepareRevertedRevision({{ entity_class }}Interface $revision, FormStateInterface $form_state) {
103     $revert_untranslated_fields = $form_state->getValue('revert_untranslated_fields');
104
105     /** @var \Drupal\{{module}}\Entity\{{ entity_class }}Interface $default_revision */
106     $latest_revision = $this->{{ entity_class }}Storage->load($revision->id());
107     $latest_revision_translation = $latest_revision->getTranslation($this->langcode);
108
109     $revision_translation = $revision->getTranslation($this->langcode);
110
111     foreach ($latest_revision_translation->getFieldDefinitions() as $field_name => $definition) {
112       if ($definition->isTranslatable() || $revert_untranslated_fields) {
113         $latest_revision_translation->set($field_name, $revision_translation->get($field_name)->getValue());
114       }
115     }
116
117     $latest_revision_translation->setNewRevision();
118     $latest_revision_translation->isDefaultRevision(TRUE);
119     $revision->setRevisionCreationTime(REQUEST_TIME);
120
121     return $latest_revision_translation;
122   }
123 {% endblock %}