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

Moved direct add button logic into a pre-render function to avoid having to...

Moved direct add button logic into a pre-render function to avoid having to override the LB class. Removed the unused class. Other code clean-up.
parent 3e890143
No related branches found
No related tags found
No related merge requests found
# Layout Builder Direct Add
Converts the "Add block" link into a drop-button to directly add custom block types. The original "Add block" action is preserved as a "More..." link.
Requires Drupal 8.8.x+ or this patch: https://www.drupal.org/files/issues/2019-07-15/2987208-19.patch.
\ No newline at end of file
......@@ -5,13 +5,109 @@
* Contains lb_direct_add.module.
*/
use Drupal\lb_direct_add\Element\LayoutBuilderDirectAdd;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
/**
* Implements hook_element_plugin_alter().
* Implementation of hook_element_info_alter().
*
* Requires 8.8.x+ or this patch: https://www.drupal.org/files/issues/2019-07-15/2987208-19.patch.
* This doesn't work because it is for changing broad-level definitions for how elements
* are handled. It doesn't allow for setting the configuration for an individual element.
*
* @param array $types
* The types to be altered.
*/
function lb_direct_add_element_plugin_alter(array &$definitions) {
$definitions['layout_builder']['class'] = LayoutBuilderDirectAdd::class;
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'];
$delta = $element['#delta'];
$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'])) {
foreach (Element::children($lb[$section]['layout-builder__section']) as $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']);
}
}
}
return $element;
}
<?php
namespace Drupal\lb_direct_add\Element;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\layout_builder\Element\LayoutBuilder;
use Drupal\layout_builder\SectionStorageInterface;
/**
* An override class to change the behavior of LayoutBuilder.
*/
class LayoutBuilderDirectAdd extends LayoutBuilder {
/**
* {@inheritdoc}
*/
protected function buildAdministrativeSection(SectionStorageInterface $section_storage, $delta) {
$build = parent::buildAdministrativeSection($section_storage, $delta);
$storage_type = $section_storage->getStorageType();
$storage_id = $section_storage->getStorageId();
foreach (Element::children($build['layout-builder__section']) as $region) {
$block_manager = \Drupal::getContainer()->get('plugin.manager.block');
$definitions = $block_manager->getFilteredDefinitions('layout_builder', $this->getAvailableContexts($section_storage), [
'section_storage' => $section_storage,
'region' => $region,
'list' => 'inline_blocks',
]);
$blocks = $block_manager->getGroupedDefinitions($definitions);
$links = [];
$inline_blocks_category = (string) $this->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' => $this->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',
],
]
),
];
$build['layout-builder__section'][$region]['layout_builder_add_block']['button'] = [
'#type' => 'dropbutton',
'#links' => $links,
];
unset($build['layout-builder__section'][$region]['layout_builder_add_block']['link']);
}
return $build;
}
}
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