class SectionFormAlter in Block Style Plugins 8.2
Base class for Section style plugins.
Hierarchy
- class \Drupal\block_style_plugins\SectionFormAlter implements ContainerInjectionInterface uses FormEntityHelperTrait, IncludeExcludeStyleTrait, StringTranslationTrait
Expanded class hierarchy of SectionFormAlter
2 files declare their use of SectionFormAlter
- block_style_plugins.module in ./
block_style_plugins.module - Contains block_style_plugins.module.
- SectionFormAlterTest.php in tests/
src/ Unit/ SectionFormAlterTest.php
File
- src/
SectionFormAlter.php, line 16
Namespace
Drupal\block_style_pluginsView source
class SectionFormAlter implements ContainerInjectionInterface {
use StringTranslationTrait;
use IncludeExcludeStyleTrait;
use FormEntityHelperTrait;
/**
* Instance of the Entity Repository service.
*
* @var \Drupal\block_style_plugins\Plugin\BlockStyleManager
*/
protected $blockStyleManager;
/**
* Construct method for SectionFormAlter.
*
* @param \Drupal\block_style_plugins\Plugin\BlockStyleManager $blockStyleManager
* A Block Style Manager instance.
*/
public function __construct(BlockStyleManager $blockStyleManager) {
$this->blockStyleManager = $blockStyleManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.block_style.processor'));
}
/**
* Alter a form and set Layout Builder Section configuration.
*
* This code will be run as part of a form alter so that the current section
* configuration will be available to the form.
*
* @param array $form
* The form definition array for the section configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function alterForm(array &$form, FormStateInterface $form_state) {
// Check if this is a new section. Exit if this is a new section and the
// patch from https://www.drupal.org/project/drupal/issues/3044117 has not
// been applied.
$new_section = strpos($form['#attributes']['data-layout-builder-target-highlight-id'], 'update') === FALSE;
if (!method_exists('\\Drupal\\layout_builder\\Form\\ConfigureSectionForm', 'getCurrentSection') && $new_section) {
return;
}
// Get the current section config entity. Exit if there is no section.
$section = $this
->getSectionFromFormState($form_state);
if (!$section) {
return;
}
// Find the section ID.
$section_plugin_id = $section
->getLayoutId();
// Retrieve a list of style plugin definitions.
$style_plugins = $this->blockStyleManager
->getSectionDefinitions();
foreach ($style_plugins as $plugin_id => $plugin_definition) {
/** @var \Drupal\block_style_plugins\Plugin\BlockStyleInterface $style_plugin */
$style_plugin = $this->blockStyleManager
->createInstance($plugin_id);
// Check to see if this should only apply to includes or if it has been
// excluded.
if ($this
->allowStyles($section_plugin_id, $plugin_definition)) {
// Create a fieldset to contain style fields.
if (!isset($form['block_styles'])) {
$form['block_styles'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Section Styles'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#weight' => 0,
];
}
$styles = $section
->getThirdPartySetting('block_style_plugins', $plugin_id, []);
$style_plugin
->setConfiguration($styles);
// Create containers to place each plugin style settings into the styles
// fieldset.
$form['third_party_settings']['block_style_plugins'][$plugin_id] = [
'#type' => 'container',
'#group' => 'block_styles',
];
// Allow plugins to add field elements to this form.
if ($style_plugin instanceof PluginFormInterface) {
$subform_state = SubformState::createForSubform($form['third_party_settings']['block_style_plugins'][$plugin_id], $form, $form_state);
$form['third_party_settings']['block_style_plugins'][$plugin_id] += $style_plugin
->buildConfigurationForm($form['third_party_settings']['block_style_plugins'][$plugin_id], $subform_state);
}
}
}
$form['#validate'][] = [
$this,
'validateForm',
];
$form['#submit'] = $form['#submit'] ?? [];
array_unshift($form['#submit'], [
$this,
'submitForm',
]);
}
/**
* Validate the section form.
*
* @param array $form
* The form definition array for the block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function validateForm(array $form, FormStateInterface $form_state) {
// Retrieve a list of possible style plugin definitions.
$available_plugins = $this->blockStyleManager
->getSectionDefinitions();
$style_settings = $form_state
->getValue([
'third_party_settings',
'block_style_plugins',
], []);
foreach ($style_settings as $plugin_id => $settings) {
// Only instantiate plugins that are available.
if (array_key_exists($plugin_id, $available_plugins)) {
/** @var \Drupal\block_style_plugins\Plugin\BlockStyleInterface $style_plugin */
$style_plugin = $this->blockStyleManager
->createInstance($plugin_id);
if ($style_plugin instanceof PluginFormInterface) {
$subform_state = SubformState::createForSubform($form['third_party_settings']['block_style_plugins'][$plugin_id], $form, $form_state);
$style_plugin
->validateConfigurationForm($form['third_party_settings']['block_style_plugins'][$plugin_id], $subform_state);
}
}
}
}
/**
* Submit the section form and save configuration.
*
* @param array $form
* The form definition array for the block configuration form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array $form, FormStateInterface $form_state) {
// Retrieve a list of possible style plugin definitions.
$available_plugins = $this->blockStyleManager
->getSectionDefinitions();
$style_settings = $form_state
->getValue([
'third_party_settings',
'block_style_plugins',
], []);
$section = $this
->getSectionFromFormState($form_state);
foreach ($style_settings as $plugin_id => $styles) {
// Only instantiate plugins that are available.
if (array_key_exists($plugin_id, $available_plugins)) {
/** @var \Drupal\block_style_plugins\Plugin\BlockStyleInterface $style_plugin */
$style_plugin = $this->blockStyleManager
->createInstance($plugin_id);
$style_plugin
->setConfiguration($styles);
if ($style_plugin instanceof PluginFormInterface) {
$subform_state = SubformState::createForSubform($form['third_party_settings']['block_style_plugins'][$plugin_id], $form, $form_state);
$style_plugin
->submitConfigurationForm($form['third_party_settings']['block_style_plugins'][$plugin_id], $subform_state);
}
// Save the style to the Section.
$section
->setThirdPartySetting('block_style_plugins', $plugin_id, $style_plugin
->getConfiguration());
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormEntityHelperTrait:: |
protected | property | Instance of the Entity Repository service. | |
FormEntityHelperTrait:: |
protected | function | Get the Block config entity from the form state. | |
FormEntityHelperTrait:: |
protected | function | Get the Block Content entity. | |
FormEntityHelperTrait:: |
public | function | Get the block content bundle type. | |
FormEntityHelperTrait:: |
protected | function | Get the config entity of the entity being styled from the form state. | |
FormEntityHelperTrait:: |
protected | function | Gets the Entity Repository service. | |
FormEntityHelperTrait:: |
protected | function | Get the Section config entity from the form state. | |
IncludeExcludeStyleTrait:: |
public | function | Determine whether a style should be allowed. | |
IncludeExcludeStyleTrait:: |
public | function | Exclude styles from appearing on blocks. | |
IncludeExcludeStyleTrait:: |
public | function | Only show styles on specific blocks. | |
IncludeExcludeStyleTrait:: |
protected | function | Match a plugin ID against a list of possible plugin IDs. | |
SectionFormAlter:: |
protected | property | Instance of the Entity Repository service. | |
SectionFormAlter:: |
public | function | Alter a form and set Layout Builder Section configuration. | |
SectionFormAlter:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
SectionFormAlter:: |
public | function | Submit the section form and save configuration. | |
SectionFormAlter:: |
public | function | Validate the section form. | |
SectionFormAlter:: |
public | function | Construct method for SectionFormAlter. | |
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. |