You are here

function responsive_menu_page_bottom in Responsive and off-canvas menu 4.0.x

Same name and namespace in other branches
  1. 8.3 responsive_menu.module \responsive_menu_page_bottom()
  2. 8.2 responsive_menu.module \responsive_menu_page_bottom()
  3. 4.4.x responsive_menu.module \responsive_menu_page_bottom()
  4. 4.1.x responsive_menu.module \responsive_menu_page_bottom()
  5. 4.3.x responsive_menu.module \responsive_menu_page_bottom()

Implements hook_page_bottom().

Used to place the off-canvas menu and supporting libraries and configuration.

File

./responsive_menu.module, line 112
Contains procedural code.

Code

function responsive_menu_page_bottom(&$page) {

  // Get the configuration.
  $config = \Drupal::config('responsive_menu.settings');

  // A site builder can allow this module to work with the admin theme.
  if ($config
    ->get('allow_admin') == FALSE && _current_theme_is_admin()) {
    return;
  }
  $output = [
    '#prefix' => '<div class="off-canvas-wrapper"><div id="off-canvas">',
    '#suffix' => '</div></div>',
    '#pre_render' => [
      '\\Drupal\\responsive_menu\\OffCanvas::preRender',
    ],
  ];

  // Determine whether the breakpoint code should be used.
  if ($config
    ->get('use_breakpoint')) {

    // Check whether the generated breakpoint css exists and if not create it.
    if (!file_exists(_get_breakpoint_css_filepath() . RESPONSIVE_MENU_BREAKPOINT_FILENAME)) {
      $breakpoint = $config
        ->get('horizontal_media_query');
      responsive_menu_generate_breakpoint_css($breakpoint);
    }

    // Add the dynamically generated library with breakpoint styles.
    $output['#attached']['library'][] = 'responsive_menu/responsive_menu.breakpoint';
  }

  // Add the hammerjs library if the user has requested it.
  $hammerjs_setting = $config
    ->get('hammerjs');
  if ($hammerjs_setting) {
    $output['#attached']['library'][] = 'responsive_menu/responsive_menu.hammerjs';
  }

  // Add the mmenu library.
  $output['#attached']['library'][] = 'responsive_menu/responsive_menu.mmenu';

  // Add this module's configuration javascript.
  $output['#attached']['library'][] = 'responsive_menu/responsive_menu.config';

  // Add the module's css file if the user does not want to disable it.
  if ($config
    ->get('include_css')) {
    $output['#attached']['library'][] = 'responsive_menu/responsive_menu.styling';
  }

  // Add some of the config as javascript settings.
  $output['#attached']['drupalSettings']['responsive_menu'] = [
    'position' => $config
      ->get('off_canvas_position'),
    'theme' => $config
      ->get('off_canvas_theme'),
    'pagedim' => $config
      ->get('pagedim'),
    'breakpoint' => $config
      ->get('horizontal_media_query'),
    'superfish' => [
      'active' => $config
        ->get('horizontal_superfish'),
      'delay' => $config
        ->get('horizontal_superfish_delay'),
      'speed' => $config
        ->get('horizontal_superfish_speed'),
      'speedOut' => $config
        ->get('horizontal_superfish_speed_out'),
    ],
  ];
  $output['#cache']['keys'] = [
    'responsive_menu',
    'off_canvas',
  ];

  // Get the menu names. These are used to build the
  // cache keys so we can cache different variations of the menu.
  $off_canvas_menus = \Drupal::config('responsive_menu.settings')
    ->get('off_canvas_menus');

  // Other modules can modify the menu names so we need to take this into
  // account when setting the cache keys.
  \Drupal::ModuleHandler()
    ->alter('responsive_menu_off_canvas_menu_names', $off_canvas_menus);
  $menus = explode(',', $off_canvas_menus);
  $output['#cache']['keys'] += $menus;
  foreach ($menus as $menu_name) {

    // If any of the menus' config changes the render cache should
    // be invalidated.
    $output['#cache']['tags'][] = 'config:system.menu.' . $menu_name;

    // The menu will also vary depending on the active trail of each merged menu
    // so this will be added as a cache context.
    $output['#cache']['context'][] = 'route.menu_active_trails:' . $menu_name;
  }
  $page['page_bottom']['off_canvas'] = $output;
}