You are here

function fc_conditional_fc_instances_alter in Field Complete 7

Implements hook_fc_instances_alter().

Remove any fields that are hidden due to conditional_fields module.

File

fc_conditional/fc_conditional.module, line 13
Field Complete Conditional - Provides the glue between the field complete module and the conditional fields module.

Code

function fc_conditional_fc_instances_alter(&$instances, $entity_type, $entity, $fc_options) {
  if (fc_entity_is_enabled($entity_type)) {
    $dependencies = conditional_fields_load_dependencies($entity_type, $fc_options['bundle']);
    if (empty($dependencies)) {

      // No dependencies at all for this entity type/bundle, so skip it
      return;
    }
    $dependents = array_intersect_key($dependencies['dependents'], $instances);
    if (empty($dependents)) {

      // No dependencies for the fields we're looking at, so skip it
      return;
    }
    foreach ($dependents as $field_name => $dependency) {
      $disable = FALSE;
      foreach ($dependency as $dependent) {
        $values = field_get_items($entity_type, $entity, $dependent['dependee']);
        $options = $dependent['options'];
        $result = conditional_fields_evaluate_dependency($fc_options['context'], $values, $options);
        switch ($options['state']) {
          case 'visible':
            $disable = !$result;
            break;
          case '!visible':
            $disable = $result;
            break;
        }
        if ($disable) {
          break;
        }
      }
      if ($disable) {
        $instances[$field_name]['settings']['fc']['disable'] = TRUE;
      }
    }
  }
}