You are here

function menu_position_menu_position_condition_taxonomy in Menu Position 7

Same name and namespace in other branches
  1. 6 plugins/menu_position.taxonomy.inc \menu_position_menu_position_condition_taxonomy()
  2. 7.2 plugins/menu_position.taxonomy.inc \menu_position_menu_position_condition_taxonomy()

Checks if a specific taxonomy term is set in the node.

Parameters

array $variables: An array containing each of the variables saved in the database necessary to evaluate this condition of the rule.

Return value

bool TRUE if condition applies successfully. Otherwise FALSE.

File

plugins/menu_position.taxonomy.inc, line 17
Provides the "Taxonomy" rule plugin for the Menu Position module.

Code

function menu_position_menu_position_condition_taxonomy(array $variables) {

  // We can only check for taxonomy terms on an "entity" page.
  if ($variables['context']['entity_type']) {

    // Fallback for legacy configuration.
    if (empty($variables['match_types'])) {
      $variables['match_types'] = array(
        'terms' => '',
        'content' => 'content',
      );
    }
    $match_types = $variables['match_types'];

    // Grab the variables stored statically in the rule.
    $vid = $variables['vid'];
    $tid = $variables['tid'];

    // Determine what kind of entity page this is.
    $entity_type = $variables['context']['entity_type'];
    $bundle_name = $variables['context']['bundle_name'];
    $entity = $variables['context'][$entity_type];
    if ($match_types['content']) {

      // Build a list of each taxonomy reference fields.
      $taxonomy_fields =& drupal_static(__FUNCTION__, NULL);
      if (!isset($taxonomy_fields)) {
        $taxonomy_fields = array();
        $field_info = field_info_fields();
        foreach (array_keys(field_info_instances($entity_type, $bundle_name)) as $key) {
          if ($field_info[$key]['type'] == 'taxonomy_term_reference') {
            $taxonomy_fields[$key] = $field_info[$key]['translatable'];
          }
        }
      }
      $tids = array();
      foreach ($taxonomy_fields as $field => $translatable) {
        $language = $translatable ? $entity->language : LANGUAGE_NONE;
        if (!empty($entity->{$field}[$language])) {

          // Check for matching terms.
          if (!empty($tid)) {
            foreach ($entity->{$field}[$language] as $term) {
              if (in_array($term['tid'], $tid)) {
                return TRUE;
              }
            }
          }
          else {
            foreach ($entity->{$field}[$language] as $term) {
              $tids[] = $term['tid'];
            }
          }
        }
      }
      if (count($tids)) {
        $count = db_query("SELECT count(tid) FROM {taxonomy_term_data} WHERE tid IN (:tids) AND vid = :vid", array(
          ':tids' => $tids,
          ':vid' => $vid,
        ))
          ->fetchField();
        if ($count > 0) {
          return TRUE;
        }
      }
    }

    // Taxonomy terms.
    if ($entity_type == 'taxonomy_term' && $match_types['terms']) {

      // Check for matching terms.
      if (!empty($tid)) {
        if (isset($entity->tid) && in_array($entity->tid, $tid)) {
          return TRUE;
        }
      }
      else {
        if (isset($entity->vid) && $entity->vid === $vid) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}