View source
<?php
namespace Drupal\block_style_plugins;
use Drupal\block_style_plugins\Plugin\BlockStyleManager;
use Drupal\Core\Form\SubformState;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\layout_builder\SectionComponent;
class BlockFormAlter implements ContainerInjectionInterface {
use StringTranslationTrait;
use IncludeExcludeStyleTrait;
use FormEntityHelperTrait;
use DependencySerializationTrait;
protected $blockStyleManager;
public function __construct(BlockStyleManager $blockStyleManager) {
$this->blockStyleManager = $blockStyleManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.block_style.processor'));
}
public function alterForm(array &$form, FormStateInterface $form_state, $layout_builder = FALSE) {
$style_plugins = $this->blockStyleManager
->getBlockDefinitions();
foreach ($style_plugins as $plugin_id => $plugin_definition) {
if ($layout_builder && empty($plugin_definition['layout_builder_embed_form'])) {
continue;
}
$style_plugin = $this->blockStyleManager
->createInstance($plugin_id);
$entity = $this
->getBlockConfigEntityFromFormState($form_state);
$block_plugin = $entity
->getPlugin();
$block_content_bundle_id = $this
->getBlockContentBundle($form_state);
$block_plugin_id = $block_plugin
->getPluginId();
if ($block_content_bundle_id) {
$block_plugin_id = $block_content_bundle_id;
}
if ($this
->allowStyles($block_plugin_id, $plugin_definition)) {
if (!isset($form['block_styles'])) {
$form['block_styles'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Block Styles'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#weight' => 0,
];
}
$styles = $entity
->getThirdPartySetting('block_style_plugins', $plugin_id, []);
$style_plugin
->setConfiguration($styles);
$form['third_party_settings']['block_style_plugins'][$plugin_id] = [
'#type' => 'container',
'#group' => 'block_styles',
];
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',
]);
}
public function layoutBuilderEmbedAlter(array &$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'layout_builder_update_block' || $form_id == 'layout_builder_add_block') {
$this
->alterForm($form, $form_state, TRUE);
}
}
public function validateForm(array $form, FormStateInterface $form_state) {
$available_plugins = $this->blockStyleManager
->getBlockDefinitions();
$style_settings = $form_state
->getValue([
'third_party_settings',
'block_style_plugins',
]);
foreach ($style_settings as $plugin_id => $settings) {
if (array_key_exists($plugin_id, $available_plugins)) {
$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);
}
}
}
}
public function submitForm(array $form, FormStateInterface $form_state) {
$available_plugins = $this->blockStyleManager
->getBlockDefinitions();
$style_settings = $form_state
->getValue([
'third_party_settings',
'block_style_plugins',
]);
$component = $this
->getBlockConfigEntityFromFormState($form_state);
foreach ($style_settings as $plugin_id => $styles) {
if (array_key_exists($plugin_id, $available_plugins)) {
$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);
}
if ($component instanceof SectionComponent) {
$component
->setThirdPartySetting('block_style_plugins', $plugin_id, $style_plugin
->getConfiguration());
}
}
}
}
}