You are here

ultimenu.module in Ultimenu 8

Same filename and directory in other branches
  1. 8.2 ultimenu.module
  2. 7 ultimenu.module

Build Ultimenu blocks based on menu, and their regions on enabled menu items.

File

ultimenu.module
View source
<?php

/**
 * @file
 * Build Ultimenu blocks based on menu, and their regions on enabled menu items.
 */
use Drupal\Core\Extension\Extension;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function ultimenu_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.ultimenu':
      return check_markup(file_get_contents(dirname(__FILE__) . '/README.html'), 'restricted_html');
  }
}

/**
 * Implements hook_library_info_build().
 */
function ultimenu_library_info_build() {
  $libraries = [];
  $ultimenu = \Drupal::service('ultimenu.manager');
  foreach ($ultimenu
    ->getSkins() as $key => $skin) {
    $skin_css_path = $ultimenu
      ->getSkinPath($key);
    $skin_basename = $ultimenu
      ->getSkinName($skin_css_path);
    $libraries['ultimenu.skin.' . $skin_basename] = [
      'dependencies' => [
        'ultimenu/ultimenu',
      ],
      'css' => [
        'theme' => [
          $skin_css_path => [],
        ],
      ],
    ];
  }
  return $libraries;
}

/**
 * Implements hook_system_info_alter().
 */
function ultimenu_system_info_alter(&$info, Extension $file, $type) {
  $ultimenu = \Drupal::service('ultimenu.manager');
  $theme_default = $ultimenu
    ->getConfig('system.theme')
    ->get('default');
  if ($type == 'theme' && $file
    ->getName() == $theme_default && isset($info['regions'])) {
    if ($regions = $ultimenu
      ->getEnabledRegions()) {

      // Append the Ultimenu regions into the theme defined regions.
      foreach ($regions as $key => $region) {
        $info['regions'] += [
          $key => $region,
        ];
      }

      // Remove unwanted Ultimenu regions from theme .info if so configured.
      if (($remove_regions = $ultimenu
        ->removeRegions()) !== FALSE) {
        foreach ($remove_regions as $key => $region) {
          unset($info['regions'][$key]);
        }
      }
    }
  }
}

/**
 * Implements hook_theme().
 */
function ultimenu_theme($existing, $type, $theme, $path) {
  return [
    'ultimenu' => [
      'render element' => 'element',
      'file' => 'ultimenu.theme.inc',
    ],
  ];
}

/**
 * Implements hook_themes_uninstalled().
 */
function ultimenu_themes_uninstalled($theme_list) {
  \Drupal::service('ultimenu.manager')
    ->clearCachedDefinitions(TRUE);
}

/**
 * Implements hook_page_bottom().
 */
function ultimenu_page_bottom(array &$page_bottom) {

  // Add a dedicated button toggle for main menu to the page_bottom region.
  $theme = \Drupal::theme()
    ->getActiveTheme()
    ->getName();
  $theme_default = \Drupal::config('system.theme')
    ->get('default');
  if ($theme_default == $theme) {
    $label = t('Menu');
    $button = '<button data-ultimenu-button="#ultimenu-main" class="button button--ultimenu"
      aria-label="' . $label . '" value="' . $label . '"><span class="bars">' . $label . '</span></button>';
    $page_bottom['ultimenu_button']['#markup'] = $button;
    $page_bottom['ultimenu_button']['#allowed_tags'] = [
      'button',
      'span',
    ];
  }
}

/**
 * Implements hook_preprocess_region().
 */
function ultimenu_preprocess_region(&$variables) {
  if (strpos($variables['region'], 'ultimenu_') !== FALSE) {
    $variables['attributes']['class'][] = 'ultimenu__region';
  }
}