You are here

function vms_get_current_mlid in Views Menu Support 8

Same name and namespace in other branches
  1. 7 vms.module \vms_get_current_mlid()

Helper function to get active menu item IDs in three different ways.

Parameters

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 value

array An array with the requested mlids.

1 call to vms_get_current_mlid()
vms_handler_filter_mlid::query in handlers/vms_handler_filter_mlid.inc
Add the filtering to the query.

File

./vms.module, line 48
Main function for Views menu support, mainly reading the current menu trail.

Code

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];
}