You are here

function _taxonomy_menu_trails_process in Taxonomy Menu Trails 7.2

Same name and namespace in other branches
  1. 6 taxonomy_menu_trails.inc \_taxonomy_menu_trails_process()
  2. 7 taxonomy_menu_trails.inc \_taxonomy_menu_trails_process()
  3. 6.x taxonomy_menu_trails.inc \_taxonomy_menu_trails_process()

Set active trail to entitie's term selected by method specified in settings.

Parameters

object $entity:

array $settings:

1 call to _taxonomy_menu_trails_process()
taxonomy_menu_trails_init in ./taxonomy_menu_trails.module
Implements hook_init().

File

./taxonomy_menu_trails.inc, line 143
Processing functions for taxonomy_menu_trails.

Code

function _taxonomy_menu_trails_process($entity, $settings) {
  $instances = $settings['instances'];
  $tids = array();

  //TODO figure out: maybe we have to choose language in term reference fields
  foreach ($instances as $field) {
    if (!empty($entity->{$field})) {
      foreach ($entity->{$field} as $lang => $items) {
        foreach ($items as $item) {
          $tids[] = isset($item['tid']) ? $item['tid'] : $item['target_id'];
        }
      }
    }
  }

  // Filter out terms with different language if this option is enabled.
  if (module_exists('i18n_taxonomy') && $settings['terms_with_current_language']) {
    $terms = taxonomy_term_load_multiple($tids);
    $allowed_languages = array(
      'und',
      $GLOBALS['language_content']->language,
    );
    foreach ($tids as $index => $tid) {
      if (empty($terms[$tid]) || !in_array($terms[$tid]->language, $allowed_languages)) {
        unset($tids[$index]);
      }
    }

    // Exit in case no valid terms left.
    if (empty($tids)) {
      return;
    }
  }
  if ($settings['selection_method'] == 'last') {
    $tids = array_reverse($tids);
  }

  // Get paths for each tid.
  $paths = _taxonomy_menu_trails_get_paths($tids, $entity, $settings);

  // Retrieve the list of menu names sorted by preference. This list is used
  // by menu system to get order of menus and not as a filter, we do the same.
  // See menu_link_get_preferred().
  $menu_names = menu_get_active_menu_names();
  $query = db_select('menu_links', 'ml', array(
    'fetch' => PDO::FETCH_ASSOC,
  ));
  $query
    ->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  $query
    ->fields('ml');

  // Weight must be taken from {menu_links}, not {menu_router}.
  $query
    ->addField('ml', 'weight', 'link_weight');
  $query
    ->fields('m');
  $query
    ->condition('ml.link_path', $paths, 'IN');
  $query
    ->condition('ml.hidden', '0');
  if ($settings['selection_method'] == 'deepest-in-menu') {
    $query
      ->orderBy('ml.depth', 'DESC');
  }
  $query
    ->orderBy('ml.weight', 'ASC');
  $results = $query
    ->execute();
  $selected_items = array();
  $vars = compact('menu_names', 'results', 'paths', 'selected_items');
  $vars['trail_per_menu'] = $settings['trail_per_menu'];
  switch ($settings['selection_method']) {
    case 'first':
    case 'last':
      $selected_items = _taxonomy_menu_trails_fetch_link_simple($vars);
      break;
    case 'deepest-in-menu':
      $selected_items = _taxonomy_menu_trails_fetch_link_deepest($vars);
      break;
  }
  if (empty($selected_items)) {
    return;
  }

  // Set selected menu item as an active path for its menu.
  // Function menu_tree_set_path() available since Drupal 7.9, so we should
  // check it exists.
  if (function_exists('menu_tree_set_path')) {
    foreach ($selected_items as $selected_item) {
      menu_tree_set_path($selected_item['menu_name'], $selected_item['link_path']);
    }
  }

  // Save selected item and "set_breadcrumb" setting for
  // taxonomy_menu_trails_menu_breadcrumb_alter().
  if (!empty($settings['set_breadcrumb'])) {
    $selected_item = reset($selected_items);
    $selected_item['set_breadcrumb'] = $settings['set_breadcrumb'];
    taxonomy_menu_trails_selected_item_for_breadcrumb($selected_item);
  }

  // TODO: Remove workaround when Menu Block will remove its
  // _menu_link_get_preferred(). But it's safe to leave it here even after
  // Menu Block update to keep compatibility with old Menu Block releases.
  // So, probably it should be here forever or until the next major release.
  if (module_exists('menu_block')) {

    // Also set preferred item for Menu Block module
    $mb_preferred_links =& drupal_static('_menu_link_get_preferred');
    foreach ($selected_items as $selected_item) {
      $mb_preferred_links[$_GET['q']][$selected_item['menu_name']] = $selected_item;
    }
  }
}