class LayoutChangeSettings in Panels 8.3
Same name and namespace in other branches
- 8.4 src/Form/LayoutChangeSettings.php \Drupal\panels\Form\LayoutChangeSettings
Provides a form for configuring a layout's settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\panels\Form\LayoutChangeSettings
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\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LayoutChangeSettings:: |
protected | property | The layout plugin manager. | |
LayoutChangeSettings:: |
protected | property | The tempstore factory. | |
LayoutChangeSettings:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
LayoutChangeSettings:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
LayoutChangeSettings:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
LayoutChangeSettings:: |
protected | function | Sets the appropriate cached values for the layout settings. | |
LayoutChangeSettings:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
LayoutChangeSettings:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
LayoutChangeSettings:: |
public | function | LayoutChangeSettings constructor. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |