You are here

public function BlockStyleBase::prepareForm in Block Style Plugins 8

Returns the configuration form elements specific to a block configuration.

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

Parameters

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

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

Return value

array The renderable form array representing the entire configuration form.

Overrides BlockStyleInterface::prepareForm

File

src/Plugin/BlockStyleBase.php, line 123

Class

BlockStyleBase
Base class for Block style plugins.

Namespace

Drupal\block_style_plugins\Plugin

Code

public function prepareForm(array $form, FormStateInterface $form_state) {

  // Get the current block config entity.

  /** @var \Drupal\block\Entity\Block $entity */
  $entity = $form_state
    ->getFormObject()
    ->getEntity();

  // Set properties and configuration.
  $this->blockPlugin = $entity
    ->getPlugin();
  $this
    ->setBlockContentBundle();

  // Check to see if this should only apply to includes or if it has been
  // excluded.
  if ($this
    ->includeOnly() && !$this
    ->exclude()) {

    // 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', $this->pluginId);
    $styles = is_array($styles) ? $styles : [];
    $this
      ->setConfiguration($styles);

    // Create containers to place each plugin style settings into the styles
    // fieldset.
    $form['third_party_settings']['block_style_plugins'][$this->pluginId] = [
      '#type' => 'container',
      '#group' => 'block_styles',
    ];

    // Allow plugins to add field elements to this form.
    $subform_state = SubformState::createForSubform($form['third_party_settings']['block_style_plugins'][$this->pluginId], $form, $form_state);
    $form['third_party_settings']['block_style_plugins'][$this->pluginId] += $this
      ->buildConfigurationForm($form['third_party_settings']['block_style_plugins'][$this->pluginId], $subform_state);

    // Allow plugins to alter this form.
    $form = $this
      ->formAlter($form, $form_state);

    // Add form Validation.
    $form['#validate'][] = [
      $this,
      'validateForm',
    ];

    // Add the submitForm method to the form.
    array_unshift($form['actions']['submit']['#submit'], [
      $this,
      'submitForm',
    ]);
  }
  return $form;
}