Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / src / Form / ConfigureBlockFormBase.php
diff --git a/web/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php b/web/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php
new file mode 100644 (file)
index 0000000..e1103e9
--- /dev/null
@@ -0,0 +1,244 @@
+<?php
+
+namespace Drupal\layout_builder\Form;
+
+use Drupal\Component\Uuid\UuidInterface;
+use Drupal\Core\Block\BlockManagerInterface;
+use Drupal\Core\Block\BlockPluginInterface;
+use Drupal\Core\DependencyInjection\ClassResolverInterface;
+use Drupal\Core\Form\FormBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Form\SubformState;
+use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
+use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
+use Drupal\Core\Plugin\ContextAwarePluginInterface;
+use Drupal\Core\Plugin\PluginFormFactoryInterface;
+use Drupal\Core\Plugin\PluginWithFormsInterface;
+use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
+use Drupal\layout_builder\Controller\LayoutRebuildTrait;
+use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
+use Drupal\layout_builder\SectionComponent;
+use Drupal\layout_builder\SectionStorageInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Provides a base form for configuring a block.
+ *
+ * @internal
+ */
+abstract class ConfigureBlockFormBase extends FormBase {
+
+  use AjaxFormHelperTrait;
+  use ContextAwarePluginAssignmentTrait;
+  use LayoutBuilderContextTrait;
+  use LayoutRebuildTrait;
+
+  /**
+   * The plugin being configured.
+   *
+   * @var \Drupal\Core\Block\BlockPluginInterface
+   */
+  protected $block;
+
+  /**
+   * The layout tempstore repository.
+   *
+   * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
+   */
+  protected $layoutTempstoreRepository;
+
+  /**
+   * The block manager.
+   *
+   * @var \Drupal\Core\Block\BlockManagerInterface
+   */
+  protected $blockManager;
+
+  /**
+   * The UUID generator.
+   *
+   * @var \Drupal\Component\Uuid\UuidInterface
+   */
+  protected $uuidGenerator;
+
+  /**
+   * The plugin form manager.
+   *
+   * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
+   */
+  protected $pluginFormFactory;
+
+  /**
+   * The field delta.
+   *
+   * @var int
+   */
+  protected $delta;
+
+  /**
+   * The current region.
+   *
+   * @var string
+   */
+  protected $region;
+
+  /**
+   * The UUID of the component.
+   *
+   * @var string
+   */
+  protected $uuid;
+
+  /**
+   * The section storage.
+   *
+   * @var \Drupal\layout_builder\SectionStorageInterface
+   */
+  protected $sectionStorage;
+
+  /**
+   * Constructs a new block form.
+   *
+   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
+   *   The layout tempstore repository.
+   * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
+   *   The context repository.
+   * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
+   *   The block manager.
+   * @param \Drupal\Component\Uuid\UuidInterface $uuid
+   *   The UUID generator.
+   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
+   *   The class resolver.
+   * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
+   *   The plugin form manager.
+   */
+  public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ContextRepositoryInterface $context_repository, BlockManagerInterface $block_manager, UuidInterface $uuid, ClassResolverInterface $class_resolver, PluginFormFactoryInterface $plugin_form_manager) {
+    $this->layoutTempstoreRepository = $layout_tempstore_repository;
+    $this->contextRepository = $context_repository;
+    $this->blockManager = $block_manager;
+    $this->uuidGenerator = $uuid;
+    $this->classResolver = $class_resolver;
+    $this->pluginFormFactory = $plugin_form_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('layout_builder.tempstore_repository'),
+      $container->get('context.repository'),
+      $container->get('plugin.manager.block'),
+      $container->get('uuid'),
+      $container->get('class_resolver'),
+      $container->get('plugin_form.factory')
+    );
+  }
+
+  /**
+   * Builds the form for the block.
+   *
+   * @param array $form
+   *   An associative array containing the structure of the form.
+   * @param \Drupal\Core\Form\FormStateInterface $form_state
+   *   The current state of the form.
+   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
+   *   The section storage being configured.
+   * @param int $delta
+   *   The delta of the section.
+   * @param \Drupal\layout_builder\SectionComponent $component
+   *   The section component containing the block.
+   *
+   * @return array
+   *   The form array.
+   */
+  public function doBuildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, SectionComponent $component = NULL) {
+    $this->sectionStorage = $section_storage;
+    $this->delta = $delta;
+    $this->uuid = $component->getUuid();
+    $this->block = $component->getPlugin();
+
+    $form_state->setTemporaryValue('gathered_contexts', $this->getAvailableContexts($section_storage));
+
+    // @todo Remove once https://www.drupal.org/node/2268787 is resolved.
+    $form_state->set('block_theme', $this->config('system.theme')->get('default'));
+
+    $form['#tree'] = TRUE;
+    $form['settings'] = [];
+    $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
+    $form['settings'] = $this->getPluginForm($this->block)->buildConfigurationForm($form['settings'], $subform_state);
+
+    $form['actions']['submit'] = [
+      '#type' => 'submit',
+      '#value' => $this->submitLabel(),
+      '#button_type' => 'primary',
+    ];
+    if ($this->isAjax()) {
+      $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
+    }
+
+    return $form;
+  }
+
+  /**
+   * Returns the label for the submit button.
+   *
+   * @return string
+   *   Submit label.
+   */
+  abstract protected function submitLabel();
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateForm(array &$form, FormStateInterface $form_state) {
+    $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
+    $this->getPluginForm($this->block)->validateConfigurationForm($form['settings'], $subform_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitForm(array &$form, FormStateInterface $form_state) {
+    // Call the plugin submit handler.
+    $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
+    $this->getPluginForm($this->block)->submitConfigurationForm($form, $subform_state);
+
+    // If this block is context-aware, set the context mapping.
+    if ($this->block instanceof ContextAwarePluginInterface) {
+      $this->block->setContextMapping($subform_state->getValue('context_mapping', []));
+    }
+
+    $configuration = $this->block->getConfiguration();
+
+    $section = $this->sectionStorage->getSection($this->delta);
+    $section->getComponent($this->uuid)->setConfiguration($configuration);
+
+    $this->layoutTempstoreRepository->set($this->sectionStorage);
+    $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl());
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
+    return $this->rebuildAndClose($this->sectionStorage);
+  }
+
+  /**
+   * Retrieves the plugin form for a given block.
+   *
+   * @param \Drupal\Core\Block\BlockPluginInterface $block
+   *   The block plugin.
+   *
+   * @return \Drupal\Core\Plugin\PluginFormInterface
+   *   The plugin form for the block.
+   */
+  protected function getPluginForm(BlockPluginInterface $block) {
+    if ($block instanceof PluginWithFormsInterface) {
+      return $this->pluginFormFactory->createInstance($block, 'configure');
+    }
+    return $block;
+  }
+
+}