function conditional_fields_nodeapi in Conditional Fields 6.2
Same name and namespace in other branches
- 5 conditional_fields.module \conditional_fields_nodeapi()
- 6 conditional_fields.module \conditional_fields_nodeapi()
Implementation of hook_nodeapi().
File
- ./
conditional_fields.module, line 190 - 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
$field_key = array_keys($type['fields'][$field['control_field_name']]['columns']);
foreach ((array) $node->{$field}['control_field_name'] as $value) {
$current_values[$field['control_field_name']][] = $value[$field_key[0]];
if (!empty($value[$field_key[0]])) {
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;
}
}
}
}
}