You are here

class ConfigureSectionController in Layout Builder UX 8

Attempts to add a new section, falls back to a form if necessary.

Hierarchy

Expanded class hierarchy of ConfigureSectionController

1 file declares its use of ConfigureSectionController
LBUXRouteAlter.php in src/Routing/LBUXRouteAlter.php

File

src/Controller/ConfigureSectionController.php, line 22

Namespace

Drupal\lb_ux\Controller
View source
class ConfigureSectionController implements ContainerInjectionInterface {
  use AjaxHelperTrait;
  use LayoutRebuildTrait;
  use StringTranslationTrait;

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

  /**
   * The layout plugin manager.
   *
   * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
   */
  protected $layoutPluginManager;

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * ConfigureSectionController constructor.
   *
   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
   *   The layout tempstore repository.
   * @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_plugin_manager
   *   The layout plugin manager.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, LayoutPluginManagerInterface $layout_plugin_manager, FormBuilderInterface $form_builder, MessengerInterface $messenger) {
    $this->layoutTempstoreRepository = $layout_tempstore_repository;
    $this->formBuilder = $form_builder;
    $this->messenger = $messenger;
    $this->layoutPluginManager = $layout_plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('layout_builder.tempstore_repository'), $container
      ->get('plugin.manager.core.layout'), $container
      ->get('form_builder'), $container
      ->get('messenger'));
  }

  /**
   * Adds the new section.
   *
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
   * @param int $delta
   *   The delta of the section to splice.
   * @param string $plugin_id
   *   The plugin ID of the layout to add.
   *
   * @return \Symfony\Component\HttpFoundation\Response|array
   *   The controller response.
   */
  public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {

    // Store any existing messages.
    $old_messages_by_type = $this->messenger
      ->all();

    // Attempt to submit the form with only default values.
    $form_state = new FormState();
    $form_state
      ->addBuildInfo('args', func_get_args());
    $this->formBuilder
      ->submitForm(ConfigureSectionForm::class, $form_state);

    // Clear all new messages and restore the original ones.
    $this->messenger
      ->deleteAll();
    foreach ($old_messages_by_type as $type => $old_messages) {
      foreach ($old_messages as $old_message) {
        $this->messenger
          ->addMessage($old_message, $type);
      }
    }

    // If there are errors, the form must be filled out manually.
    if (FormState::hasAnyErrors()) {

      // Clear any existing errors.
      $form_state
        ->clearErrors();
      $form_state = new FormState();
      $form_state
        ->addBuildInfo('args', func_get_args());
      return $this->formBuilder
        ->buildForm(ConfigureSectionForm::class, $form_state);
    }
    if ($this
      ->isAjax()) {
      return $this
        ->rebuildAndClose($section_storage);
    }
    else {
      $url = $section_storage
        ->getLayoutBuilderUrl();
      return new RedirectResponse($url
        ->setAbsolute()
        ->toString());
    }
  }

  /**
   * Returns the title for the configure section route.
   *
   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
   *   The section storage.
   * @param int $delta
   *   The delta of the section to splice.
   * @param string $plugin_id
   *   The plugin ID of the layout to add.
   *
   * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
   *   The route title.
   */
  public function title(SectionStorageInterface $section_storage, $delta, $plugin_id) {
    if (is_null($plugin_id)) {
      $layout_definition = $section_storage
        ->getSection($delta)
        ->getLayout()
        ->getPluginDefinition();
    }
    else {
      $layout_definition = $this->layoutPluginManager
        ->getDefinition($plugin_id);
    }
    return $this
      ->t('Configure @label section', [
      '@label' => $layout_definition
        ->getLabel(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.
ConfigureSectionController::$formBuilder protected property The form builder.
ConfigureSectionController::$layoutPluginManager protected property The layout plugin manager.
ConfigureSectionController::$layoutTempstoreRepository protected property The layout tempstore repository.
ConfigureSectionController::$messenger protected property The messenger.
ConfigureSectionController::build public function Adds the new section.
ConfigureSectionController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ConfigureSectionController::title public function Returns the title for the configure section route.
ConfigureSectionController::__construct public function ConfigureSectionController constructor.
LayoutRebuildTrait::rebuildAndClose protected function Rebuilds the layout.
LayoutRebuildTrait::rebuildLayout protected function Rebuilds the layout.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.