function conditional_fields_nodeapi in Conditional Fields 5
Same name and namespace in other branches
- 6.2 conditional_fields.module \conditional_fields_nodeapi()
- 6 conditional_fields.module \conditional_fields_nodeapi()
Implementation of hook_nodeapi()
File
- ./
conditional_fields.module, line 129
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;
}
$orphaned_settings = variable_get('c_fields_view_' . $node->type, C_FIELDS_ORPHANED_SHOW_TRIGGERED);
foreach ($data as $field) {
// Check if we must look for the controlling field in a group
if (module_exists('fieldgroup')) {
$group = fieldgroup_get_group($node->type, $field['control_field_name']);
}
else {
$group = FALSE;
}
// Check if the controlling field is viewed
$viewed = FALSE;
if ($group && !empty($node->content[$group][$field['control_field_name']]['#value'])) {
$viewed = TRUE;
}
if (!$group && !empty($node->content[$field['control_field_name']]['#value'])) {
$viewed = TRUE;
}
// Create an array with the selected controlling field's values
$current_values = array();
if ($node->{$field['control_field_name']}) {
foreach ($node->{$field['control_field_name']} as $value) {
$current_values[] = $value['value'];
}
}
if ($viewed) {
// Hide the controlled field if it is not triggered
if (!conditional_fields_is_triggered($current_values, $field['trigger_values'])) {
if ($group) {
unset($node->content[$group][$field['field_name']]);
$changed_groups[$group] = $group;
}
else {
unset($node->content[$field['field_name']]);
}
}
}
else {
// Apply orphaned fields settings
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['trigger_values'])) {
break;
}
case C_FIELDS_ORPHANED_HIDE:
// We hide the field
if ($group) {
unset($node->content[$group][$field['field_name']]);
$changed_groups[$group] = $group;
}
else {
unset($node->content[$field['field_name']]);
}
case C_FIELDS_ORPHANED_SHOW_ALL:
// Nothing to do...
break;
}
}
}
// If a group is now empty, we should hide it too
foreach ((array) $changed_groups as $group) {
$keep = FALSE;
foreach ((array) $node->content[$group] as $key => $group_content) {
if (substr($key, 0, 6) == 'field_' && $group_content['#value'] !== '') {
$keep = TRUE;
}
}
if ($keep == FALSE) {
unset($node->content[$group]);
}
}
}
}