RevertOverridesForm.php in Drupal 10
File
core/modules/layout_builder/src/Form/RevertOverridesForm.php
View source
<?php
namespace Drupal\layout_builder\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\OverridesSectionStorageInterface;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RevertOverridesForm extends ConfirmFormBase {
protected $layoutTempstoreRepository;
protected $messenger;
protected $sectionStorage;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'), $container
->get('messenger'));
}
public function getFormId() {
return 'layout_builder_revert_overrides';
}
public function getQuestion() {
return $this
->t('Are you sure you want to revert this to defaults?');
}
public function getConfirmText() {
return $this
->t('Revert');
}
public function getCancelUrl() {
return $this->sectionStorage
->getLayoutBuilderUrl();
}
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
if (!$section_storage instanceof OverridesSectionStorageInterface) {
throw new \InvalidArgumentException(sprintf('The section storage with type "%s" and ID "%s" does not provide overrides', $section_storage
->getStorageType(), $section_storage
->getStorageId()));
}
$this->sectionStorage = $section_storage;
$form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->sectionStorage
->removeAllSections()
->save();
$this->layoutTempstoreRepository
->delete($this->sectionStorage);
$this->messenger
->addMessage($this
->t('The layout has been reverted back to defaults.'));
$form_state
->setRedirectUrl($this->sectionStorage
->getRedirectUrl());
}
}