You are here

class LayoutPluginSelector in Panels 8.3

Same name and namespace in other branches
  1. 8.4 src/Form/LayoutPluginSelector.php \Drupal\panels\Form\LayoutPluginSelector

Provides a form for selecting a layout plugin.

Hierarchy

Expanded class hierarchy of LayoutPluginSelector

2 files declare their use of LayoutPluginSelector
PanelsDisplayVariant.php in src/Plugin/DisplayVariant/PanelsDisplayVariant.php
StandardDisplayBuilder.php in src/Plugin/DisplayBuilder/StandardDisplayBuilder.php

File

src/Form/LayoutPluginSelector.php, line 15

Namespace

Drupal\panels\Form
View source
class LayoutPluginSelector extends FormBase {

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

  /**
   * The tempstore factory.
   *
   * @var \Drupal\user\SharedTempStoreFactory
   */
  protected $tempstore;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.layout_plugin'), $container
      ->get('user.shared_tempstore'));
  }

  /**
   * LayoutPluginSelector constructor.
   *
   * @param \Drupal\layout_plugin\Plugin\Layout\LayoutPluginManagerInterface $manager
   *   The layout plugin manager.
   * @param \Drupal\user\SharedTempStoreFactory $tempstore
   *   The tempstore factory.
   */
  public function __construct(LayoutPluginManagerInterface $manager, SharedTempStoreFactory $tempstore) {
    $this->manager = $manager;
    $this->tempstore = $tempstore;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'panels_layout_selection_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $cached_values = $form_state
      ->getTemporaryValue('wizard');

    /* @var $variant_plugin \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant */
    $variant_plugin = $cached_values['plugin'];
    $form['layout'] = [
      '#title' => $this
        ->t('Layout'),
      '#type' => 'select',
      '#options' => Layout::layoutPluginManager()
        ->getLayoutOptions([
        'group_by_category' => TRUE,
      ]),
      '#default_value' => $variant_plugin
        ->getConfiguration()['layout'] ?: NULL,
    ];
    $wizard = $form_state
      ->getFormObject();
    $form['update_layout'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Change Layout'),
      '#access' => !empty($variant_plugin
        ->getConfiguration()['layout']),
      '#validate' => [
        [
          $this,
          'validateForm',
        ],
      ],
      '#submit' => [
        [
          $this,
          'submitForm',
        ],
        [
          $wizard,
          'submitForm',
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $cached_values = $form_state
      ->getTemporaryValue('wizard');

    /* @var $variant_plugin \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant */
    $variant_plugin = $cached_values['plugin'];

    // If we're changing the layout, the variant plugin must remain out of date
    // until the layout is fully configured and regions are remapped.
    if ($form_state
      ->getValue('op') == $form['update_layout']['#value']) {
      $cached_values['layout_change'] = [
        'old_layout' => $variant_plugin
          ->getConfiguration()['layout'],
        'new_layout' => $form_state
          ->getValue('layout'),
      ];

      /** @var \Drupal\ctools\Wizard\EntityFormWizardInterface $wizard */
      $wizard = $form_state
        ->getFormObject();
      $next_op = $wizard
        ->getNextOp();
      $form_state
        ->setValue('op', $next_op);
    }
    $variant_plugin
      ->setLayout($form_state
      ->getValue('layout'), $form_state
      ->getValue('layout_settings') ?: []);
    $cached_values['plugin'] = $variant_plugin;
    $form_state
      ->setTemporaryValue('wizard', $cached_values);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $cached_values = $form_state
      ->getTemporaryValue('wizard');

    /* @var $variant_plugin \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant */
    $variant_plugin = $cached_values['plugin'];
    if ((string) $form_state
      ->getValue('op') == $this
      ->t('Change Layout') && $variant_plugin
      ->getConfiguration()['layout'] == $form_state
      ->getValue('layout')) {
      $form_state
        ->setErrorByName('layout', $this
        ->t('You must select a different layout if you wish to change layouts.'));
    }
    if ($form['update_layout']['#access'] && $variant_plugin
      ->getConfiguration()['layout'] != $form_state
      ->getValue('layout') && $form_state
      ->getValue('op') != $form['update_layout']['#value']) {
      $form_state
        ->setErrorByName('layout', $this
        ->t('To select a different layout, you must click "Change Layout".'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LayoutPluginSelector::$manager protected property The layout plugin manager.
LayoutPluginSelector::$tempstore protected property The tempstore factory.
LayoutPluginSelector::buildForm public function Form constructor. Overrides FormInterface::buildForm
LayoutPluginSelector::create public static function Instantiates a new instance of this class. Overrides FormBase::create
LayoutPluginSelector::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LayoutPluginSelector::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LayoutPluginSelector::validateForm public function Form validation handler. Overrides FormBase::validateForm
LayoutPluginSelector::__construct public function LayoutPluginSelector constructor.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.