You are here

function power_menu_get_fields_entity in Power Menu 7.2

Gets the Power Menu Fileds entity for given path

Parameters

$path: The system path to search for the fields entity. Is no path set, the system path is used. To get the default element use '/' as value for $path.

$fallback: Is set to TRUE, the default entity is returned when no entity is retrived from defined path.

string $menu_name: The menu name to retrive the default menu field entity from

Return value

The entity or NULL is no entity found for the given path

1 call to power_menu_get_fields_entity()
power_menu_block_view in ./power_menu.module
Implements hook_block_view().

File

./power_menu.module, line 778

Code

function power_menu_get_fields_entity($path = NULL, $fallback = TRUE, $menu_name = 'main-menu') {
  $menu_name_orig = $menu_name;
  $menu_name = power_menu_create_machine_name($menu_name);

  // Is no path set, use the current sytsem path
  if (!isset($path)) {
    $path = implode('/', arg());
  }

  // The cache is used to store the resolved menu item path from power menu for given system path.
  // With this mechanism it is not necessary to store the field entity for every system path
  $cache_path_key = 'path:' . $path;
  $cached_path = cache_get($cache_path_key, 'cache_power_menu');
  $lookup_path = $cached_path ? $cached_path->data : $path;

  // Loaded menu field entities also stored in cache. Get it.
  $cache_key = 'fields:' . $lookup_path;
  $entity = cache_get($cache_key, 'cache_power_menu');
  $entity = $entity ? $entity->data : NULL;

  // Is the entity not cached, load it
  if (!$entity) {

    // Lookup for the default menu field entity?
    if ($lookup_path == '/' && $fallback) {
      $id = power_menu_get_fields_entity_id_by_menu($menu_name);
    }
    else {

      // Is the defined path the front page, get <front> as search condition
      if (variable_get('site_frontpage', 'node') == $lookup_path) {
        $lookup_path = '<front>';
      }

      // Get the menu link from given path
      $query = db_select('power_menu_fields', 'pmf');
      $query
        ->leftJoin('menu_links', 'ml', 'pmf.mlid = ml.mlid');
      $id = $query
        ->fields('pmf', array(
        'id',
      ))
        ->condition('ml.link_path', $lookup_path)
        ->condition('ml.menu_name', $menu_name_orig)
        ->execute()
        ->fetchField();
    }

    // No element found, lookup for the default entry
    if (!$id && $fallback && $lookup_path != '/') {
      $entity = power_menu_get_fields_entity('/', TRUE, $menu_name);
      $lookup_path = '/';
    }
    elseif ($id) {
      $entity = entity_load('power_menu_fields', array(
        $id,
      ));

      // Return only the entity and not an array
      $entity = $entity ? array_pop($entity) : NULL;
    }
    cache_set($cache_path_key, $lookup_path, 'cache_power_menu');
    cache_set('fields:' . $lookup_path, $entity, 'cache_power_menu');
  }
  return $entity;
}