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

Added .info.yml, .module, and LayoutBuilder element override class. Updated README.

parent 078f61e8
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.
\ No newline at end of file
name: "Layout Builder Direct Add"
description: Converts the "Add block" link into a drop-button to directly add custom block types.
type: module
core: 8.x
dependencies:
- drupal:layout_builder
- drupal:block
<?php
/**
* @file
* Contains lb_direct_add.module.
*/
use Drupal\lb_direct_add\Element\LayoutBuilderDirectAdd;
/**
* Implements hook_element_plugin_alter().
*
* Requires 8.8.x+ or this patch: https://www.drupal.org/files/issues/2019-07-15/2987208-19.patch.
*/
function lb_enhancements_element_plugin_alter(array &$definitions) {
$definitions['layout_builder']['class'] = LayoutBuilderDirectAdd::class;
}
<?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();
$section = $section_storage->getSection($delta);
$layout = $section->getLayout();
$layout_definition = $layout->getPluginDefinition();
$region_labels = $layout_definition->getRegionLabels();
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