layout_builder_component_attributes-1.1.0/layout_builder_component_attributes.module

layout_builder_component_attributes.module
<?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'];

    $attribute_names = [
      'attributes',
      'title_attributes',
      'content_attributes',
    ];

    foreach ($attribute_names as $attribute_name) {
      if ($config['allowed_block_' . $attribute_name]['id'] && $component_attributes['block_' . $attribute_name]['id']) {
        $variables[$attribute_name]['id'] = $component_attributes['block_' . $attribute_name]['id'];
      }
      if ($config['allowed_block_' . $attribute_name]['class'] && $component_attributes['block_' . $attribute_name]['class']) {
        $classes = explode(' ', $component_attributes['block_' . $attribute_name]['class']);
        $existing_classes = $variables[$attribute_name]['class'] ?? [];
        $variables[$attribute_name]['class'] = array_merge($existing_classes, $classes);
      }
      if ($config['allowed_block_' . $attribute_name]['style'] && $component_attributes['block_' . $attribute_name]['style']) {
        $variables[$attribute_name]['style'] = $component_attributes['block_' . $attribute_name]['style'];
      }
      if ($config['allowed_block_' . $attribute_name]['data'] && $component_attributes['block_' . $attribute_name]['data']) {
        $data_attributes = preg_split('/\R/', $component_attributes['block_' . $attribute_name]['data']);
        foreach ($data_attributes as $data_attribute) {
          $data_attribute = explode('|', $data_attribute);
          // Values are optional for data-* attributes.
          $variables[$attribute_name][$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' && isset($links['layout_builder_block_attributes'])) {
    // 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);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc