You are here

public function BlockFormAlter::alterForm in Block Style Plugins 8.2

Alter a form and set block configuration.

This code will be run as part of a form alter so that the current blocks configuration will be available to the form.

Parameters

array $form: The form definition array for the block configuration form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

bool $layout_builder: TRUE if the current form is the Layout Builder component settings.

1 call to BlockFormAlter::alterForm()
BlockFormAlter::layoutBuilderEmbedAlter in src/BlockFormAlter.php
Alter the component settings form in Layout Builder settings tray.

File

src/BlockFormAlter.php, line 64

Class

BlockFormAlter
Base class for Block style plugins.

Namespace

Drupal\block_style_plugins

Code

public function alterForm(array &$form, FormStateInterface $form_state, $layout_builder = FALSE) {

  // Retrieve a list of style plugin definitions.
  $style_plugins = $this->blockStyleManager
    ->getBlockDefinitions();
  foreach ($style_plugins as $plugin_id => $plugin_definition) {
    if ($layout_builder && empty($plugin_definition['layout_builder_embed_form'])) {
      continue;
    }

    /** @var \Drupal\block_style_plugins\Plugin\BlockStyleInterface $style_plugin */
    $style_plugin = $this->blockStyleManager
      ->createInstance($plugin_id);

    // Get the current block config entity.
    $entity = $this
      ->getBlockConfigEntityFromFormState($form_state);

    // Set properties and configuration.
    $block_plugin = $entity
      ->getPlugin();
    $block_content_bundle_id = $this
      ->getBlockContentBundle($form_state);

    // Find the plugin ID or block content bundle id.
    $block_plugin_id = $block_plugin
      ->getPluginId();
    if ($block_content_bundle_id) {
      $block_plugin_id = $block_content_bundle_id;
    }

    // Check to see if this should only apply to includes or if it has been
    // excluded.
    if ($this
      ->allowStyles($block_plugin_id, $plugin_definition)) {

      // Create a fieldset to contain style fields.
      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);

      // 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',
  ]);
}