You are here

vms.module in Views Menu Support 7

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

Main function for Views menu support, mainly reading the current menu trail.

File

vms.module
View source
<?php

/**
 * @file
 * Main function for Views menu support, mainly reading the current menu trail.
 */

// Special responses to mlid queries.
define('VMS_FRONT_PAGE', -1);
define('VMS_MLID_MISSING', -2);
define('VMS_BAD_CONFIG', -3);

/**
 * Array describing different mlid queries.
 */
function vms_mlid_query_types() {
  return array(
    '***VMS_CURRENT***' => t('Currently active menu item'),
    '***VMS_TRAIL***' => t('Current trail of menu items'),
    '***VMS_EXCLUDE_CURRENT***' => t('Trail, excluding current page'),
  );
}

/**
 * Advertise the current views api version.
 */
function vms_views_api() {
  return array(
    'api' => '3',
    'path' => drupal_get_path('module', 'vms'),
  );
}

/**
 * Helper function to get active menu item IDs in three different ways.
 *
 * @param string $trail
 *   Sets how the menu trail should be included (or not). The recognized values
 *   are the same as those defined by vms_mlid_query_type():
 *     * '***VMS_CURRENT***' returns the current mlid, period.
 *     * '***VMS_TRAIL***' returns the trail of mlids to the top of the menu
 *       tree, ending with the front page.
 *     * '***VMS_EXCLUDE_CURRENT***' works as the '***VMS_TRAIL***' option, but
 *       excludes the currently active mlid from the list.
 *
 * @return array
 *   An array with the requested mlids.
 */
function vms_get_current_mlid($trail = '***VMS_CURRENT***') {

  // This function might be called a large number of times on a page request, so
  // it makes sense to have a static cache. Using drupal_static allows other
  // modules to modify the active trail, if need be.
  $vms_active_trail =& drupal_static('vms_active_trail', array());

  // Some bonus stuff to recognise fake menu items set by Rules Bonus Pack and
  // Menu Position in combination.
  // @TODO: Make this a hook that any module can implement.
  if (module_exists('rb_misc')) {

    // If RB has set any faked active menu items, retrieve them and make the
    // processing necessary to get the response expected by Views Menu Support.
    if (is_array(drupal_static('rb_misc_mlid_trail'))) {
      $vms_active_trail['***VMS_TRAIL***'] = array_merge(array(
        VMS_FRONT_PAGE,
      ), drupal_static('rb_misc_mlid_trail'));
      $vms_active_trail['***VMS_EXCLUDE_CURRENT***'] = $vms_active_trail['***VMS_TRAIL***'];
      $vms_active_trail['***VMS_CURRENT***'] = array(
        array_pop($vms_active_trail['***VMS_EXCLUDE_CURRENT***']),
      );
    }
  }

  // If the trail is already found, return and quit.
  if (isset($vms_active_trail[$trail])) {
    return $vms_active_trail[$trail];
  }

  // If the trail type is undefined, return a 'bad config' response.
  if (!array_key_exists($trail, vms_mlid_query_types())) {
    $vms_active_trail[$trail] = VMS_BAD_CONFIG;
    return $vms_active_trail[$trail];
  }

  // Insert the front page at the top of the menu trail.
  $vms_active_trail['***VMS_TRAIL***'] = array(
    VMS_FRONT_PAGE,
  );

  // Get the current active menu link trail. Yes, the *set* function is also
  // used to get the active trail. The phenomenon is called 'DX'.
  $menu_trail = menu_set_active_trail();
  foreach ($menu_trail as &$menu_item) {
    if (isset($menu_item['mlid'])) {
      $vms_active_trail['***VMS_TRAIL***'][] = $menu_item['mlid'];
    }
  }

  // Remove the final $menu_item variable, left by the foreach-by-reference.
  unset($menu_item);

  // Knock off the current mlid from the EXCLUDE_CURRENT response, and store it
  // in the CURRENT response.
  $vms_active_trail['***VMS_EXCLUDE_CURRENT***'] = $vms_active_trail['***VMS_TRAIL***'];
  if (count($vms_active_trail['***VMS_EXCLUDE_CURRENT***'])) {
    $vms_active_trail['***VMS_CURRENT***'] = array(
      array_pop($vms_active_trail['***VMS_EXCLUDE_CURRENT***']),
    );
  }

  // Get if any of the responses are empty, replace them with the appropriate
  // response.
  foreach (vms_mlid_query_types() as $key => $name) {
    if (!count($vms_active_trail[$key])) {
      $vms_active_trail[$key] = array(
        VMS_MLID_MISSING,
      );
    }
  }

  // Return the required trail type.
  return $vms_active_trail[$trail];
}

Functions

Namesort descending Description
vms_get_current_mlid Helper function to get active menu item IDs in three different ways.
vms_mlid_query_types Array describing different mlid queries.
vms_views_api Advertise the current views api version.

Constants