PageVariantDeleteForm.php in Page Manager 8.4
File
page_manager_ui/src/Form/PageVariantDeleteForm.php
View source
<?php
namespace Drupal\page_manager_ui\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PageVariantDeleteForm extends ConfirmFormBase {
protected $tempstore;
public function __construct(SharedTempStoreFactory $tempstore) {
$this->tempstore = $tempstore;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.shared'));
}
protected function getTempstoreId() {
return 'page_manager.page';
}
public function getFormId() {
return 'page_manager_variant_delete_form';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete this variant?');
}
public function getCancelUrl() {
$machine_name = $this
->getRouteMatch()
->getParameter('machine_name');
return new Url('entity.page.edit_form', [
'machine_name' => $machine_name,
'step' => 'general',
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$machine_name = $this
->getRouteMatch()
->getParameter('machine_name');
$variant_machine_name = $this
->getRouteMatch()
->getParameter('variant_machine_name');
$cached_values = $this->tempstore
->get($this
->getTempstoreId())
->get($machine_name);
$page = $cached_values['page'];
$page_variant = $page
->getVariant($variant_machine_name);
$cached_values['deleted_variants'][$variant_machine_name] = $page_variant;
$this
->messenger()
->addMessage($this
->t('The variant %label has been removed.', [
'%label' => $page_variant
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
$this->tempstore
->get($this
->getTempstoreId())
->set($page
->id(), $cached_values);
}
}