Skip to content
Snippets Groups Projects
Commit b6f6ce5b authored by Sean Adams-Hiett's avatar Sean Adams-Hiett
Browse files

Went in a different direction when I realized that the event happens before...

Went in a different direction when I realized that the event happens before the administrative buttons are added.
parent 1b80b3a7
No related branches found
No related tags found
No related merge requests found
......@@ -18,105 +18,5 @@ use Drupal\Core\Url;
* The types to be altered.
*/
function lb_direct_add_element_info_alter(array &$types) {
$types['layout_builder']['#pre_render'][] = 'lb_direct_add_layout_builder_prerender';
}
function lb_direct_add_layout_builder_prerender(array $element) {
$lb = &$element['layout_builder'];
$section_storage = $element['#section_storage'];
// @todo Is there a better way to find the delta value?
$delta = 0;
$contexts = \Drupal::service('context.repository')->getAvailableContexts();
$storage_type = $section_storage->getStorageType();
$storage_id = $section_storage->getStorageId();
$block_manager = \Drupal::getContainer()->get('plugin.manager.block');
foreach (Element::children($lb) as $section) {
if (isset($lb[$section]['layout-builder__section'])) {
$sec = $lb[$section]['layout-builder__section'];
foreach (Element::children($lb[$section]['layout-builder__section']) as $region) {
$reg = $lb[$section]['layout-builder__section'][$region];
$definitions = $block_manager->getFilteredDefinitions('layout_builder', $contexts, [
'section_storage' => $section_storage,
'region' => $region,
'list' => 'inline_blocks',
]);
$blocks = $block_manager->getGroupedDefinitions($definitions);
$links = [];
$inline_blocks_category = (string) t('Inline blocks');
if (isset($blocks[$inline_blocks_category])) {
foreach ($blocks[$inline_blocks_category] as $block_id => $block) {
$attributes = [
'class' => [
'use-ajax',
// 'layout-builder__link',
// 'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
];
$attributes['class'][] = 'js-layout-builder-block-link';
$link = [
'title' => "Add {$block['admin_label']}",
'url' => Url::fromRoute('layout_builder.add_block',
[
'section_storage_type' => $section_storage->getStorageType(),
'section_storage' => $section_storage->getStorageId(),
'delta' => $delta,
'region' => $region,
'plugin_id' => $block_id,
]
),
'attributes' => $attributes,
];
$links[] = $link;
}
}
$links['other'] = [
'title' => t('More<span class="visually-hidden"> options</span>...'),
'url' => Url::fromRoute('layout_builder.choose_block',
[
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
'delta' => $delta,
'region' => $region,
],
[
'attributes' => [
'class' => [
'use-ajax',
// 'layout-builder__link',
// 'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
],
]
),
];
$lb[$section]['layout-builder__section'][$region]['layout_builder_add_block']['button'] = [
'#type' => 'dropbutton',
'#links' => $links,
];
unset($lb[$section]['layout-builder__section'][$region]['layout_builder_add_block']['link']);
}
// @todo Is there a better way to find the delta value?
$delta++;
}
}
return $element;
$types['layout_builder']['#pre_render'][] = Drupal\lb_direct_add\LayoutBuilder::class . '::preRender';
}
services:
lb_direct_add.element.prepare_layout:
class: Drupal\lb_direct_add\EventSubscriber\PrepareLayout
arguments: ['@layout_builder.tempstore_repository']
tags:
- { name: event_subscriber }
<?php
namespace Drupal\lb_direct_add\EventSubscriber;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Alters layout builder to create a drop-button for selecting custom blocks directly on the page.
*
* @package Drupal\lb_direct_add\EventSubscriber
*/
class PrepareLayout implements EventSubscriberInterface {
/**
* The layout tempstore repository.
*
* @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
*/
protected $layoutTempstoreRepository;
/**
* PrepareLayout constructor.
*
* @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
*/
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// @todo Determine whether the weight needs to be set.
$events[LayoutBuilderEvents::PREPARE_LAYOUT] = ['onPrepareLayout', 10];
return $events;
}
public function onPrepareLayout(PrepareLayoutEvent $event) {
}
}
<?php
namespace Drupal\lb_direct_add;
use Drupal\Core\Render\Element;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Url;
class LayoutBuilder implements TrustedCallbackInterface {
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['preRender'];
}
/**
* #pre_render callback: Alters layout builder to use dropbuttons to add custom blocks.
*/
public static function preRender($element) {
$lb = &$element['layout_builder'];
$section_storage = $element['#section_storage'];
// @todo Is there a better way to find the delta value?
$delta = 0;
$contexts = \Drupal::service('context.repository')->getAvailableContexts();
$storage_type = $section_storage->getStorageType();
$storage_id = $section_storage->getStorageId();
$block_manager = \Drupal::getContainer()->get('plugin.manager.block');
foreach (Element::children($lb) as $section) {
if (isset($lb[$section]['layout-builder__section'])) {
$sec = $lb[$section]['layout-builder__section'];
foreach (Element::children($lb[$section]['layout-builder__section']) as $region) {
$reg = $lb[$section]['layout-builder__section'][$region];
$definitions = $block_manager->getFilteredDefinitions('layout_builder', $contexts, [
'section_storage' => $section_storage,
'region' => $region,
'list' => 'inline_blocks',
]);
$blocks = $block_manager->getGroupedDefinitions($definitions);
$links = [];
$inline_blocks_category = (string) t('Inline blocks');
if (isset($blocks[$inline_blocks_category])) {
foreach ($blocks[$inline_blocks_category] as $block_id => $block) {
$attributes = [
'class' => [
'use-ajax',
// 'layout-builder__link',
// 'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
];
$attributes['class'][] = 'js-layout-builder-block-link';
$link = [
'title' => "Add {$block['admin_label']}",
'url' => Url::fromRoute('layout_builder.add_block',
[
'section_storage_type' => $section_storage->getStorageType(),
'section_storage' => $section_storage->getStorageId(),
'delta' => $delta,
'region' => $region,
'plugin_id' => $block_id,
]
),
'attributes' => $attributes,
];
$links[] = $link;
}
}
$links['other'] = [
'title' => t('More<span class="visually-hidden"> options</span>...'),
'url' => Url::fromRoute('layout_builder.choose_block',
[
'section_storage_type' => $storage_type,
'section_storage' => $storage_id,
'delta' => $delta,
'region' => $region,
],
[
'attributes' => [
'class' => [
'use-ajax',
// 'layout-builder__link',
// 'layout-builder__link--add',
],
'data-dialog-type' => 'dialog',
'data-dialog-renderer' => 'off_canvas',
],
]
),
];
$lb[$section]['layout-builder__section'][$region]['layout_builder_add_block']['button'] = [
'#type' => 'dropbutton',
'#links' => $links,
];
unset($lb[$section]['layout-builder__section'][$region]['layout_builder_add_block']['link']);
}
// @todo Is there a better way to find the delta value?
$delta++;
}
}
return $element;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment