You are here

LayoutBuilderForm.php in Layout builder library 8

File

src/Form/LayoutBuilderForm.php
View source
<?php

namespace Drupal\layout_library\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form containing the Layout Builder UI for Layout Library.
 *
 * @todo Refactor this after https://www.drupal.org/node/3035189 is resolved.
 */
class LayoutBuilderForm extends EntityForm {

  /**
   * Layout tempstore repository.
   *
   * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
   */
  protected $layoutTempstoreRepository;

  /**
   * The section storage.
   *
   * @var \Drupal\layout_builder\SectionStorageInterface
   */
  protected $sectionStorage;

  /**
   * Constructs a new DefaultsEntityForm.
   *
   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
   *   The layout tempstore repository.
   */
  public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
    $this->layoutTempstoreRepository = $layout_tempstore_repository;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('layout_builder.tempstore_repository'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    $form['layout_builder'] = [
      '#type' => 'layout_builder',
      '#section_storage' => $section_storage,
    ];
    $this->sectionStorage = $section_storage;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    $actions['submit']['#value'] = $this
      ->t('Save layout');
    $actions['delete']['#access'] = FALSE;
    $actions['#weight'] = -1000;
    $actions['discard_changes'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Discard changes'),
      '#attributes' => [
        'class' => [
          'button',
        ],
      ],
      '#url' => $this->sectionStorage
        ->getLayoutBuilderUrl('discard_changes'),
    ];
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $return = $this->sectionStorage
      ->save();
    $this->layoutTempstoreRepository
      ->delete($this->sectionStorage);
    $this
      ->messenger()
      ->addMessage($this
      ->t('The layout has been saved.'));
    $form_state
      ->setRedirectUrl($this->sectionStorage
      ->getRedirectUrl());
    return $return;
  }

}

Classes

Namesort descending Description
LayoutBuilderForm Provides a form containing the Layout Builder UI for Layout Library.