You are here

function _taxonomy_menu_trails_process in Taxonomy Menu Trails 7

Same name and namespace in other branches
  1. 6 taxonomy_menu_trails.inc \_taxonomy_menu_trails_process()
  2. 7.2 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 39
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) {
    foreach ($entity->{$field} as $lang => $terms) {
      foreach ($terms as $term) {
        $tids[] = $term['tid'];
      }
    }
  }
  if ($settings['selection_method'] == 'last') {
    $tids = array_reverse($tids);
  }
  $vids = array();
  if ($settings['tm_integration']) {

    // Unfortunately, there is no way to get tids-vids map without DB query
    // If you know one, please create issue and describe method
    $vids = db_select('taxonomy_term_data', 'ttd')
      ->fields('ttd', array(
      'tid',
      'vid',
    ))
      ->condition('ttd.tid', $tids, 'IN')
      ->execute()
      ->fetchAllKeyed();
  }
  $selected_item = FALSE;
  $paths = array();
  foreach ($tids as $tid) {
    $path = _taxonomy_menu_trails_get_path($tid, $settings, $vids);
    $paths[] = $path;
  }

  // Retrieve a list of menu names, ordered by preference.
  $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.menu_name', $menu_names, 'IN');
  $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();
  $vars = compact('menu_names', 'results', 'paths', 'selected_item');
  switch ($settings['selection_method']) {
    case 'first':
    case 'last':
      $selected_item = _taxonomy_menu_trails_fetch_link_simple($vars);
      break;
    case 'deepest-in-menu':
      $selected_item = _taxonomy_menu_trails_fetch_link_deepest($vars);
      break;
  }
  if (!$selected_item) {
    return;
  }

  // Keep title from original menu item, so we'll have original page title
  // instead of term menu item title. This happens if nobody calls
  // drupal_set_title() and Drupal gets title from last active trail menu item.
  $original_item = menu_get_item();
  drupal_set_title($original_item['title']);

  // Set selected menu item as a preferred for core function
  $preferred_links =& drupal_static('menu_link_get_preferred');
  $preferred_links[$_GET['q']] = $selected_item;

  //TODO remove workaround when core bug #942782 will be fixed and Menu Block

  //remove its workarounds.
  if (module_exists('menu_block')) {

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