You are here

responsive_menu.admin.inc in Responsive and off-canvas menu 7

The settings form for the responsive menu module.

File

includes/responsive_menu.admin.inc
View source
<?php

/**
 * @file
 * The settings form for the responsive menu module.
 */
function responsive_menu_settings_form($form) {
  $form['responsive_menu'] = array(
    '#type' => 'fieldset',
    '#title' => t('Responsive menu'),
  );
  $form['responsive_menu']['responsive_menu_menu'] = array(
    '#type' => 'select',
    '#title' => t('Choose which Drupal menu will be rendered as a horizontal menu at the breakpoint width'),
    '#default_value' => variable_get('responsive_menu_menu', 'main-menu'),
    '#options' => menu_get_menus(),
  );
  $form['responsive_menu']['off_canvas'] = array(
    '#type' => 'fieldset',
    '#title' => t('Off canvas'),
  );
  $form['responsive_menu']['off_canvas']['responsive_menu_off_canvas_menus'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter the name(s) of Drupal menus to be rendered in an off-canvas menu'),
    '#description' => t('Enter the names of menus in a comma delimited format. If more than one menu is entered the menu items will be merged together. This is useful if you have a main menu and a utility menu that display separately at wider screen sizes but should be merged into a single menu at smaller screen sizes. Note that the menus will be merged in the entered order.'),
    '#default_value' => variable_get('responsive_menu_off_canvas_menus', 'main-menu'),
  );
  $form['responsive_menu']['responsive_menu_element'] = array(
    '#type' => 'select',
    '#title' => t('Choose the HTML element to wrap the menu block in'),
    '#default_value' => variable_get('responsive_menu_element', 'nav'),
    '#options' => array(
      'nav' => 'nav',
      'div' => 'div',
    ),
  );

  // Add breakpoint module support
  if (module_exists('breakpoints')) {
    $breakpoints = breakpoints_breakpoint_load_all();
    $queries = array();
    foreach ($breakpoints as $breakpoint) {
      $queries[$breakpoint->machine_name] = $breakpoint->name;
    }
    $form['responsive_menu']['responsive_menu_breakpoint'] = array(
      '#type' => 'select',
      '#title' => t('Choose a breakpoint to trigger the desktop format menu at'),
      '#default_value' => variable_get('responsive_menu_breakpoint', FALSE),
      '#options' => $queries,
    );
    if (empty($queries)) {
      $form['responsive_menu']['responsive_menu_breakpoint']['#disabled'] = TRUE;
      $form['responsive_menu']['responsive_menu_breakpoint']['#description'] = '<div class="description">' . t('You must configure at least one !breakpoint to see any options. Until then the select widget above is disabled.', array(
        '!breakpoint' => l('breakpoint', 'admin/config/media/breakpoints'),
      )) . '</div>';
    }
  }
  else {

    // Fallback to entering a media query string.
    $form['responsive_menu']['responsive_menu_media_query'] = array(
      '#type' => 'textfield',
      '#title' => t('Enter a media query string for the desktop format menu'),
      '#description' => t('For example: (min-width: 960px)'),
      '#default_value' => variable_get('responsive_menu_media_query', '(min-width: 960px)'),
    );
  }

  // Whether to load the base css.
  $form['responsive_menu']['css'] = array(
    '#type' => 'checkbox',
    '#title' => t("Load the responsive_menu module's css"),
    '#description' => t('It might be that you want to override all of the css that comes with the responsive_menu module in which case you can disable the loading of the css here and include it instead in your theme.'),
    '#default_value' => variable_get('responsive_menu_css', TRUE),
  );

  // Left or right positioned panel.
  $form['responsive_menu']['position'] = array(
    '#type' => 'select',
    '#options' => array(
      'left' => t('Left'),
      'right' => t('Right'),
    ),
    '#title' => t('Which side the mobile menu panel should slide out from'),
    '#default_value' => variable_get('responsive_menu_position', 'left'),
  );

  // The theme of the slideout panel.
  $form['responsive_menu']['theme'] = array(
    '#type' => 'select',
    '#options' => array(
      'theme-light' => t('Light'),
      'theme-dark' => t('Dark'),
      'theme-black' => t('Black'),
      'theme-white' => t('White'),
    ),
    '#title' => t('Which mmenu theme to use'),
    '#default_value' => variable_get('responsive_menu_theme', 'theme-dark'),
  );

  // A javascript enhancements fieldset.
  $form['responsive_menu']['js'] = array(
    '#type' => 'fieldset',
    '#title' => t('Javascript enhancements'),
  );
  $form['responsive_menu']['js']['superfish'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply Superfish to the horizontal menu'),
    '#description' => t('Adds the superfish.js library functionality to the horizontal menu. This enhances the menu with %hoverintent which provides better support for hovering and support for mobiles. Note that hoverIntent is included in the superfish library and does not require an extra download.', array(
      '%hoverintent' => l('hoverIntent', 'http://cherne.net/brian/resources/jquery.hoverIntent.html'),
    )),
    '#default_value' => variable_get('responsive_menu_superfish', TRUE),
  );
  $form['responsive_menu']['js']['superfish_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Superfish options'),
    '#states' => array(
      'visible' => array(
        ':input[name="superfish"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['responsive_menu']['js']['superfish_options']['superfish_delay'] = array(
    '#type' => 'textfield',
    '#title' => t('Delay'),
    '#description' => t('The amount of time in milliseconds a menu will remain after the mouse leaves it.'),
    '#default_value' => variable_get('responsive_menu_superfish_delay', '300'),
  );
  $form['responsive_menu']['js']['superfish_options']['superfish_speed'] = array(
    '#type' => 'textfield',
    '#title' => t('Speed'),
    '#description' => t('The amount of time in milliseconds it takes for a menu to reach 100% opacity when it opens.'),
    '#default_value' => variable_get('responsive_menu_superfish_speed', '100'),
  );
  $form['responsive_menu']['js']['superfish_options']['superfish_speed_out'] = array(
    '#type' => 'textfield',
    '#title' => t('Speed out'),
    '#description' => t('The amount of time in milliseconds it takes for a menu to reach 0% opacity when it closes.'),
    '#default_value' => variable_get('responsive_menu_superfish_speed_out', '100'),
  );

  // Whether the optional superfish library is to be used.
  if (!function_exists('libraries_get_path') || !($supperfish_path = libraries_get_path('superfish'))) {
    $form['responsive_menu']['js']['superfish']['#disabled'] = TRUE;
    $form['responsive_menu']['js']['superfish']['#description'] .= '<br/><span class="warning">' . t('You must have the !libraries module installed and also have downloaded the !superfish library and placed it in the libraries directory. Until then the superfish option is disabled.', array(
      '!libraries' => l('libraries', 'http://drupal.org/project/libraries'),
      '!superfish' => l('superfish', 'https://github.com/joeldbirch/superfish/archive/master.zip'),
    )) . '</span>';
  }

  // The hammer js library is optional.
  $form['responsive_menu']['js']['hammer'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add swipe gestures'),
    '#description' => t('Adds the hammer.js library to enhance the mobile experience with swipe gestures to open or close the menu.'),
    '#default_value' => variable_get('responsive_menu_hammer', FALSE),
  );

  // If the libraries module isn't installed or if the hammer.min.js
  // file isn't in the correct location then disable the hammer option
  // and display an appropriate message.
  if (!function_exists('libraries_get_path') || !($hammer_path = libraries_get_path('hammerjs'))) {
    $form['responsive_menu']['js']['hammer']['#disabled'] = TRUE;
    $form['responsive_menu']['js']['hammer']['#description'] .= '<br/><span class="warning">' . t('You must have the !libraries module installed and also have downloaded the !hammer file and placed it in a hammerjs directory in libraries. Until then the hammerjs option is disabled.', array(
      '!libraries' => l('libraries', 'http://drupal.org/project/libraries'),
      '!hammer' => l('hammer.min.js', 'http://hammerjs.github.io/dist/hammer.min.js'),
    )) . '</span>';
  }
  if (!module_exists('fastclick')) {
    $form['responsive_menu']['js']['fastclick'] = array(
      '#markup' => '<div class="description">' . t('The !fastclick module is highly recommended and will remove the 300ms tap delay on mobile devices.', array(
        '!fastclick' => l('Fastclick', 'https://drupal.org/project/fastclick'),
      )) . '</div>',
    );
  }
  $form['#submit'][] = 'responsive_menu_admin_form_submit';
  return $form;
}

/**
 * Submit handler for the menu settings form.
 */
function responsive_menu_admin_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  variable_set('responsive_menu_menu', $values['responsive_menu_menu']);
  variable_set('responsive_menu_css', $values['css']);
  variable_set('responsive_menu_off_canvas_menus', $values['responsive_menu_off_canvas_menus']);
  variable_set('responsive_menu_position', $values['position']);
  variable_set('responsive_menu_theme', $values['theme']);
  variable_set('responsive_menu_superfish', $values['superfish']);
  variable_set('responsive_menu_superfish_delay', $values['superfish_delay']);
  variable_set('responsive_menu_superfish_speed', $values['superfish_speed']);
  variable_set('responsive_menu_superfish_speed_out', $values['superfish_speed_out']);
  if (module_exists('breakpoints')) {
    $breakpoints = breakpoints_breakpoint_load_all();

    // Check if the breakpoint exists.
    if (isset($breakpoints[$values['responsive_menu_breakpoint']])) {

      // Store the breakpoint for using again in the form.
      variable_set('responsive_menu_breakpoint', $values['responsive_menu_breakpoint']);

      // Also store the actual breakpoint string for use in calling the stylesheet.
      variable_set('responsive_menu_media_query', $breakpoints[$values['responsive_menu_breakpoint']]->breakpoint);
    }
  }
  else {
    variable_set('responsive_menu_media_query', $values['responsive_menu_media_query']);
  }

  // Store the boolean value of the hammer option.
  variable_set('responsive_menu_hammer', $values['hammer']);
}

