You are here

function responsive_menu_page_build in Responsive and off-canvas menu 7.3

Same name and namespace in other branches
  1. 7 responsive_menu.module \responsive_menu_page_build()
  2. 7.2 responsive_menu.module \responsive_menu_page_build()

Implements hook_page_build().

File

./responsive_menu.module, line 248

Code

function responsive_menu_page_build(&$page) {
  if (path_is_admin(current_path())) {
    return;
  }
  if (!function_exists('libraries_load')) {
    return;
  }

  // Load mmenu library and check if polfills need to be included for IE11 support.
  $library_status = variable_get('responsive_menu_use_polyfills', FALSE) ? libraries_load('mmenu', 'with_polyfills') : libraries_load('mmenu');

  // Check whether the library was loaded before trying to render the HTML.
  // This should avoid issues where the unstyled menu is shown at the
  // bottom of the page if the library has not been installed.
  if (!$library_status['loaded']) {
    watchdog('responsive_menu', 'The mmenu library could not be loaded. Please check you have installed it correctly by following the instructions in the README.md within the module directory.', NULL, WATCHDOG_ERROR);
    return;
  }
  module_load_include('inc', 'responsive_menu', 'includes/responsive_menu.menu');
  $off_canvas_menus = variable_get('responsive_menu_off_canvas_menus', 'main-menu');

  // Allow other modules to modify this configuration.
  drupal_alter('responsive_menu_off_canvas_menu_names', $off_canvas_menus);
  $menus = explode(',', $off_canvas_menus);
  $menu_tree = array();
  foreach ($menus as $menu_name) {
    $tree = responsive_menu_tree_block_data(trim($menu_name));
    $menu_tree = array_merge($menu_tree, $tree);
  }

  // Allow this merged menu tree to be altered.
  drupal_alter('responsive_menu_off_canvas_tree', $menu_tree);

  // Load the superfish library.
  if (variable_get('responsive_menu_superfish', TRUE) && ($library = drupal_get_library('system', 'jquery')) && version_compare($library['version'], 1.7, '>=')) {

    // Determine whether to load hoverintent or not.
    // The default is to include it with superfish.
    variable_get('responsive_menu_superfish_hoverintent', TRUE) ? libraries_load('superfish') : libraries_load('superfish', 'no_hoverintent');
  }

  // Add the variables from the settings page for the javascript to use.
  $page['page_bottom']['#attached']['js'][] = array(
    'data' => array(
      'responsive_menu' => array(
        'position' => variable_get('responsive_menu_position', 'left'),
        'theme' => variable_get('responsive_menu_theme', 'theme-dark'),
        'breakpoint' => variable_get('responsive_menu_breakpoint', FALSE),
        'superfish' => array(
          'delay' => variable_get('responsive_menu_superfish_delay', 300),
          'speed' => variable_get('responsive_menu_superfish_speed', 100),
          'speedOut' => variable_get('responsive_menu_superfish_speed_out', 100),
        ),
      ),
    ),
    'type' => 'setting',
  );
  $page['page_bottom']['#attached']['js'][] = array(
    'data' => drupal_get_path('module', 'responsive_menu') . '/js/responsive_menu.config.js',
    'type' => 'file',
    'scope' => 'footer',
    'group' => JS_THEME,
    'weight' => 20,
  );

  // Add the module's css file if the user does not want to disable it.
  if (variable_get('responsive_menu_css', TRUE)) {
    $page['page_bottom']['#attached']['css'][drupal_get_path('module', 'responsive_menu') . '/css/responsive_menu.css'] = array();
  }

  // Choose the first menu as the menu name.
  $menu_name = reset($menus);

  // Generate the output.
  $output = responsive_menu_tree_output($menu_tree, $menu_name);
  $output['#id'] = 'off-canvas-menu';

  // Add the output to the page_bottom region.
  $page['page_bottom']['off_canvas'] = array(
    '#prefix' => '<div class="off-canvas-wrapper"><div id="off-canvas">',
    '#suffix' => '</div></div>',
    '#markup' => drupal_render($output),
  );
}