You are here

public function BlockFormAlter::submitForm in Block Style Plugins 8.2

Submit the form and save configuration.

Parameters

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

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

File

src/BlockFormAlter.php, line 177

Class

BlockFormAlter
Base class for Block style plugins.

Namespace

Drupal\block_style_plugins

Code

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

  // Retrieve a list of possible style plugin definitions.
  $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) {

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

      // Embedded form on Layout Builder settings tray needs styles saved to
      // the component.
      if ($component instanceof SectionComponent) {
        $component
          ->setThirdPartySetting('block_style_plugins', $plugin_id, $style_plugin
          ->getConfiguration());
      }
    }
  }
}