You are here

public function SectionFormAlter::alterForm in Block Style Plugins 8.2

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.

Parameters

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

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

File

src/SectionFormAlter.php, line 59

Class

SectionFormAlter
Base class for Section style plugins.

Namespace

Drupal\block_style_plugins

Code

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