"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/layout_builder/src/Form/DiscardLayoutChangesForm.php" (26 Nov 2020, 2824 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "DiscardLayoutChangesForm.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\layout_builder\Form;
    4 
    5 use Drupal\Core\Form\ConfirmFormBase;
    6 use Drupal\Core\Form\FormStateInterface;
    7 use Drupal\Core\Messenger\MessengerInterface;
    8 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
    9 use Drupal\layout_builder\SectionStorageInterface;
   10 use Symfony\Component\DependencyInjection\ContainerInterface;
   11 
   12 /**
   13  * Discards any pending changes to the layout.
   14  *
   15  * @internal
   16  *   Form classes are internal.
   17  */
   18 class DiscardLayoutChangesForm extends ConfirmFormBase {
   19 
   20   /**
   21    * The layout tempstore repository.
   22    *
   23    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
   24    */
   25   protected $layoutTempstoreRepository;
   26 
   27   /**
   28    * The messenger service.
   29    *
   30    * @var \Drupal\Core\Messenger\MessengerInterface
   31    */
   32   protected $messenger;
   33 
   34   /**
   35    * The section storage.
   36    *
   37    * @var \Drupal\layout_builder\SectionStorageInterface
   38    */
   39   protected $sectionStorage;
   40 
   41   /**
   42    * Constructs a new DiscardLayoutChangesForm.
   43    *
   44    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
   45    *   The layout tempstore repository.
   46    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   47    *   The messenger service.
   48    */
   49   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, MessengerInterface $messenger) {
   50     $this->layoutTempstoreRepository = $layout_tempstore_repository;
   51     $this->messenger = $messenger;
   52   }
   53 
   54   /**
   55    * {@inheritdoc}
   56    */
   57   public static function create(ContainerInterface $container) {
   58     return new static(
   59       $container->get('layout_builder.tempstore_repository'),
   60       $container->get('messenger')
   61     );
   62   }
   63 
   64   /**
   65    * {@inheritdoc}
   66    */
   67   public function getFormId() {
   68     return 'layout_builder_discard_changes';
   69   }
   70 
   71   /**
   72    * {@inheritdoc}
   73    */
   74   public function getQuestion() {
   75     return $this->t('Are you sure you want to discard your layout changes?');
   76   }
   77 
   78   /**
   79    * {@inheritdoc}
   80    */
   81   public function getCancelUrl() {
   82     return $this->sectionStorage->getRedirectUrl();
   83   }
   84 
   85   /**
   86    * {@inheritdoc}
   87    */
   88   public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
   89     $this->sectionStorage = $section_storage;
   90     // Mark this as an administrative page for JavaScript ("Back to site" link).
   91     $form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
   92     return parent::buildForm($form, $form_state);
   93   }
   94 
   95   /**
   96    * {@inheritdoc}
   97    */
   98   public function submitForm(array &$form, FormStateInterface $form_state) {
   99     $this->layoutTempstoreRepository->delete($this->sectionStorage);
  100 
  101     $this->messenger->addMessage($this->t('The changes to the layout have been discarded.'));
  102 
  103     $form_state->setRedirectUrl($this->getCancelUrl());
  104   }
  105 
  106 }