You are here

function menu_position_menu_position_condition_taxonomy in Menu Position 7.2

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

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

Parameters

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

Return value

TRUE if condition applies successfully. Otherwise FALSE.

File

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

Code

function menu_position_menu_position_condition_taxonomy($variables) {

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

    // 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];

    // 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'];
        }
      }
    }
    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) {
            if (isset($term['taxonomy_term']->vid) && $term['taxonomy_term']->vid === $vid) {
              return TRUE;
            }
          }
        }
      }
    }
  }
  return FALSE;
}