You are here

function responsive_menu_page_bottom in Responsive and off-canvas menu 4.3.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.0.x responsive_menu.module \responsive_menu_page_bottom()
  5. 4.1.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 113
Contains procedural code.

Code

function responsive_menu_page_bottom(&$page) {

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

  // A developer may not want to output the off-canvas menu's HTML into the DOM
  // and activate the mmenu library. We allow a variable to by altered by a
  // custom hook. If the value is FALSE then rendering does not continue and no
  // output is added to page bottom.
  $off_canvas_output = TRUE;
  \Drupal::ModuleHandler()
    ->alter('responsive_menu_off_canvas_output', $off_canvas_output);
  if ($off_canvas_output === FALSE) {
    return;
  }

  // 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 mmenu library.
  $output['#attached']['library'][] = 'responsive_menu/responsive_menu.mmenu';

  // If the polyfills are requested and the file exists then add it too.
  // Note that in the past the final build files have not included the
  // polyfills file (see d.o issues #3143984, #3111949) so we need to
  // check if its actually there to avoid a 404.
  if ($config
    ->get('use_polyfills') && file_exists(DRUPAL_ROOT . '/libraries/mmenu/dist/mmenu.polyfills.js')) {
    $output['#attached']['library'][] = 'responsive_menu/responsive_menu.polyfills';
  }

  // 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 the bootstrap specific code if needed.
  if ($config
    ->get('use_bootstrap')) {
    $output['#attached']['library'][] = 'responsive_menu/responsive_menu.bootstrap';
  }

  // 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'),
    'modifyViewport' => $config
      ->get('modify_viewport'),
    'use_bootstrap' => $config
      ->get('use_bootstrap'),
    'breakpoint' => $config
      ->get('horizontal_media_query'),
    'drag' => $config
      ->get('drag'),
  ];

  // Allow for the menu position to be based on the language direction.
  // @see issue #3095162 for details.
  if ($config
    ->get('off_canvas_position') === 'contextual') {
    $language = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getDirection();
    $output['#attached']['drupalSettings']['responsive_menu']['position'] = $language === 'rtl' ? 'right' : 'left';
  }
  else {
    $output['#attached']['drupalSettings']['responsive_menu']['position'] = $config
      ->get('off_canvas_position');
  }
  $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;
}