You are here

function cheeseburger_menu_page_attachments in Cheeseburger Menu 5.0.x

Same name and namespace in other branches
  1. 8.4 cheeseburger_menu.module \cheeseburger_menu_page_attachments()
  2. 8 cheeseburger_menu.module \cheeseburger_menu_page_attachments()
  3. 8.2 cheeseburger_menu.module \cheeseburger_menu_page_attachments()
  4. 8.3 cheeseburger_menu.module \cheeseburger_menu_page_attachments()

Implements hook_page_attachments().

File

./cheeseburger_menu.module, line 62
Contains cheeseburger_menu.module.

Code

function cheeseburger_menu_page_attachments(array &$attachments) {
  $active_theme = \Drupal::service('theme.manager')
    ->getActiveTheme();
  $grouped_triggers = cheeseburger_get_trigger_ids_grouped_by_block_to_trigger($active_theme
    ->getName());

  /** @var \Drupal\block\Entity\Block[] $block */
  $blocks = \Drupal::entityTypeManager()
    ->getStorage('block')
    ->loadByProperties([
    'plugin' => 'cheeseburger_menu',
    'theme' => $active_theme
      ->getName(),
  ]);
  foreach ($blocks as $block) {

    /** @var \Drupal\cheeseburger_menu\Plugin\Block\CheeseburgerMenuBlock $block_plugin */
    $block_plugin = $block
      ->getPlugin();
    $block_ids = [
      $block
        ->id(),
    ];
    $block_ids = array_merge($block_ids, $grouped_triggers['block_ids'][$block
      ->id()] ?? []);
    $html_ids = array_map(function ($block_id) {
      return '#block-' . str_replace('_', '-', $block_id);
    }, $block_ids);
    if ($active_theme
      ->getName() === 'glisseo') {
      $html_ids = array_merge($block_ids, array_map(function ($block_id) {
        return '.block--' . str_replace('_', '-', $block_id);
      }, $block_ids));
    }
    $colors = implode(', ', $html_ids) . ' {';
    $colors .= '--cheese-aside-bg-color: ' . cheeseburger_hex_to_rgba($block_plugin
      ->getConfigValue('left_side_background_color'), $block_plugin
      ->getConfigValue('left_side_background_opacity')) . ';';
    $colors .= '--cheese-aside-text-color: ' . cheeseburger_hex_to_rgba($block_plugin
      ->getConfigValue('left_side_text_color'), $block_plugin
      ->getConfigValue('left_side_text_opacity')) . ';';
    $colors .= '--cheese-main-bg-color: ' . cheeseburger_hex_to_rgba($block_plugin
      ->getConfigValue('right_side_background_color'), $block_plugin
      ->getConfigValue('right_side_background_opacity')) . ';';
    $colors .= '--cheese-main-text-color: ' . cheeseburger_hex_to_rgba($block_plugin
      ->getConfigValue('right_side_text_color'), $block_plugin
      ->getConfigValue('right_side_text_opacity')) . ';';
    $colors .= '--cheese-trigger-color: ' . cheeseburger_hex_to_rgba($block_plugin
      ->getConfigValue('trigger_color'), $block_plugin
      ->getConfigValue('trigger_opacity')) . ';';
    $colors .= '--cheese-trigger-bg-color: ' . cheeseburger_hex_to_rgba($block_plugin
      ->getConfigValue('trigger_background_color'), $block_plugin
      ->getConfigValue('trigger_background_opacity')) . ';';
    $colors .= '--cheese-scrollbar-color: ' . cheeseburger_hex_to_rgba($block_plugin
      ->getConfigValue('scrollbar_color'), $block_plugin
      ->getConfigValue('scrollbar_opacity')) . ';';
    $colors .= '}';
    $attachments['#attached']['html_head'][] = [
      [
        '#tag' => 'style',
        '#value' => $colors,
      ],
      'cheeseburger-colors-' . $block
        ->id(),
    ];
  }
  foreach ($grouped_triggers['media_queries'] as $block_id => $media_query) {
    $attachments['#attached']['html_head'][] = [
      [
        '#tag' => 'style',
        '#value' => '@media ' . $media_query . ' {#block-' . str_replace('_', '-', $block_id) . '{ display:none; }}',
      ],
      'cheeseburger-media-query-' . $block
        ->id(),
    ];
  }
}