class LayoutBuilderAddBlockController in Panopoly 8.2
Controller for adding blocks in layout builder.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\panopoly_magic\Controller\LayoutBuilderAddBlockController uses AjaxHelperTrait, LayoutBuilderContextTrait, LayoutRebuildTrait, LayoutBuilderHighlightTrait
Expanded class hierarchy of LayoutBuilderAddBlockController
File
- modules/
panopoly/ panopoly_magic/ src/ Controller/ LayoutBuilderAddBlockController.php, line 24
Namespace
Drupal\panopoly_magic\ControllerView source
class LayoutBuilderAddBlockController extends ControllerBase {
use AjaxHelperTrait;
use LayoutRebuildTrait;
use LayoutBuilderHighlightTrait;
use LayoutBuilderContextTrait;
/**
* The form builder service.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* The Uuid generator service.
*
* @var \Drupal\Component\Uuid\UuidInterface
*/
protected $uuid;
/**
* The context handler.
*
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
*/
protected $contextHandler;
/**
* The block plugin manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockPluginManager;
/**
* LayoutBuilderAddBlockController constructor.
*
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder service.
* @param \Drupal\Component\Uuid\UuidInterface $uuid
* The uuid generator service.
* @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
* The context handler service.
* @param \Drupal\Core\Block\BlockManagerInterface $block_plugin_manager
* The block manager server.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* Adds the new block to layout builder and opens the configuration form.
*
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage.
* @param int $delta
* The delta of the section to add the block to.
* @param string $region
* The region for the section.
* @param string $plugin_id
* The plugin ID of the layout to add.
*
* @return \Symfony\Component\HttpFoundation\Response|array
* The controller response.
*/
public function addBlock(SectionStorageInterface $section_storage, $delta, $region, $plugin_id) {
// Create a new component and add it to the section storage.
$plugin_configuration = [
'id' => $plugin_id,
];
/** @var \Drupal\Core\Block\BlockPluginInterface $plugin */
$plugin = $this->blockPluginManager
->createInstance($plugin_id, [
'id' => $plugin_id,
]);
$contexts = $this
->getAvailableContexts($section_storage);
// Map contexts for plugins that need them.
if ($contexts && $plugin instanceof ContextAwarePluginInterface) {
$context_mapping = [];
foreach ($plugin
->getContextDefinitions() as $context_slot => $definition) {
// If the context is required, we have to give it something.
if ($definition
->isRequired()) {
$valid_contexts = $this->contextHandler
->getMatchingContexts($contexts, $definition);
// Get the first context id, and assign that to the slot. The user can
// change it later.
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);
// Rebuild the layout.
$response = $this
->rebuildLayout($section_storage);
// Build the panopoly magic update block form and open it in the off canvas.
$form = $this->formBuilder
->getForm(LayoutBuilderUpdateBlockForm::class, $section_storage, $delta, $region, $component
->getUuid());
$response
->addCommand(new HtmlCommand('#drupal-off-canvas', $form));
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxHelperTrait:: |
protected | function | Gets the wrapper format of the current request. | |
AjaxHelperTrait:: |
protected | function | Determines if the current request is via AJAX. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LayoutBuilderAddBlockController:: |
protected | property | The block plugin manager. | |
LayoutBuilderAddBlockController:: |
protected | property | The context handler. | |
LayoutBuilderAddBlockController:: |
protected | property |
The form builder service. Overrides ControllerBase:: |
|
LayoutBuilderAddBlockController:: |
protected | property | The Uuid generator service. | |
LayoutBuilderAddBlockController:: |
public | function | Adds the new block to layout builder and opens the configuration form. | |
LayoutBuilderAddBlockController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
LayoutBuilderAddBlockController:: |
public | function | LayoutBuilderAddBlockController constructor. | |
LayoutBuilderContextTrait:: |
protected | property | The context repository. | |
LayoutBuilderContextTrait:: |
protected | function | Gets the context repository service. | |
LayoutBuilderContextTrait:: |
protected | function | Provides all available contexts, both global and section_storage-specific. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutRebuildTrait:: |
protected | function | Rebuilds the layout. | |
LayoutRebuildTrait:: |
protected | function | Rebuilds the layout. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |