You are here

class LayoutChangeSettings in Panels 8.3

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

Provides a form for configuring a layout's settings.

Hierarchy

Expanded class hierarchy of LayoutChangeSettings

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

File

src/Form/LayoutChangeSettings.php, line 20

Namespace

Drupal\panels\Form
View source
class LayoutChangeSettings 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'));
  }

  /**
   * LayoutChangeSettings 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_settings_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['old_layout'] = [
      '#title' => $this
        ->t('Old Layout'),
      '#type' => 'select',
      '#options' => Layout::getLayoutOptions([
        'group_by_category' => TRUE,
      ]),
      '#default_value' => !empty($cached_values['layout_change']['old_layout']) ? $cached_values['layout_change']['old_layout'] : '',
      '#disabled' => TRUE,
      '#access' => !empty($cached_values['layout_change']),
    ];
    $form['new_layout'] = [
      '#title' => $this
        ->t('New Layout'),
      '#type' => 'select',
      '#options' => Layout::getLayoutOptions([
        'group_by_category' => TRUE,
      ]),
      '#default_value' => !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : '',
      '#disabled' => TRUE,
      '#access' => !empty($cached_values['layout_change']),
    ];

    // If a layout is already selected, show the layout settings.
    $form['layout_settings_wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Layout settings'),
      '#tree' => TRUE,
    ];
    $layout_settings = !empty($cached_values['layout_change']['layout_settings']) ? $cached_values['layout_change']['layout_settings'] : [];
    if (!$layout_settings && $variant_plugin
      ->getLayout() instanceof ConfigurablePluginInterface) {
      $layout_settings = $variant_plugin
        ->getLayout()
        ->getConfiguration();
    }
    $layout_id = !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : $variant_plugin
      ->getConfiguration()['layout'];
    $layout = Layout::layoutPluginManager()
      ->createInstance($layout_id, $layout_settings);
    $form['layout_settings_wrapper']['layout_settings'] = $layout
      ->buildConfigurationForm([], $form_state);
    return $form;
  }

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

    /** @var \Drupal\ctools\Wizard\EntityFormWizardInterface $wizard */
    $wizard = $form_state
      ->getFormObject();
    $next_params = $wizard
      ->getNextParameters($cached_values);

    /* @var $plugin \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant */
    $plugin = $cached_values['plugin'];
    $layout_id = !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : $plugin
      ->getConfiguration()['layout'];

    /** @var \Drupal\layout_plugin\Plugin\Layout\LayoutInterface $layout */
    $layout = Layout::layoutPluginManager()
      ->createInstance($layout_id, []);

    // If we're dealing with a form, submit it.
    if ($layout instanceof PluginFormInterface) {
      $sub_form_state = new FormState();
      $plugin_values = $form_state
        ->getValue([
        'layout_settings_wrapper',
        'layout_settings',
      ]);

      // If form values came through the step's submission, handle them.
      if ($plugin_values) {
        $sub_form_state
          ->setValues($plugin_values);
        $layout
          ->submitConfigurationForm($form, $sub_form_state);

        // If this plugin is configurable, get that configuration and set it in
        // cached values.
        if ($layout instanceof ConfigurablePluginInterface) {
          $cached_values = $this
            ->setCachedValues($next_params['step'], $plugin, $layout, $cached_values, $layout
            ->getConfiguration());
        }
      }
      else {
        $cached_values = $this
          ->setCachedValues($next_params['step'], $plugin, $layout, $cached_values, []);
      }
    }
    else {
      $cached_values = $this
        ->setCachedValues($next_params['step'], $plugin, $layout, $cached_values, []);
    }
    $form_state
      ->setTemporaryValue('wizard', $cached_values);
  }

  /**
   * Sets the appropriate cached values for the layout settings.
   *
   * Depending upon the next step, this form could be required to properly
   * update the values of the PanelsDisplayVariant plugin in the cached values
   * or it could just be adding the configuration to the cached values
   * directly. This bit of logic is repeated a number of times in the form
   * submission, and so abstracting it is typical DRY approach.
   *
   * @param string $next_step
   *   The next step of the wizard.
   * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $plugin
   *   The plugin to update.
   * @param \Drupal\layout_plugin\Plugin\Layout\LayoutInterface $layout
   *   The layout for which we are upating settings.
   * @param array $cached_values
   *   The current cached values from the wizard.
   * @param array $configuration
   *   The new configuration of the layout.
   *
   * @return mixed
   *   Returns the new cached values.
   */
  protected function setCachedValues($next_step, PanelsDisplayVariant $plugin, LayoutInterface $layout, $cached_values, $configuration) {

    // The step is modified by various wizards but will end in "regions"
    if (substr($next_step, 0 - 7) == 'regions') {
      $cached_values['layout_change']['layout_settings'] = $configuration;
    }
    else {
      $plugin
        ->setLayout($layout, $configuration);
      $cached_values['plugin'] = $plugin;
      unset($cached_values['layout_change']);
    }
    return $cached_values;
  }

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

    /* @var $plugin \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant */
    $plugin = $cached_values['plugin'];
    $layout_id = !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : $plugin
      ->getConfiguration()['layout'];
    $layout = Layout::layoutPluginManager()
      ->createInstance($layout_id, []);
    if ($layout instanceof PluginFormInterface) {
      $sub_form_state = new FormState();
      $plugin_values = $form_state
        ->getValue([
        'layout_settings_wrapper',
        'layout_settings',
      ]);
      if ($plugin_values) {
        $sub_form_state
          ->setValues($plugin_values);
        $layout
          ->validateConfigurationForm($form, $sub_form_state);
      }
    }
  }

}

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.
LayoutChangeSettings::$manager protected property The layout plugin manager.
LayoutChangeSettings::$tempstore protected property The tempstore factory.
LayoutChangeSettings::buildForm public function Form constructor. Overrides FormInterface::buildForm
LayoutChangeSettings::create public static function Instantiates a new instance of this class. Overrides FormBase::create
LayoutChangeSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LayoutChangeSettings::setCachedValues protected function Sets the appropriate cached values for the layout settings.
LayoutChangeSettings::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LayoutChangeSettings::validateForm public function Form validation handler. Overrides FormBase::validateForm
LayoutChangeSettings::__construct public function LayoutChangeSettings 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.