devel_wizard-2.x-dev/templates/spell/entity_type/content/revision_revert_translation_form.php.twig
templates/spell/entity_type/content/revision_revert_translation_form.php.twig
{%
include '@devel_wizard/php/devel_wizard.php.file.header.php.twig'
with {
'namespace': content.namespace,
}
%}
use {{ content.interface_fqn }};
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RevisionRevertTranslationForm extends RevisionRevertForm {
protected string $langcode;
protected LanguageManagerInterface $languageManager;
/**
* {@inheritdoc}
*
* @phpstan-return static
*/
public static function create(ContainerInterface $container) {
// @phpstan-ignore-next-line
return new static(
$container->get('entity_type.manager'),
$container->get('date.formatter'),
$container->get('datetime.time'),
$container->get('language_manager'),
);
}
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
DateFormatterInterface $dateFormatter,
TimeInterface $time,
LanguageManagerInterface $languageManager,
) {
$this->languageManager = $languageManager;
parent::__construct($entityTypeManager, $dateFormatter, $time);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return "{$this->entityTypeId}_revision_revert_translation_form";
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->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()),
]
);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
// @phpstan-ignore-next-line
return '';
}
/**
* {@inheritdoc}
*
* @phpstan-param array<string, mixed> $form
* @phpstan-param ?int $ispim_preview_image_revision
* @phpstan-param ?string $langcode
*
* @phpstan-return array<string, mixed>
*/
public function buildForm(
array $form,
FormStateInterface $form_state,
${{ content.id }}_revision = NULL,
$langcode = NULL,
): array {
$this->langcode = $langcode;
$form = parent::buildForm($form, $form_state, ${{ content.id }}_revision);
// Unless untranslatable fields are configured to affect only the default
// translation, we need to ask the user whether they should be included in
// the revert process.
$isDefaultTranslationAffectedOnly = $this->revision->isDefaultTranslationAffectedOnly();
$form['revert_untranslated_fields'] = [
'#type' => 'checkbox',
'#title' => $this->t('Revert content shared among translations'),
'#default_value' => $isDefaultTranslationAffectedOnly && $this->revision->getTranslation($this->langcode)->isDefaultTranslation(),
'#access' => !$isDefaultTranslationAffectedOnly,
];
return $form;
}
protected function prepareRevertedRevision({{ content.interface }} $revision, FormStateInterface $form_state): {{ content.interface }} {
$revert_untranslated_fields = (bool) $form_state->getValue('revert_untranslated_fields');
$translation = $revision->getTranslation($this->langcode);
/** @var \Drupal\ispim\PreviewImage\StorageInterface $storage */
/* @noinspection PhpUnhandledExceptionInspection */
$storage = $this->entityTypeManager->getStorage($this->entityTypeId);
return $storage->createRevision($translation, TRUE, $revert_untranslated_fields);
}
}
