"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php" (26 Nov 2020, 9301 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ConfigureBlockFormBase.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\layout_builder\Form;
    4 
    5 use Drupal\Component\Utility\Html;
    6 use Drupal\Component\Uuid\UuidInterface;
    7 use Drupal\Core\Ajax\AjaxFormHelperTrait;
    8 use Drupal\Core\Block\BlockManagerInterface;
    9 use Drupal\Core\Block\BlockPluginInterface;
   10 use Drupal\Core\Form\BaseFormIdInterface;
   11 use Drupal\Core\Form\FormBase;
   12 use Drupal\Core\Form\FormStateInterface;
   13 use Drupal\Core\Form\SubformState;
   14 use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
   15 use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
   16 use Drupal\Core\Plugin\ContextAwarePluginInterface;
   17 use Drupal\Core\Plugin\PluginFormFactoryInterface;
   18 use Drupal\Core\Plugin\PluginWithFormsInterface;
   19 use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
   20 use Drupal\layout_builder\Controller\LayoutRebuildTrait;
   21 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
   22 use Drupal\layout_builder\SectionComponent;
   23 use Drupal\layout_builder\SectionStorageInterface;
   24 use Symfony\Component\DependencyInjection\ContainerInterface;
   25 
   26 /**
   27  * Provides a base form for configuring a block.
   28  *
   29  * @internal
   30  *   Form classes are internal.
   31  */
   32 abstract class ConfigureBlockFormBase extends FormBase implements BaseFormIdInterface {
   33 
   34   use AjaxFormHelperTrait;
   35   use ContextAwarePluginAssignmentTrait;
   36   use LayoutBuilderContextTrait;
   37   use LayoutRebuildTrait;
   38 
   39   /**
   40    * The plugin being configured.
   41    *
   42    * @var \Drupal\Core\Block\BlockPluginInterface
   43    */
   44   protected $block;
   45 
   46   /**
   47    * The layout tempstore repository.
   48    *
   49    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
   50    */
   51   protected $layoutTempstoreRepository;
   52 
   53   /**
   54    * The block manager.
   55    *
   56    * @var \Drupal\Core\Block\BlockManagerInterface
   57    */
   58   protected $blockManager;
   59 
   60   /**
   61    * The UUID generator.
   62    *
   63    * @var \Drupal\Component\Uuid\UuidInterface
   64    */
   65   protected $uuidGenerator;
   66 
   67   /**
   68    * The plugin form manager.
   69    *
   70    * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
   71    */
   72   protected $pluginFormFactory;
   73 
   74   /**
   75    * The field delta.
   76    *
   77    * @var int
   78    */
   79   protected $delta;
   80 
   81   /**
   82    * The current region.
   83    *
   84    * @var string
   85    */
   86   protected $region;
   87 
   88   /**
   89    * The UUID of the component.
   90    *
   91    * @var string
   92    */
   93   protected $uuid;
   94 
   95   /**
   96    * The section storage.
   97    *
   98    * @var \Drupal\layout_builder\SectionStorageInterface
   99    */
  100   protected $sectionStorage;
  101 
  102   /**
  103    * Constructs a new block form.
  104    *
  105    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
  106    *   The layout tempstore repository.
  107    * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
  108    *   The context repository.
  109    * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
  110    *   The block manager.
  111    * @param \Drupal\Component\Uuid\UuidInterface $uuid
  112    *   The UUID generator.
  113    * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
  114    *   The plugin form manager.
  115    */
  116   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ContextRepositoryInterface $context_repository, BlockManagerInterface $block_manager, UuidInterface $uuid, PluginFormFactoryInterface $plugin_form_manager) {
  117     $this->layoutTempstoreRepository = $layout_tempstore_repository;
  118     $this->contextRepository = $context_repository;
  119     $this->blockManager = $block_manager;
  120     $this->uuidGenerator = $uuid;
  121     $this->pluginFormFactory = $plugin_form_manager;
  122   }
  123 
  124   /**
  125    * {@inheritdoc}
  126    */
  127   public static function create(ContainerInterface $container) {
  128     return new static(
  129       $container->get('layout_builder.tempstore_repository'),
  130       $container->get('context.repository'),
  131       $container->get('plugin.manager.block'),
  132       $container->get('uuid'),
  133       $container->get('plugin_form.factory')
  134     );
  135   }
  136 
  137   /**
  138    * {@inheritdoc}
  139    */
  140   public function getBaseFormId() {
  141     return 'layout_builder_configure_block';
  142   }
  143 
  144   /**
  145    * Builds the form for the block.
  146    *
  147    * @param array $form
  148    *   An associative array containing the structure of the form.
  149    * @param \Drupal\Core\Form\FormStateInterface $form_state
  150    *   The current state of the form.
  151    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
  152    *   The section storage being configured.
  153    * @param int $delta
  154    *   The delta of the section.
  155    * @param \Drupal\layout_builder\SectionComponent $component
  156    *   The section component containing the block.
  157    *
  158    * @return array
  159    *   The form array.
  160    */
  161   public function doBuildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, SectionComponent $component = NULL) {
  162     $this->sectionStorage = $section_storage;
  163     $this->delta = $delta;
  164     $this->uuid = $component->getUuid();
  165     $this->block = $component->getPlugin();
  166 
  167     $form_state->setTemporaryValue('gathered_contexts', $this->getAvailableContexts($section_storage));
  168 
  169     // @todo Remove once https://www.drupal.org/node/2268787 is resolved.
  170     $form_state->set('block_theme', $this->config('system.theme')->get('default'));
  171 
  172     $form['#tree'] = TRUE;
  173     $form['settings'] = [];
  174     $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
  175     $form['settings'] = $this->getPluginForm($this->block)->buildConfigurationForm($form['settings'], $subform_state);
  176 
  177     $form['actions']['submit'] = [
  178       '#type' => 'submit',
  179       '#value' => $this->submitLabel(),
  180       '#button_type' => 'primary',
  181     ];
  182     if ($this->isAjax()) {
  183       $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
  184       // @todo static::ajaxSubmit() requires data-drupal-selector to be the same
  185       //   between the various Ajax requests. A bug in
  186       //   \Drupal\Core\Form\FormBuilder prevents that from happening unless
  187       //   $form['#id'] is also the same. Normally, #id is set to a unique HTML
  188       //   ID via Html::getUniqueId(), but here we bypass that in order to work
  189       //   around the data-drupal-selector bug. This is okay so long as we
  190       //   assume that this form only ever occurs once on a page. Remove this
  191       //   workaround in https://www.drupal.org/node/2897377.
  192       $form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']);
  193     }
  194 
  195     // Mark this as an administrative page for JavaScript ("Back to site" link).
  196     $form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
  197     return $form;
  198   }
  199 
  200   /**
  201    * Returns the label for the submit button.
  202    *
  203    * @return string
  204    *   Submit label.
  205    */
  206   abstract protected function submitLabel();
  207 
  208   /**
  209    * {@inheritdoc}
  210    */
  211   public function validateForm(array &$form, FormStateInterface $form_state) {
  212     $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
  213     $this->getPluginForm($this->block)->validateConfigurationForm($form['settings'], $subform_state);
  214   }
  215 
  216   /**
  217    * {@inheritdoc}
  218    */
  219   public function submitForm(array &$form, FormStateInterface $form_state) {
  220     // Call the plugin submit handler.
  221     $subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
  222     $this->getPluginForm($this->block)->submitConfigurationForm($form, $subform_state);
  223 
  224     // If this block is context-aware, set the context mapping.
  225     if ($this->block instanceof ContextAwarePluginInterface) {
  226       $this->block->setContextMapping($subform_state->getValue('context_mapping', []));
  227     }
  228 
  229     $configuration = $this->block->getConfiguration();
  230 
  231     $section = $this->sectionStorage->getSection($this->delta);
  232     $section->getComponent($this->uuid)->setConfiguration($configuration);
  233 
  234     $this->layoutTempstoreRepository->set($this->sectionStorage);
  235     $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl());
  236   }
  237 
  238   /**
  239    * {@inheritdoc}
  240    */
  241   protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
  242     return $this->rebuildAndClose($this->sectionStorage);
  243   }
  244 
  245   /**
  246    * Retrieves the plugin form for a given block.
  247    *
  248    * @param \Drupal\Core\Block\BlockPluginInterface $block
  249    *   The block plugin.
  250    *
  251    * @return \Drupal\Core\Plugin\PluginFormInterface
  252    *   The plugin form for the block.
  253    */
  254   protected function getPluginForm(BlockPluginInterface $block) {
  255     if ($block instanceof PluginWithFormsInterface) {
  256       return $this->pluginFormFactory->createInstance($block, 'configure');
  257     }
  258     return $block;
  259   }
  260 
  261   /**
  262    * Retrieves the section storage object.
  263    *
  264    * @return \Drupal\layout_builder\SectionStorageInterface
  265    *   The section storage for the current form.
  266    */
  267   public function getSectionStorage() {
  268     return $this->sectionStorage;
  269   }
  270 
  271   /**
  272    * Retrieves the current layout section being edited by the form.
  273    *
  274    * @return \Drupal\layout_builder\Section
  275    *   The current layout section.
  276    */
  277   public function getCurrentSection() {
  278     return $this->sectionStorage->getSection($this->delta);
  279   }
  280 
  281   /**
  282    * Retrieves the current component being edited by the form.
  283    *
  284    * @return \Drupal\layout_builder\SectionComponent
  285    *   The current section component.
  286    */
  287   public function getCurrentComponent() {
  288     return $this->getCurrentSection()->getComponent($this->uuid);
  289   }
  290 
  291 }