layout_builder_component_attributes.module in Layout Builder Component Attributes 1.0.x
Same filename and directory in other branches
Layout Builder Component Attributes module.
File
layout_builder_component_attributes.moduleView source
<?php
/**
* @file
* Layout Builder Component Attributes module.
*/
/**
* Implements template_preprocess_block().
*/
function layout_builder_component_attributes_preprocess_block(&$variables) {
$config = \Drupal::config('layout_builder_component_attributes.settings')
->get();
if (isset($variables['elements']['#component_attributes'])) {
$component_attributes = $variables['elements']['#component_attributes'];
// Process block attributes.
if ($config['allowed_block_attributes']['id'] && $component_attributes['block_attributes']['id']) {
$variables['attributes']['id'] = $component_attributes['block_attributes']['id'];
}
if ($config['allowed_block_attributes']['class'] && $component_attributes['block_attributes']['class']) {
$classes = explode(' ', $component_attributes['block_attributes']['class']);
$existing_classes = $variables['attributes']['class'] ?? [];
$variables['attributes']['class'] = array_merge($existing_classes, $classes);
}
if ($config['allowed_block_attributes']['style'] && $component_attributes['block_attributes']['style']) {
$variables['attributes']['style'] = $component_attributes['block_attributes']['style'];
}
if ($config['allowed_block_attributes']['data'] && $component_attributes['block_attributes']['data']) {
$data_attributes = preg_split('/\\R/', $component_attributes['block_attributes']['data']);
foreach ($data_attributes as $data_attribute) {
$data_attribute = explode('|', $data_attribute);
// Values are optional for data-* attributes.
$variables['attributes'][$data_attribute[0]] = $data_attribute[1] ?? TRUE;
}
}
// Process block title attributes.
if ($config['allowed_block_title_attributes']['id'] && $component_attributes['block_title_attributes']['id']) {
$variables['title_attributes']['id'] = $component_attributes['block_title_attributes']['id'];
}
if ($config['allowed_block_title_attributes']['class'] && $component_attributes['block_title_attributes']['class']) {
$classes = explode(' ', $component_attributes['block_title_attributes']['class']);
$existing_classes = $variables['title_attributes']['class'] ?? [];
$variables['title_attributes']['class'] = array_merge($existing_classes, $classes);
}
if ($config['allowed_block_title_attributes']['style'] && $component_attributes['block_title_attributes']['style']) {
$variables['title_attributes']['style'] = $component_attributes['block_title_attributes']['style'];
}
if ($config['allowed_block_title_attributes']['data'] && $component_attributes['block_title_attributes']['data']) {
$data_attributes = preg_split('/\\R/', $component_attributes['block_title_attributes']['data']);
foreach ($data_attributes as $data_attribute) {
$data_attribute = explode('|', $data_attribute);
// Values are optional for data-* attributes.
$variables['title_attributes'][$data_attribute[0]] = $data_attribute[1] ?? TRUE;
}
}
// Process block content attributes.
if ($config['allowed_block_content_attributes']['id'] && $component_attributes['block_content_attributes']['id']) {
$variables['content_attributes']['id'] = $component_attributes['block_content_attributes']['id'];
}
if ($config['allowed_block_content_attributes']['class'] && $component_attributes['block_content_attributes']['class']) {
$classes = explode(' ', $component_attributes['block_content_attributes']['class']);
$existing_classes = $variables['content_attributes']['class'] ?? [];
$variables['content_attributes']['class'] = array_merge($existing_classes, $classes);
}
if ($config['allowed_block_content_attributes']['style'] && $component_attributes['block_content_attributes']['style']) {
$variables['content_attributes']['style'] = $component_attributes['block_content_attributes']['style'];
}
if ($config['allowed_block_content_attributes']['data'] && $component_attributes['block_content_attributes']['data']) {
$data_attributes = preg_split('/\\R/', $component_attributes['block_content_attributes']['data']);
foreach ($data_attributes as $data_attribute) {
$data_attribute = explode('|', $data_attribute);
// Values are optional for data-* attributes.
$variables['content_attributes'][$data_attribute[0]] = $data_attribute[1] ?? TRUE;
}
}
}
}
/**
* Implements hook_contextual_links_alter().
*/
function layout_builder_component_attributes_contextual_links_alter(array &$links, $group, array $route_parameters) {
if ($group == 'layout_builder_block') {
// Link weights are not respected, so a glorified array splice is used
// instead. The 'Manage attributes' link should be inserted immediately
// after the 'Configure' link.
$insert_array['layout_builder_block_attributes'] = $links['layout_builder_block_attributes'];
unset($links['layout_builder_block_attributes']);
$array_keys = array_flip(array_keys($links));
$update_pos = $array_keys['layout_builder_block_update'];
$links = layout_builder_component_attributes_array_splice_assoc($links, $insert_array, $update_pos + 1);
}
}
/**
* Insert an associative array into a specific position in an array.
*
* See https://jboullion.com/array-splice-associative/.
*
* @param array $original
* The original array.
* @param array $new
* The new array of values to insert into the original array.
* @param int $offset
* The position in the array ( 0 index ) where the new array should
* be inserted.
*
* @return array
* The new combined array
*/
function layout_builder_component_attributes_array_splice_assoc(array $original, array $new, int $offset) {
return array_slice($original, 0, $offset, TRUE) + $new + array_slice($original, $offset, NULL, TRUE);
}
Functions
Name | Description |
---|---|
layout_builder_component_attributes_array_splice_assoc | Insert an associative array into a specific position in an array. |
layout_builder_component_attributes_contextual_links_alter | Implements hook_contextual_links_alter(). |
layout_builder_component_attributes_preprocess_block | Implements template_preprocess_block(). |