You are here

cami.module in Custom Active Menu Item 7

Same filename and directory in other branches
  1. 8 cami.module

Allows to specify which menu item is active and the classes that will be added to it.

File

cami.module
View source
<?php

/**
 * @file
 * Allows to specify which menu item is active and the classes that will be
 * added to it.
 */

/**
 * Implements hook_menu().
 */
function cami_menu() {
  $items['admin/config/user-interface/custom-active-menu-item'] = array(
    'title' => 'Custom Active Menu Item',
    'description' => 'Configure Custom Active Menu Item',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'cami_settings_form',
    ),
    'access arguments' => array(
      'administer cami',
    ),
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function cami_permission() {
  return array(
    'administer cami' => array(
      'title' => t('Administer Custom Active Menu Item'),
      'description' => t('Allows to users edit the menu item to indicate when is active.'),
    ),
  );
}

/**
 * Settings form configuration.
 */
function cami_settings_form($form_state) {
  $form = array();
  $form['cami_li_classes'] = array(
    '#type' => 'textfield',
    '#title' => t('List classes'),
    '#description' => t('Classes applied to <strong>li</strong> element (separate classes with spaces)'),
    '#default_value' => variable_get('cami_li_classes', 'active-trail'),
  );
  $form['cami_a_classes'] = array(
    '#type' => 'textfield',
    '#title' => t('Link classes'),
    '#description' => t('Classes applied to <strong>a</strong> element (separate classes with spaces)'),
    '#default_value' => variable_get('cami_a_classes', 'active'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_menu_edit_item_alter().
 */
function cami_form_menu_edit_item_alter(&$form, &$form_state) {
  if (user_access('administer cami')) {
    $form['options'] = array(
      '#tree' => TRUE,
      '#weight' => 50,
    );
    $form['options']['cami'] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Custom Active Menu Item'),
      '#weight' => 0,
    );
    $form['options']['cami']['pages'] = array(
      '#type' => 'textarea',
      '#title' => t('Pages'),
      '#description' => t("Pages where the menu item will be active, Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog."),
      '#default_value' => isset($form['original_item']['#value']['options']['cami']['pages']) ? $form['original_item']['#value']['options']['cami']['pages'] : '',
    );
  }
}

/**
 * Implements hook_preprocess_menu_link().
 */
function cami_preprocess_menu_link(&$variables) {
  if (isset($variables['element']['#localized_options']['cami']['pages'])) {
    $pages = $variables['element']['#localized_options']['cami']['pages'];
    $path = drupal_get_path_alias(current_path());

    // Compare with the internal and path alias (if any).
    $page_match = drupal_match_path($path, $pages);
    if ($path != current_path()) {
      $page_match = $page_match || drupal_match_path(current_path(), $pages);
    }
  }
  if (isset($page_match) && $page_match) {
    $variables['element']['#attributes']['class'][] = variable_get('cami_li_classes', 'active-trail');
    $variables['element']['#localized_options']['attributes']['class'][] = variable_get('cami_a_classes', 'active');
  }
}

Functions

Namesort descending Description
cami_form_menu_edit_item_alter Implements hook_menu_edit_item_alter().
cami_menu Implements hook_menu().
cami_permission Implements hook_permission().
cami_preprocess_menu_link Implements hook_preprocess_menu_link().
cami_settings_form Settings form configuration.