LayoutBuilderAddBlockController.php in Panopoly 8.2
File
modules/panopoly/panopoly_magic/src/Controller/LayoutBuilderAddBlockController.php
View source
<?php
namespace Drupal\panopoly_magic\Controller;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\Controller\LayoutRebuildTrait;
use Drupal\layout_builder\LayoutBuilderHighlightTrait;
use Drupal\layout_builder\SectionComponent;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\panopoly_magic\Form\LayoutBuilderUpdateBlockForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LayoutBuilderAddBlockController extends ControllerBase {
use AjaxHelperTrait;
use LayoutRebuildTrait;
use LayoutBuilderHighlightTrait;
use LayoutBuilderContextTrait;
protected $formBuilder;
protected $uuid;
protected $contextHandler;
protected $blockPluginManager;
public function __construct(FormBuilderInterface $form_builder, UuidInterface $uuid, ContextHandlerInterface $context_handler, BlockManagerInterface $block_plugin_manager) {
$this->formBuilder = $form_builder;
$this->uuid = $uuid;
$this->contextHandler = $context_handler;
$this->blockPluginManager = $block_plugin_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('form_builder'), $container
->get('uuid'), $container
->get('context.handler'), $container
->get('plugin.manager.block'));
}
public function addBlock(SectionStorageInterface $section_storage, $delta, $region, $plugin_id) {
$plugin_configuration = [
'id' => $plugin_id,
];
$plugin = $this->blockPluginManager
->createInstance($plugin_id, [
'id' => $plugin_id,
]);
$contexts = $this
->getAvailableContexts($section_storage);
if ($contexts && $plugin instanceof ContextAwarePluginInterface) {
$context_mapping = [];
foreach ($plugin
->getContextDefinitions() as $context_slot => $definition) {
if ($definition
->isRequired()) {
$valid_contexts = $this->contextHandler
->getMatchingContexts($contexts, $definition);
reset($valid_contexts);
$context_id = key($valid_contexts);
$context_mapping[$context_slot] = $context_id;
}
}
$plugin
->setContextMapping($context_mapping);
$plugin_configuration = $plugin
->getConfiguration();
}
$component = new SectionComponent($this->uuid
->generate(), $region, $plugin_configuration);
$section_storage
->getSection($delta)
->appendComponent($component);
$response = $this
->rebuildLayout($section_storage);
$form = $this->formBuilder
->getForm(LayoutBuilderUpdateBlockForm::class, $section_storage, $delta, $region, $component
->getUuid());
$response
->addCommand(new HtmlCommand('#drupal-off-canvas', $form));
return $response;
}
}