You are here

function taxonomy_menu_trails_init in Taxonomy Menu Trails 7

Same name and namespace in other branches
  1. 6 taxonomy_menu_trails.module \taxonomy_menu_trails_init()
  2. 7.2 taxonomy_menu_trails.module \taxonomy_menu_trails_init()
  3. 6.x taxonomy_menu_trails.module \taxonomy_menu_trails_init()

Implements hook_init().

File

./taxonomy_menu_trails.module, line 75
Changes menu trail of current entity to its term's menu item.

Code

function taxonomy_menu_trails_init() {
  switch (arg(0)) {
    case 'node':
      if (is_numeric(arg(1))) {
        $entity_type = 'node';
        $entities = entity_load($entity_type, array(
          arg(1),
        ));
        $entity = reset($entities);
      }
      break;
  }

  // If entity was found, extract bundle name and get entity path from entity
  // API, so it will be real entity path and not its child tab path.
  // We don't need this for entity found with regexp.
  if (!empty($entity)) {
    list(, , $bundle) = entity_extract_ids($entity_type, $entity);
    list($settings) = _taxonomy_menu_trails_get_settings($entity_type, $bundle, FALSE);
    if ($settings['only_without_menu']) {
      $entity_uri = entity_uri($entity_type, $entity);
      $entity_path = $entity_uri['path'];
    }
  }
  else {
    $all_regexps = variable_get('taxonomy_menu_trails__path_regexps', array());
    foreach ($all_regexps as $entity_type => $bundles) {
      foreach ($bundles as $bundle => $placeholders) {
        foreach ($placeholders as $placeholder => $regexps) {
          foreach ($regexps as $regexp) {

            // Run regular expression test. $matches[1] will contain placeholder
            // matched value.
            if (preg_match($regexp, $_GET['q'], $matches)) {
              $placeholder_value = $matches[1];
              list($id_key, $title_key, $bundle_key) = _taxonomy_menu_trails_get_entity_type_keys($entity_type, array(
                'id',
                'label',
                'bundle',
              ));
              $ids = FALSE;
              $conditions = array(
                $bundle_key => $bundle,
              );
              switch ($placeholder) {
                case $id_key:
                  $ids = array(
                    $placeholder_value,
                  );
                  break;
                case $title_key:
                  $conditions[$title_key] = $placeholder_value;
                  break;
              }
              $entities = entity_load($entity_type, $ids, $conditions);
              $entity = reset($entities);
              if (!empty($entity)) {

                // Entity found, set current page as entity path and break all loops.
                list($settings) = _taxonomy_menu_trails_get_settings($entity_type, $bundle, FALSE);
                if ($settings['only_without_menu']) {
                  $entity_path = $_GET['q'];
                }
                break 4;
              }
            }
          }
        }
      }
    }
  }
  if (!empty($entity)) {

    // TODO check for active trail cache existence and process only if there is
    // no cache data for current path and make sure this behavior will work
    // for Menu Block
    if (!empty($settings['instances'])) {
      $pass_own_menu_check = TRUE;
      if ($settings['only_without_menu']) {
        $has_own_menu = db_query('SELECT mlid FROM {menu_links} WHERE link_path = :path AND hidden = 0 LIMIT 1', array(
          ':path' => $entity_path,
        ))
          ->fetchField();
        $pass_own_menu_check = !$has_own_menu;
      }
      if ($pass_own_menu_check) {
        $has_terms = FALSE;

        //TODO figure out: maybe we have to choose language in term reference fields
        foreach ($settings['instances'] as $field) {
          if (!empty($entity->{$field})) {
            $has_terms = TRUE;
            break;
          }
        }
        if ($has_terms) {
          module_load_include('inc', 'taxonomy_menu_trails');
          _taxonomy_menu_trails_process($entity, $settings);
        }
      }
    }
  }
}