You are here

function conditional_fields_nodeapi in Conditional Fields 6

Same name and namespace in other branches
  1. 5 conditional_fields.module \conditional_fields_nodeapi()
  2. 6.2 conditional_fields.module \conditional_fields_nodeapi()

Implementation of hook_nodeapi().

File

./conditional_fields.module, line 189
Content fields and groups visibility based on the values of user defined 'trigger' fields.

Code

function conditional_fields_nodeapi(&$node, $op, $teaser, $page) {
  if ($op == 'view') {

    // First we check if there any conditional fields in this node type
    $type = content_types($node->type);
    if (!($data = conditional_fields_load_data($type['type']))) {
      return;
    }

    // Then we check if user is an administrator and this content type
    // and has the show hidden fields pref enabled
    if (user_access('administer conditional fields') && variable_get('c_fields_show_all_' . $type['type'], 0)) {
      return;
    }
    foreach ($data as $field) {

      // We might have to look for the field in a group
      $controlled_group = conditional_fields_get_group($node->type, $field['field_name']);
      $controlling_group = conditional_fields_get_group($node->type, $field['control_field_name']);

      // The controlled field is not in a group and is not in the form for other reasons. Skip.
      if (!$controlled_group && !$node->content[$field['field_name']]) {
        continue;
      }

      // The controlled field is in a group and is not the form for other reasons. Skip.
      if ($controlled_group && !$node->content[$controlled_group]['group'][$field['field_name']]) {
        continue;
      }
      $viewed = FALSE;
      $current_values[$field['control_field_name']] = array();

      // Create an array with the selected controlling field's values
      // Check if the controlling field is viewed as well
      foreach ((array) $node->{$field}['control_field_name'] as $value) {
        $current_values[$field['control_field_name']][] = $value['value'];
        if (!empty($value['value'])) {
          if ($node->content[$field['control_field_name']]['field']['#access'] == TRUE || $controlling_group && $node->content[$controlling_group]['group'][$field['control_field_name']]['field']['#access'] == TRUE) {
            $viewed = TRUE;
          }
        }
      }
      if ($viewed) {

        // Hide the controlled field if it is not triggered
        if (!conditional_fields_is_triggered($current_values[$field['control_field_name']], $field['trigger_values'])) {
          if ($controlled_group) {
            $node->content[$controlled_group]['group'][$field['field_name']]['#access'] = FALSE;
          }
          else {
            $node->content[$field['field_name']]['#access'] = FALSE;
          }
        }
      }
      else {

        // Apply orphaned fields settings
        $orphaned_settings = variable_get('c_fields_view_' . $node->type, C_FIELDS_ORPHANED_SHOW_TRIGGERED);
        switch ($orphaned_settings) {
          case C_FIELDS_ORPHANED_SHOW_TRIGGERED:

            // If the field was triggered, don't hide it
            if (conditional_fields_is_triggered($current_values[$field['control_field_name']], $field['trigger_values'])) {
              break;
            }
          case C_FIELDS_ORPHANED_HIDE:

            // We hide the field
            if ($controlled_group) {
              $node->content[$controlled_group]['group'][$field['field_name']]['#access'] = FALSE;
            }
            else {
              $node->content[$field['field_name']]['#access'] = FALSE;
            }
          case C_FIELDS_ORPHANED_SHOW_ALL:

            // Nothing to do...
            break;
        }
      }
    }
  }
}