You are here

function conditional_fields_entity_view_alter in Conditional Fields 7.3

Implements hook_entity_view_alter().

Applies entity view logic to conditional fields.

File

./conditional_fields.module, line 841
Define dependencies between fields based on their states and values.

Code

function conditional_fields_entity_view_alter(&$build, $type) {
  if (!(isset($build['#entity_type'], $build['#bundle']) && ($dependencies = conditional_fields_load_dependencies($build['#entity_type'], $build['#bundle'])))) {
    return;
  }
  $evaluated_dependents = array();
  foreach ($dependencies['dependents'] as $dependent => $dependency) {
    if (empty($build[$dependent]['#access'])) {
      continue;
    }
    foreach ($dependency as $dependency_options) {
      $dependee = $dependency_options['dependee'];
      $options = $dependency_options['options'];

      // We can interface with the States API only through the Value condition.
      if ($options['condition'] != 'value') {
        continue;
      }

      // Determine field view behaviors.
      // If this dependent has multiple dependencies, only the logic of the
      // first dependency will be taken into account.
      if (!isset($behaviors)) {
        $behaviors = conditional_fields_field_behaviors('view', $options);
      }

      // Manage orphan fields (dependents with no dependees).
      $evaluate = in_array(CONDITIONAL_FIELDS_FIELD_VIEW_EVALUATE, $behaviors);
      $hide_orphan = in_array(CONDITIONAL_FIELDS_FIELD_VIEW_HIDE_ORPHAN, $behaviors);
      $hide_untriggered_orphan = in_array(CONDITIONAL_FIELDS_FIELD_VIEW_HIDE_UNTRIGGERED_ORPHAN, $behaviors);
      $is_orphan = empty($build[$dependee]['#access']);
      if ($is_orphan) {

        // Hide the dependent. No need to evaluate the dependency.
        if ($hide_orphan) {
          $build[$dependent]['#access'] = FALSE;
          continue;
        }
        if ($hide_untriggered_orphan) {
          $evaluate = TRUE;
        }
        if ($evaluate) {

          // We have to look for the dependee in the entity object.
          // TODO: Is it possible to avoid hardcoding this?
          switch ($type) {
            case 'node':
              $entity_property = '#node';
              break;
            case 'user':
              $entity_property = '#account';
              break;
            case 'term':
              $entity_property = '#term';
              break;
            case 'field_collection_item':
            case 'profile2':
            default:
              $entity_property = '#entity';
          }

          // If we didn't find the field, there is nothing more we can do.
          if (!isset($build[$entity_property]->{$dependee})) {
            continue;
          }
          $items = $build[$entity_property]->{$dependee};

          // Values are keyed by language here, remove it.
          $items = array_shift($items);
        }
      }
      else {
        $items = $build[$dependee]['#items'];
      }
      if ($evaluate) {
        $evaluated_dependents[$dependent][$options['grouping']][] = conditional_fields_evaluate_dependency('view', $items, $options);
      }
    }
    if (isset($evaluated_dependents[$dependent])) {
      $is_triggered = conditional_fields_evaluate_grouping($evaluated_dependents[$dependent]);
      foreach ($behaviors as $behavior) {
        switch ($behavior) {
          case CONDITIONAL_FIELDS_FIELD_VIEW_EVALUATE:

            // Hide the dependent if it is not triggered.
            if (!$is_triggered) {
              $build[$dependent]['#access'] = FALSE;
            }
            break;
          case CONDITIONAL_FIELDS_FIELD_VIEW_HIDE_ORPHAN:

            // This case was handled while looking for the field.
            break;
          case CONDITIONAL_FIELDS_FIELD_VIEW_HIDE_UNTRIGGERED_ORPHAN:

            // Hide the dependent if the dependee is not viewable and the dependency is not triggered.
            if ($is_orphan && !$is_triggered) {
              $build[$dependent]['#access'] = FALSE;
            }
            break;
          case CONDITIONAL_FIELDS_FIELD_VIEW_HIGHLIGHT:

            // Show the dependent themed like an error message if it is not triggered.
            if (!$is_triggered) {
              $build[$dependent]['#prefix'] = isset($build[$dependent]['#prefix']) ? '<div class="messages error">' . $build[$dependent]['#prefix'] : '<div class="messages error">';
              $build[$dependent]['#suffix'] = isset($build[$dependent]['#suffix']) ? $build[$dependent]['#suffix'] . '</div>' : '</div>';
            }
            break;
          case CONDITIONAL_FIELDS_FIELD_VIEW_DESCRIBE:

            // Show a textual description of the dependency under the dependent field.
            if ($build[$dependent]['#access']) {
              $dependee_title = isset($build[$dependee]['#title']) ? $build[$dependee]['#title'] : $dependee;
              $dependent_title = isset($build[$dependent]['#title']) ? $build[$dependent]['#title'] : $dependent;
              $description = conditional_fields_dependency_description($dependee_title, $dependent_title, $options);
              if (isset($build[$dependent]['#suffix'])) {
                $description = $build[$dependent]['#suffix'] . $description;
              }
              $build[$dependent]['#suffix'] = $description;
            }
            break;
          default:

            // Custom behaviors are callbacks.
            $behavior($dependee, $dependent, $is_triggered, $dependencies, $build, $type);
            break;
        }
        if (empty($build[$dependent]['#access'])) {
          break;
        }
      }
    }
    unset($behaviors);
  }
}