/**
 * Prune a tree so it does not extend beyond the specified depth limit.
 *
 * @param $tree
 *   array The menu tree to prune.
 * @param $depth_limit
 *   int The maximum depth of the returned tree; must be a positive integer.
 * @return
 *   void
 */
function responsive_menu_tree_depth_trim(&$tree, $depth_limit) {

  // Prevent invalid input from returning a trimmed tree.
  if ($depth_limit < 1) {
    return;
  }

  // Examine each element at this level to find any possible children.
  foreach ($tree as $key => &$value) {
    if ($tree[$key]['below']) {
      if ($depth_limit > 1) {
        responsive_menu_tree_depth_trim($tree[$key]['below'], $depth_limit - 1);
      }
      else {

        // Remove the children items.
        $tree[$key]['below'] = FALSE;
      }
    }
    if ($depth_limit == 1 && $tree[$key]['link']['has_children']) {

      // Turn off the menu styling that shows there were children.
      $tree[$key]['link']['has_children'] = FALSE;
      $tree[$key]['link']['leaf_has_children'] = TRUE;
    }
  }
}

Functions

Namesort descending Description
responsive_menu_admin_form_submit Submit handler for the menu settings form.
responsive_menu_settings_form @file The settings form for the responsive menu module.
responsive_menu_tree_depth_trim Prune a tree so it does not extend beyond the specified depth limit.