You are here

function features_menu_link_load in Features 7.2

Same name and namespace in other branches
  1. 6 includes/features.menu.inc \features_menu_link_load()
  2. 7 includes/features.menu.inc \features_menu_link_load()

Loads a menu link by its (features-specific) identifier.

@todo Describe behavior for old vs new identifier format. See #3075693.

Parameters

string $identifier: Format: One of:

  • "{$menu_name}_{$clean_title}:{$link_path}" (new format)
  • "{$menu_name}:{$link_path}" (old format)

Return value

array|false The menu link, or FALSE if not found.

See also

\menu_links_features_identifier()

4 calls to features_menu_link_load()
menu_links_features_export in includes/features.menu.inc
Implements hook_features_export().
menu_links_features_export_render in includes/features.menu.inc
Implements hook_features_export_render().
menu_links_features_identifier in includes/features.menu.inc
Callback for generating the menu link exportable identifier.
menu_links_features_rebuild_ordered in includes/features.menu.inc
Generate a depth tree of all menu links.

File

includes/features.menu.inc, line 423
Features integration for 'menu' module.

Code

function features_menu_link_load($identifier) {
  $menu_name = '';
  $link_path = '';

  // This gets variables for menu_name_cleantitle:link_path format.
  if (strstr($identifier, "_")) {
    $link_path = substr($identifier, strpos($identifier, ":") + 1);
    list($menu_name) = explode('_', $identifier, 2);
    $clean_title = substr($identifier, strpos($identifier, "_") + 1, strpos($identifier, ":") - strpos($identifier, "_") - 1);
  }
  else {
    $clean_title = '';
    list($menu_name, $link_path) = explode(':', $identifier, 2);
  }
  $links = db_select('menu_links')
    ->fields('menu_links', array(
    'menu_name',
    'mlid',
    'plid',
    'link_path',
    'router_path',
    'link_title',
    'options',
    'module',
    'hidden',
    'external',
    'has_children',
    'expanded',
    'weight',
    'customized',
  ))
    ->condition('menu_name', $menu_name)
    ->condition('link_path', $link_path)
    ->addTag('features_menu_link')
    ->execute()
    ->fetchAllAssoc('mlid');
  foreach ($links as $link) {
    $link->options = unserialize($link->options);

    // Title or previous identifier matches.
    if (isset($link->options['identifier']) && strcmp($link->options['identifier'], $identifier) == 0 || isset($clean_title) && strcmp(features_clean_title($link->link_title), $clean_title) == 0) {
      return (array) $link;
    }
  }

  // Only one link with the requested menu_name and link_path does exists,
  // -- providing an upgrade possibility for links saved in a feature before the
  // new identifier-pattern was added.
  if (count($links) == 1 && empty($clean_title)) {

    // Get the first item.
    $link = reset($links);
    return (array) $link;
  }
  elseif (isset($clean_title)) {
    $links = db_select('menu_links')
      ->fields('menu_links', array(
      'menu_name',
      'mlid',
      'plid',
      'link_path',
      'router_path',
      'link_title',
      'options',
      'module',
      'hidden',
      'external',
      'has_children',
      'expanded',
      'weight',
    ))
      ->condition('menu_name', $menu_name)
      ->execute()
      ->fetchAllAssoc('mlid');
    foreach ($links as $link) {
      $link->options = unserialize($link->options);

      // Links with a stored identifier must only be matched on that identifier,
      // to prevent cross over assumptions.
      if (isset($link->options['identifier'])) {
        if (strcmp($link->options['identifier'], $identifier) == 0) {
          return (array) $link;
        }
      }
      elseif (strcmp(features_clean_title($link->link_title), $clean_title) == 0) {
        return (array) $link;
      }
    }
  }
  return FALSE;
}