| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id$ |
| 3 |
|
/** |
| 4 |
|
* @file |
| 5 |
|
* Content fields and groups visibility based on the values of user defined 'trigger' fields. |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
// Fields settings |
// Fields settings |
| 9 |
define ('C_FIELDS_JS_NO', 0); |
define ('C_FIELDS_JS_NO', 0); |
| 47 |
*/ |
*/ |
| 48 |
function conditional_fields_menu() { |
function conditional_fields_menu() { |
| 49 |
$items = array(); |
$items = array(); |
| 50 |
|
|
| 51 |
foreach (node_get_types() as $type) { |
foreach (node_get_types() as $type) { |
| 52 |
$content_type = content_types($type->type); |
$content_type = content_types($type->type); |
| 53 |
|
|
| 60 |
'weight' => 5 |
'weight' => 5 |
| 61 |
); |
); |
| 62 |
} |
} |
| 63 |
|
|
| 64 |
return $items; |
return $items; |
| 65 |
} |
} |
| 66 |
|
|
| 67 |
/** |
/** |
| 68 |
* Implementation of hook_perm() |
* Implementation of hook_perm(). |
| 69 |
*/ |
*/ |
| 70 |
function conditional_fields_perm() { |
function conditional_fields_perm() { |
| 71 |
return array('administer conditional fields'); |
return array('administer conditional fields'); |
| 81 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 82 |
'#title' => t('User Interface options'), |
'#title' => t('User Interface options'), |
| 83 |
'#collapsible' => TRUE |
'#collapsible' => TRUE |
| 84 |
); |
); |
| 85 |
$form['js_set']['js'] = array( |
$form['js_set']['js'] = array( |
| 86 |
'#type' => 'radios', |
'#type' => 'radios', |
| 87 |
'#options' => array( |
'#options' => array( |
| 88 |
C_FIELDS_JS_NO => t("Don't use javascript. Fields are only hidden on node view."), |
C_FIELDS_JS_NO => t("Don't use javascript. Fields are only hidden on node view."), |
| 89 |
C_FIELDS_JS_HIDE => t('Hide untriggered fields.'), |
C_FIELDS_JS_HIDE => t('Hide untriggered fields.'), |
| 90 |
C_FIELDS_JS_DISABLE => t('Disable untriggered fields.') |
C_FIELDS_JS_DISABLE => t('Disable untriggered fields.') |
| 91 |
), |
), |
| 92 |
'#title' => 'Javascript', |
'#title' => 'Javascript', |
| 93 |
'#description' => t('Choose the desired javascript behaviour in node editing forms.'), |
'#description' => t('Choose the desired javascript behaviour in node editing forms.'), |
| 94 |
'#default_value' => variable_get('c_fields_js_' . $type, C_FIELDS_JS_HIDE) |
'#default_value' => variable_get('c_fields_js_' . $type, C_FIELDS_JS_HIDE) |
| 95 |
); |
); |
| 96 |
$form['js_set']['anim'] = array( |
$form['js_set']['anim'] = array( |
| 97 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 98 |
'#title' => t('Animation'), |
'#title' => t('Animation'), |
| 99 |
'#description' => t("These settings have effect only if you select the 'Hide untriggered fields' option above."), |
'#description' => t("These settings have effect only if you select the 'Hide untriggered fields' option above."), |
| 100 |
); |
); |
| 101 |
$form['js_set']['anim']['animation'] = array( |
$form['js_set']['anim']['animation'] = array( |
| 102 |
'#type' => 'radios', |
'#type' => 'radios', |
| 103 |
'#title' => t('Type'), |
'#title' => t('Type'), |
| 106 |
C_FIELDS_ANIMATION_NO => t('No animation'), |
C_FIELDS_ANIMATION_NO => t('No animation'), |
| 107 |
C_FIELDS_ANIMATION_FADE => t('Slide down'), |
C_FIELDS_ANIMATION_FADE => t('Slide down'), |
| 108 |
C_FIELDS_ANIMATION_SLIDE => t('Fade'), |
C_FIELDS_ANIMATION_SLIDE => t('Fade'), |
| 109 |
), |
), |
| 110 |
); |
); |
| 111 |
$form['js_set']['anim']['anim_speed'] = array( |
$form['js_set']['anim']['anim_speed'] = array( |
| 112 |
'#type' => 'radios', |
'#type' => 'radios', |
| 117 |
'slow' => t('Slow'), |
'slow' => t('Slow'), |
| 118 |
'normal' => t('Normal'), |
'normal' => t('Normal'), |
| 119 |
'fast' => t('Fast'), |
'fast' => t('Fast'), |
| 120 |
), |
), |
| 121 |
); |
); |
| 122 |
$form['orphaned'] = array( |
$form['orphaned'] = array( |
| 123 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 161 |
'#type' => 'submit', |
'#type' => 'submit', |
| 162 |
'#value' => t('Save'), |
'#value' => t('Save'), |
| 163 |
); |
); |
| 164 |
|
|
| 165 |
return $form; |
return $form; |
| 166 |
} |
} |
| 167 |
|
|
| 168 |
function _conditional_fields_admin_submit($form, &$form_state) { |
function _conditional_fields_admin_submit($form, &$form_state) { |
| 169 |
$type = $form['#parameters'][2]; |
$type = $form['#parameters'][2]; |
| 170 |
|
|
| 171 |
if ($form_state['values']['delete'] == 1) { |
variable_set('c_fields_js_' . $type, $form_state['values']['js']); |
|
conditional_fields_node_type_delete($type); |
|
|
$message = t(' All configured conditional fields have been deleted.'); |
|
|
} |
|
|
|
|
|
variable_set('c_fields_js_' . $type, $form_state['values']['js']); |
|
| 172 |
variable_set('c_fields_animation_' . $type, $form_state['values']['animation']); |
variable_set('c_fields_animation_' . $type, $form_state['values']['animation']); |
| 173 |
variable_set('c_fields_anim_speed_' . $type, $form_state['values']['anim_speed']); |
variable_set('c_fields_anim_speed_' . $type, $form_state['values']['anim_speed']); |
| 174 |
variable_set('c_fields_view_' . $type, $form_state['values']['orphaned_view']); |
variable_set('c_fields_view_' . $type, $form_state['values']['orphaned_view']); |
| 177 |
variable_set('c_fields_show_all_' . $type, $form_state['values']['show_all']); |
variable_set('c_fields_show_all_' . $type, $form_state['values']['show_all']); |
| 178 |
|
|
| 179 |
drupal_set_message(t('Conditional fields options for this content type saved.') . $message); |
drupal_set_message(t('Conditional fields options for this content type saved.') . $message); |
| 180 |
|
|
| 181 |
|
if ($form_state['values']['delete'] == 1) { |
| 182 |
|
conditional_fields_node_type_delete($type); |
| 183 |
|
drupal_set_message(t('All configured conditional fields for this content type have been deleted.')); |
| 184 |
|
} |
| 185 |
} |
} |
| 186 |
|
|
| 187 |
/** |
/** |
| 188 |
* Implementation of hook_nodeapi() |
* Implementation of hook_nodeapi(). |
| 189 |
*/ |
*/ |
| 190 |
function conditional_fields_nodeapi(&$node, $op, $teaser, $page) { |
function conditional_fields_nodeapi(&$node, $op, $teaser, $page) { |
| 191 |
if ($op == 'view') { |
if ($op == 'view') { |
| 194 |
if (!$data = conditional_fields_load_data($type['type'])) { |
if (!$data = conditional_fields_load_data($type['type'])) { |
| 195 |
return; |
return; |
| 196 |
} |
} |
| 197 |
|
|
| 198 |
// Then we check if user is an administrator and this content type |
// Then we check if user is an administrator and this content type |
| 199 |
// and has the show hidden fields pref enabled |
// and has the show hidden fields pref enabled |
| 200 |
if (user_access('administer conditional fields') && variable_get('c_fields_show_all_' . $type['type'], 0)) { |
if (user_access('administer conditional fields') && variable_get('c_fields_show_all_' . $type['type'], 0)) { |
| 201 |
return; |
return; |
| 202 |
} |
} |
| 203 |
|
|
| 204 |
foreach ($data as $field) { |
foreach ($data as $field) { |
| 205 |
|
|
| 206 |
// We might have to look for the field in a group |
// We might have to look for the field in a group |
| 207 |
$controlled_group = conditional_fields_get_group($node->type, $field['field_name']); |
$controlled_group = conditional_fields_get_group($node->type, $field['field_name']); |
| 208 |
$controlling_group = conditional_fields_get_group($node->type, $field['control_field_name']); |
$controlling_group = conditional_fields_get_group($node->type, $field['control_field_name']); |
| 209 |
|
|
| 210 |
// The controlled field is not in a group and is not in the form for other reasons. Skip. |
// The controlled field is not in a group and is not in the form for other reasons. Skip. |
| 211 |
if (!$controlled_group && !$node->content[$field['field_name']]) { |
if (!$controlled_group && !$node->content[$field['field_name']]) { |
| 212 |
continue; |
continue; |
| 213 |
} |
} |
| 214 |
|
|
| 215 |
// The controlling field is not in a group and is not the form for other reasons. Skip. |
// The controlling field is not in a group and is not the form for other reasons. Skip. |
| 216 |
if (!$controlling_group && !$node->content[$field['control_field_name']]) { |
if (!$controlling_group && !$node->content[$field['control_field_name']]) { |
| 217 |
continue; |
continue; |
| 218 |
} |
} |
| 219 |
|
|
| 220 |
// The controlled field is in a group and is not the form for other reasons. Skip. |
// The controlled field is in a group and is not the form for other reasons. Skip. |
| 221 |
if ($controlled_group && !$node->content[$controlled_group]['group'][$field['field_name']]) { |
if ($controlled_group && !$node->content[$controlled_group]['group'][$field['field_name']]) { |
| 222 |
continue; |
continue; |
| 226 |
if ($controlling_group && !$node->content[$controlling_group]['group'][$field['control_field_name']]) { |
if ($controlling_group && !$node->content[$controlling_group]['group'][$field['control_field_name']]) { |
| 227 |
continue; |
continue; |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
$viewed = FALSE; |
$viewed = FALSE; |
| 231 |
|
|
| 232 |
// Create an array with the selected controlling field's values |
// Create an array with the selected controlling field's values |
| 233 |
// Check if the controlling field is viewed as well |
// Check if the controlling field is viewed as well |
| 234 |
foreach ((array)$node->$field['control_field_name'] as $value) { |
foreach ((array)$node->$field['control_field_name'] as $value) { |
| 235 |
$current_values[$field['control_field_name']] = $value['value']; |
$current_values[$field['control_field_name']] = $value['value']; |
| 236 |
if (!empty($value['value']) && $node->content[$field['control_field_name']]['field']['#access'] == TRUE) { |
if (!empty($value['value']) && $node->content[$field['control_field_name']]['field']['#access'] == TRUE) { |
| 237 |
$viewed = TRUE; |
$viewed = TRUE; |
| 238 |
} |
} |
| 239 |
} |
} |
| 240 |
|
|
| 241 |
if ($viewed) { |
if ($viewed) { |
| 275 |
} |
} |
| 276 |
} |
} |
| 277 |
} |
} |
| 278 |
|
|
| 279 |
/** |
/** |
| 280 |
* Implementation of hook_form_alter() |
* Implementation of hook_form_alter(). |
| 281 |
*/ |
*/ |
| 282 |
function conditional_fields_form_alter(&$form, $form_state, $form_id) { |
function conditional_fields_form_alter(&$form, $form_state, $form_id) { |
| 283 |
switch ($form_id) { |
switch ($form_id) { |
| 290 |
conditional_fields_fieldgroup_group_edit_form($form); |
conditional_fields_fieldgroup_group_edit_form($form); |
| 291 |
break; |
break; |
| 292 |
case 'content_field_overview_form': |
case 'content_field_overview_form': |
| 293 |
// Find conditional fields, mark them, and disable group select for them |
// Find conditional fields, mark them, and disable group select for them |
| 294 |
$conditional_fields = conditional_fields_field_overview_form($form); |
$conditional_fields = conditional_fields_field_overview_form($form); |
| 295 |
break; |
break; |
| 296 |
case '_content_admin_field_remove': |
case '_content_admin_field_remove': |
| 297 |
$form['#submit'] = $form['#submit'] + array('_conditional_fields_content_admin_field_remove_submit' => array()); |
$form['#submit'] = $form['#submit'] + array('_conditional_fields_content_admin_field_remove_submit' => array()); |
| 314 |
$form['#submit'][] = 'conditional_fields_import'; |
$form['#submit'][] = 'conditional_fields_import'; |
| 315 |
break; |
break; |
| 316 |
} |
} |
| 317 |
|
|
| 318 |
if (isset($form['type']['#value']) && $form_id == $form['type']['#value'] . '_node_form') { |
if (isset($form['type']['#value']) && $form_id == $form['type']['#value'] . '_node_form') { |
| 319 |
conditional_fields_node_editing_form($form, $form_state); |
conditional_fields_node_editing_form($form, $form_state); |
| 320 |
} |
} |
| 321 |
} |
} |
| 322 |
|
|
| 323 |
/** |
/** |
| 324 |
* Alteration of the field editing form |
* Alteration of the field editing form |
| 325 |
*/ |
*/ |
| 340 |
$controlled_fields[$row['field_name']] = $row['trigger_values']; |
$controlled_fields[$row['field_name']] = $row['trigger_values']; |
| 341 |
} |
} |
| 342 |
} |
} |
| 343 |
|
|
| 344 |
// Build controlling field form. Return here because nested conditional fields are not supported. |
// Build controlling field form. Return here because nested conditional fields are not supported. |
| 345 |
if (!empty($controlled_fields)) { |
if (!empty($controlled_fields)) { |
| 346 |
$form['#controlled_fields'] = $controlled_fields; |
$form['#controlled_fields'] = $controlled_fields; |
| 350 |
|
|
| 351 |
conditional_fields_content_admin_field_controlling($form['widget'], $type, $controlled_fields); |
conditional_fields_content_admin_field_controlling($form['widget'], $type, $controlled_fields); |
| 352 |
return; |
return; |
| 353 |
} |
} |
| 354 |
|
|
| 355 |
// Check if field is in a group |
// Check if field is in a group |
| 356 |
if (isset($form['widget']['group'])) { |
if (isset($form['widget']['group'])) { |
| 378 |
|
|
| 379 |
if (isset($available_fields)) { |
if (isset($available_fields)) { |
| 380 |
conditional_fields_content_admin_field_controllable($form['widget'], $type, $form['field_name']['#value'], $available_fields, $allowed_values, 'field'); |
conditional_fields_content_admin_field_controllable($form['widget'], $type, $form['field_name']['#value'], $available_fields, $allowed_values, 'field'); |
| 381 |
|
|
| 382 |
// Add validation function |
// Add validation function |
| 383 |
$form['#validate'] = array_merge(array('conditional_fields_content_admin_field_validate'), $form['#validate']); |
$form['#validate'] = array_merge(array('conditional_fields_content_admin_field_validate'), $form['#validate']); |
| 384 |
|
|
| 397 |
conditional_fields_content_admin_field_fieldset($form, $description); |
conditional_fields_content_admin_field_fieldset($form, $description); |
| 398 |
|
|
| 399 |
foreach ($controlled_fields as $field => $trigger_values) { |
foreach ($controlled_fields as $field => $trigger_values) { |
| 400 |
if (substr($field, 0, 6) == 'group_') { // It's a group |
if (strpos($field , 'group_') === 0) { // It's a group |
| 401 |
$rows[] = array($field, |
$rows[] = array($field, |
| 402 |
implode($trigger_values, ', '), |
implode($trigger_values, ', '), |
| 403 |
t('group'), |
t('group'), |
| 404 |
t('<a href="@edit-group">edit</a>', array('@edit-group' => url('admin/content/node-type/' . $type['url_str'] . '/groups/' . $field, array('fragment' => 'conditional-fields-settings')))), |
t('<a href="@edit-group">edit</a>', array('@edit-group' => url('admin/content/node-type/' . $type['url_str'] . '/groups/' . $field, array('fragment' => 'conditional-fields-settings')))), |
| 405 |
); |
); |
| 406 |
} |
} |
| 407 |
else { // It's a field |
else { // It's a field |
| 408 |
$rows[] = array($field, |
$rows[] = array($field, |
| 409 |
implode($trigger_values, ', '), |
implode($trigger_values, ', '), |
| 410 |
t('field'), |
t('field'), |
| 411 |
t('<a href="@edit-field">edit</a>', array('@edit-field' => url('admin/content/node-type/' . $type['url_str'] . '/fields/' . $field, array('fragment' => 'conditional-fields-settings')))), |
t('<a href="@edit-field">edit</a>', array('@edit-field' => url('admin/content/node-type/' . $type['url_str'] . '/fields/' . $field, array('fragment' => 'conditional-fields-settings')))), |
| 412 |
); |
); |
| 413 |
} |
} |
| 422 |
$description = '<p>' . t('Choose which allowed values of available controlling fields will trigger this @context, making it visible both in node editing and view. If no value is set, the @context will be always visible. Only fields and groups within the same group as this one, and with <em>Allowed values</em> set, are available for control.', array('@context' => t($context))) . '</p>'; |
$description = '<p>' . t('Choose which allowed values of available controlling fields will trigger this @context, making it visible both in node editing and view. If no value is set, the @context will be always visible. Only fields and groups within the same group as this one, and with <em>Allowed values</em> set, are available for control.', array('@context' => t($context))) . '</p>'; |
| 423 |
|
|
| 424 |
conditional_fields_content_admin_field_fieldset($form, $description, TRUE); |
conditional_fields_content_admin_field_fieldset($form, $description, TRUE); |
| 425 |
|
|
| 426 |
$default_values = conditional_fields_available_fields_default_values($field_name, $available_fields); |
$default_values = conditional_fields_available_fields_default_values($field_name, $available_fields); |
| 427 |
|
|
| 428 |
// Create selection lists |
// Create selection lists |
| 429 |
foreach ($available_fields as $field) { |
foreach ($available_fields as $field) { |
| 430 |
$allowed_values[$field['field_name']] = array('conditional_field_no_value' => t('- Not controlling -')) + $allowed_values[$field['field_name']]; |
$allowed_values[$field['field_name']] = array('conditional_field_no_value' => t('- Not controlling -')) + $allowed_values[$field['field_name']]; |
| 431 |
|
|
| 432 |
if (isset($default_values[$field['field_name']]) && $default_values[$field['field_name']] != FALSE) { |
if (isset($default_values[$field['field_name']]) && $default_values[$field['field_name']] != FALSE) { |
| 433 |
$default_value = $default_values[$field['field_name']]; |
$default_value = $default_values[$field['field_name']]; |
| 434 |
$set = TRUE; |
$set = TRUE; |
| 436 |
else { |
else { |
| 437 |
$default_value = 'conditional_field_no_value'; |
$default_value = 'conditional_field_no_value'; |
| 438 |
} |
} |
| 439 |
|
|
| 440 |
$form['conditional_fields'][$field['field_name']] = array( |
$form['conditional_fields'][$field['field_name']] = array( |
| 441 |
'#type' => 'select', |
'#type' => 'select', |
| 442 |
'#multiple' => TRUE, |
'#multiple' => TRUE, |
| 443 |
'#title' => t($field['widget']['label']) . ' (' . $field['field_name'] . ')', |
'#title' => $field['widget']['label'] . ' (' . $field['field_name'] . ')', |
| 444 |
// To do: set right url for groups |
// To do: set right url for groups |
| 445 |
'#description' => t('<a href="@edit-field">Edit the allowed values</a> of %field-name.', array('@edit-field' => url('admin/content/node-type/' . $type['url_str'] . '/fields/' . $field['field_name'], array('query' => 'destination=admin/content/node-type/' . arg(3) . '/' . arg(4) . '/' . arg(5))), '%field-name' => $field['field_name']) ), |
'#description' => t('<a href="@edit-field">Edit the allowed values</a> of %field-name.', array('@edit-field' => url('admin/content/node-type/' . $type['url_str'] . '/fields/' . $field['field_name'], array('query' => 'destination=admin/content/node-type/' . arg(3) . '/' . arg(4) . '/' . arg(5))), '%field-name' => $field['field_name']) ), |
| 446 |
'#options' => $allowed_values[$field['field_name']], |
'#options' => $allowed_values[$field['field_name']], |
| 447 |
'#default_value' => $default_value, |
'#default_value' => $default_value, |
| 448 |
); |
); |
| 449 |
|
|
| 450 |
} |
} |
| 451 |
|
|
| 452 |
// Don't collapse the settings if we already have a configuration |
// Don't collapse the settings if we already have a configuration |
| 453 |
if ($set) { |
if ($set) { |
| 454 |
$form['conditional_fields']['#collapsed'] = FALSE; |
$form['conditional_fields']['#collapsed'] = FALSE; |
| 478 |
if (!user_access('administer conditional fields')) { |
if (!user_access('administer conditional fields')) { |
| 479 |
return; |
return; |
| 480 |
} |
} |
| 481 |
|
|
| 482 |
// Find fields with allowed values which are not inside a group |
// Find fields with allowed values which are not inside a group |
| 483 |
foreach ($form['#content_type']['fields'] as $field) { |
foreach ($form['#content_type']['fields'] as $field) { |
| 484 |
$in_group = fieldgroup_get_group($form['#content_type']['type'], $field['field_name']); |
$in_group = fieldgroup_get_group($form['#content_type']['type'], $field['field_name']); |
| 495 |
$form['#validate'][] = 'conditional_fields_content_admin_field_validate'; |
$form['#validate'][] = 'conditional_fields_content_admin_field_validate'; |
| 496 |
// Add submission function |
// Add submission function |
| 497 |
$form['#submit'] = array_merge(array('conditional_fields_forms_submit'), $form['#submit']); |
$form['#submit'] = array_merge(array('conditional_fields_forms_submit'), $form['#submit']); |
| 498 |
} |
} |
| 499 |
return; |
return; |
| 500 |
} |
} |
| 501 |
|
|
| 502 |
/** |
/** |
| 503 |
* Check selection of values |
* Check selection of values |
| 504 |
*/ |
*/ |
| 505 |
function conditional_fields_content_admin_field_validate($form, &$form_state) { |
function conditional_fields_content_admin_field_validate($form, &$form_state) { |
| 506 |
if ($form_state['values']['conditional_fields']) { |
if ($form_state['values']['conditional_fields']) { |
| 507 |
foreach ($form_state['values']['conditional_fields'] as $available_field => $trigger_values) { |
foreach ($form_state['values']['conditional_fields'] as $available_field => $trigger_values) { |
| 508 |
// Disallow selecting Not set and values at the same time |
// Disallow selecting Not set and values at the same time |
| 528 |
|
|
| 529 |
isset($form_state['values']['field_name']) ? $controlled_field = $form_state['values']['field_name'] : $controlled_field = $form_state['values']['group_name']; |
isset($form_state['values']['field_name']) ? $controlled_field = $form_state['values']['field_name'] : $controlled_field = $form_state['values']['group_name']; |
| 530 |
isset($form['#field']['type_name']) ? $type = $form['#field']['type_name'] : $type = $form['#content_type']['type']; |
isset($form['#field']['type_name']) ? $type = $form['#field']['type_name'] : $type = $form['#content_type']['type']; |
| 531 |
|
|
| 532 |
conditional_fields_save_field($type, $controlled_field, $form_state['values']['conditional_fields']); |
conditional_fields_save_field($type, $controlled_field, $form_state['values']['conditional_fields']); |
| 533 |
} |
} |
| 534 |
|
|
| 537 |
*/ |
*/ |
| 538 |
function conditional_fields_node_editing_form(&$form, $form_state) { |
function conditional_fields_node_editing_form(&$form, $form_state) { |
| 539 |
$type_name = $form['type']['#value']; |
$type_name = $form['type']['#value']; |
| 540 |
|
|
| 541 |
// Do nothing if there are no conditional fields |
// Do nothing if there are no conditional fields |
| 542 |
if (!$data = conditional_fields_load_data($type_name)) { |
if (!$data = conditional_fields_load_data($type_name)) { |
| 543 |
return; |
return; |
| 544 |
} |
} |
| 545 |
|
|
| 546 |
// Remove from data fields that user can't edit |
// Remove from data fields that user can't edit |
| 547 |
// and apply orphaned fields settings |
// and apply orphaned fields settings |
| 548 |
$orphaned_settings = variable_get('c_fields_edit_' . $type_name, C_FIELDS_ORPHANED_SHOW_TRIGGERED); |
$orphaned_settings = variable_get('c_fields_edit_' . $type_name, C_FIELDS_ORPHANED_SHOW_TRIGGERED); |
| 571 |
// Check if the controlling field is in form |
// Check if the controlling field is in form |
| 572 |
// If not, unset controlled field |
// If not, unset controlled field |
| 573 |
if ($field['control_field_in_group']) { |
if ($field['control_field_in_group']) { |
| 574 |
if (!$form[$field['control_field_in_group']][$field['control_field_name']] || |
if (!$form[$field['control_field_in_group']][$field['control_field_name']] || |
| 575 |
$form[$field['control_field_in_group']][$field['control_field_name']]['#type'] == 'markup' || |
$form[$field['control_field_in_group']][$field['control_field_name']]['#type'] == 'markup' || |
| 576 |
$form[$field['control_field_in_group']][$field['control_field_name']]['#access'] == FALSE) { |
$form[$field['control_field_in_group']][$field['control_field_name']]['#access'] == FALSE) { |
| 577 |
if (!$show_triggered || !in_array($form_state['values'][$field['control_field_name']][0]['value'], $field['trigger_values'])) { |
if (!$show_triggered || !in_array($form_state['values'][$field['control_field_name']][0]['value'], $field['trigger_values'])) { |
| 578 |
unset($form[$field['control_field_in_group']][$field['field_name']]); |
unset($form[$field['control_field_in_group']][$field['field_name']]); |
| 579 |
unset($data[$key]); |
unset($data[$key]); |
| 581 |
} |
} |
| 582 |
} |
} |
| 583 |
else { |
else { |
| 584 |
if (!$form[$field['control_field_name']] || |
if (!$form[$field['control_field_name']] || |
| 585 |
$form[$field['control_field_name']]['#type'] == 'markup' || |
$form[$field['control_field_name']]['#type'] == 'markup' || |
| 586 |
$form[$field['control_field_name']]['#access'] == FALSE) { |
$form[$field['control_field_name']]['#access'] == FALSE) { |
| 587 |
if (!$show_triggered || !in_array($form_state['values'][$field['control_field_name']][0]['value'], $field['trigger_values'])) { |
if (!$show_triggered || !in_array($form_state['values'][$field['control_field_name']][0]['value'], $field['trigger_values'])) { |
| 588 |
unset($form[$field['field_name']]); |
unset($form[$field['field_name']]); |
| 589 |
unset($data[$key]); |
unset($data[$key]); |
| 596 |
break; |
break; |
| 597 |
} |
} |
| 598 |
} |
} |
| 599 |
|
|
| 600 |
// Data could be empty by now |
// Data could be empty by now |
| 601 |
if (empty($data)) { |
if (empty($data)) { |
| 602 |
return; |
return; |
| 609 |
// Add javascript settings for this field |
// Add javascript settings for this field |
| 610 |
$settings['controlling_fields']['#conditional-' . conditional_fields_form_clean_id($row['control_field_name'])]['#conditional-' . conditional_fields_form_clean_id($row['field_name'])] = array('field_id' => '#conditional-' . conditional_fields_form_clean_id($row['field_name']), 'trigger_values' => $row['trigger_values']); |
$settings['controlling_fields']['#conditional-' . conditional_fields_form_clean_id($row['control_field_name'])]['#conditional-' . conditional_fields_form_clean_id($row['field_name'])] = array('field_id' => '#conditional-' . conditional_fields_form_clean_id($row['field_name']), 'trigger_values' => $row['trigger_values']); |
| 611 |
// To do: feature, add an array of controlled fields to js to allow for multiple controlling fields for a field. |
// To do: feature, add an array of controlled fields to js to allow for multiple controlling fields for a field. |
| 612 |
|
|
| 613 |
// Build helper arrays |
// Build helper arrays |
| 614 |
$controlling_fields[$row['control_field_name']] = $row['control_field_name']; |
$controlling_fields[$row['control_field_name']] = $row['control_field_name']; |
| 615 |
$controlled_fields[$row['field_name']] = $row['field_name']; |
$controlled_fields[$row['field_name']] = $row['field_name']; |
| 616 |
} |
} |
| 617 |
|
|
| 618 |
// Controlled fields and fields inside controlled groups should only be required when user triggers them. |
// Controlled fields and fields inside controlled groups should only be required when user triggers them. |
| 619 |
// Since required input check is hardcoded in _form_validate, we need to unset it here. |
// Since required input check is hardcoded in _form_validate, we need to unset it here. |
| 620 |
// We will check triggered fields in a custom validation form. |
// We will check triggered fields in a custom validation form. |
| 621 |
// Here we also add enclosing divs for easier javascript handling to controlling fields and to controlled fields and groups |
// Here we also add enclosing divs for easier javascript handling to controlling fields and to controlled fields and groups |
| 622 |
$required_fields = array(); |
$required_fields = array(); |
| 623 |
|
|
| 624 |
foreach (element_children($form) as $element) { |
foreach (element_children($form) as $element) { |
| 625 |
// Fields |
// Fields |
| 626 |
if (substr($element, 0, 6) == 'field_') { |
if (strpos($element , 'field_') === 0) { |
| 627 |
if ($form[$element]['#theme'] == 'content_multiple_values') { |
if ($form[$element]['#theme'] == 'content_multiple_values') { |
| 628 |
$form[$element]['#conditional_fields_multiple'] = TRUE; |
$form[$element]['#conditional_fields_multiple'] = TRUE; |
| 629 |
} |
} |
| 640 |
$form[$element]['#theme'] = array('conditional_fields_form_item'); |
$form[$element]['#theme'] = array('conditional_fields_form_item'); |
| 641 |
} |
} |
| 642 |
} |
} |
| 643 |
else if (substr($element, 0, 6) == 'group_') { |
else if (strpos($element , 'group_') === 0) { |
| 644 |
// Groups |
// Groups |
| 645 |
if ($controlled_fields[$element]) { |
if ($controlled_fields[$element]) { |
| 646 |
// Group markup is still hardcoded. |
// Group markup is still hardcoded. |
| 668 |
$form[$element][$group_element]['#controlled_field'] = $group_element; |
$form[$element][$group_element]['#controlled_field'] = $group_element; |
| 669 |
$form[$element][$group_element]['#theme'] = array('conditional_fields_form_item'); |
$form[$element][$group_element]['#theme'] = array('conditional_fields_form_item'); |
| 670 |
} |
} |
| 671 |
} |
} |
| 672 |
} |
} |
| 673 |
} |
} |
| 674 |
|
|
| 685 |
} |
} |
| 686 |
|
|
| 687 |
if ($ui_settings != C_FIELDS_JS_NO) { |
if ($ui_settings != C_FIELDS_JS_NO) { |
| 688 |
conditional_fields_add_js($settings); |
conditional_fields_add_js($settings); |
| 689 |
} |
} |
| 690 |
|
|
| 691 |
// Pass variables for validation |
// Pass variables for validation |
| 692 |
$form['#conditional_fields']['data'] = $data; |
$form['#conditional_fields']['data'] = $data; |
| 693 |
$form['#conditional_fields']['required_fields'] = $required_fields; |
$form['#conditional_fields']['required_fields'] = $required_fields; |
| 719 |
foreach ($form['#conditional_fields']['data'] as $row) { |
foreach ($form['#conditional_fields']['data'] as $row) { |
| 720 |
// Check if the controlling field was triggered |
// Check if the controlling field was triggered |
| 721 |
$triggered = conditional_fields_is_triggered($form_state['values'][$row['control_field_name']], $row['trigger_values']); |
$triggered = conditional_fields_is_triggered($form_state['values'][$row['control_field_name']], $row['trigger_values']); |
| 722 |
|
|
| 723 |
$required_fields = $form['#conditional_fields']['required_fields']; |
$required_fields = $form['#conditional_fields']['required_fields']; |
| 724 |
|
|
| 725 |
// Controlled field |
// Controlled field |
| 726 |
if (substr($row['field_name'], 0, 6) == 'field_') { |
if (strpos($row['field_name'] , 'field_') === 0) { |
| 727 |
|
|
| 728 |
if ($triggered) { |
if ($triggered) { |
| 729 |
|
|
| 730 |
// Check required |
// Check required |
| 731 |
if (!empty($required_fields) && $required_fields[$row['field_name']]) { |
if (!empty($required_fields) && $required_fields[$row['field_name']]) { |
| 732 |
|
|
| 733 |
// Check if the controlled field is empty |
// Check if the controlled field is empty |
| 734 |
if (conditional_fields_check_empty($form_state['values'][$row['field_name']])) { |
if (conditional_fields_check_empty($form_state['values'][$row['field_name']])) { |
| 735 |
|
|
| 736 |
// Check whether the controlled field is in a group or not and set error accordingly |
// Check whether the controlled field is in a group or not and set error accordingly |
| 737 |
if (!isset($required_fields[$row['field_name']]['in_group'])) { |
if (!isset($required_fields[$row['field_name']]['in_group'])) { |
| 738 |
form_error($form[$row['field_name']], |
form_error($form[$row['field_name']], |
| 739 |
t('!name field is required.', |
t('!name field is required.', |
| 740 |
array('!name' => $form[$row['field_name']]['#title']))); |
array('!name' => $form[$row['field_name']]['#title']))); |
| 741 |
} |
} |
| 742 |
else { |
else { |
| 743 |
form_error($form[$required_fields[$row['field_name']]['in_group']][$row['field_name']], |
form_error($form[$required_fields[$row['field_name']]['in_group']][$row['field_name']], |
| 744 |
t('!name field is required.', |
t('!name field is required.', |
| 745 |
array('!name' => $form[$required_fields[$row['field_name']]['in_group']][$row['field_name']]['#title']))); |
array('!name' => $form[$required_fields[$row['field_name']]['in_group']][$row['field_name']]['#title']))); |
| 746 |
} |
} |
| 747 |
} |
} |
| 748 |
} |
} |
| 750 |
else { |
else { |
| 751 |
// Do not submit values of controlled fields which were not triggered (except on preview) |
// Do not submit values of controlled fields which were not triggered (except on preview) |
| 752 |
if (variable_get('c_fields_reset_default_' . $form['type']['#value'], 1) && |
if (variable_get('c_fields_reset_default_' . $form['type']['#value'], 1) && |
| 753 |
!in_array('node_form_build_preview', $form_state['submit_handlers']) && |
!in_array('node_form_build_preview', $form_state['submit_handlers']) && |
| 754 |
is_array($form['#field_info'][$row['field_name']]['widget']['default_value'])) { |
is_array($form['#field_info'][$row['field_name']]['widget']['default_value'])) { |
| 755 |
foreach ($form['#field_info'][$row['field_name']]['widget']['default_value'] as $delta => $value) { |
foreach ($form['#field_info'][$row['field_name']]['widget']['default_value'] as $delta => $value) { |
| 756 |
$form_state['values'][$row['field_name']][$delta]['value'] = $value['value']; |
$form_state['values'][$row['field_name']][$delta]['value'] = $value['value']; |
| 757 |
} |
} |
| 758 |
} |
} |
| 759 |
} |
} |
| 760 |
} |
} |
| 761 |
|
|
| 762 |
// Controlled group |
// Controlled group |
| 763 |
else if (substr($row['field_name'], 0, 6) == 'group_') { |
else if (strpos($row['field_name'] , 'group_') === 0) { |
| 764 |
|
|
| 765 |
foreach (element_children($form[$row['field_name']]) as $field_in_group) { |
foreach (element_children($form[$row['field_name']]) as $field_in_group) { |
| 766 |
|
|
| 767 |
// Check if the controlling field was triggered |
// Check if the controlling field was triggered |
| 768 |
if ($triggered) { |
if ($triggered) { |
| 769 |
|
|
| 770 |
// Check required |
// Check required |
| 771 |
if (!empty($required_fields) |
if (!empty($required_fields) |
| 772 |
&& $required_fields[$field_in_group] |
&& $required_fields[$field_in_group] |
| 773 |
&& !$form[$row['field_name']][$field_in_group]['#controlled_field'] |
&& !$form[$row['field_name']][$field_in_group]['#controlled_field'] |
| 774 |
&& conditional_fields_check_empty($form_state['values'][$field_in_group])) { |
&& conditional_fields_check_empty($form_state['values'][$field_in_group])) { |
| 775 |
|
|
| 776 |
form_error($form[$row['field_name']][$field_in_group], |
form_error($form[$row['field_name']][$field_in_group], |
| 777 |
t('!name field is required.', |
t('!name field is required.', |
| 778 |
array('!name' => $form[$row['field_name']][$field_in_group]['#title']))); |
array('!name' => $form[$row['field_name']][$field_in_group]['#title']))); |
| 779 |
} |
} |
| 780 |
} |
} |
| 781 |
else { |
else { |
| 782 |
// Do not submit values of controlled fields which were not triggered (except on preview) |
// Do not submit values of controlled fields which were not triggered (except on preview) |
| 783 |
if (variable_get('c_fields_reset_default_' . $form['type']['#value'], 1) && |
if (variable_get('c_fields_reset_default_' . $form['type']['#value'], 1) && |
| 784 |
!in_array('node_form_build_preview', $form_state['submit_handlers']) && |
!in_array('node_form_build_preview', $form_state['submit_handlers']) && |
| 785 |
is_array($form['#field_info'][$field_in_group]['widget']['default_value'])) { |
is_array($form['#field_info'][$field_in_group]['widget']['default_value'])) { |
| 786 |
foreach ($form['#field_info'][$field_in_group]['widget']['default_value'] as $delta => $value) { |
foreach ($form['#field_info'][$field_in_group]['widget']['default_value'] as $delta => $value) { |
| 787 |
$form_state['values'][$field_in_group][$delta]['value'] = $value['value']; |
$form_state['values'][$field_in_group][$delta]['value'] = $value['value']; |
| 788 |
} |
} |
| 805 |
else if (isset($field[0]['nid'])) { |
else if (isset($field[0]['nid'])) { |
| 806 |
$value = $field[0]['nid']; |
$value = $field[0]['nid']; |
| 807 |
} |
} |
| 808 |
|
|
| 809 |
if (!count($value) || (is_string($value) && strlen(trim($value)) == 0)) { |
if (!count($value) || (is_string($value) && drupal_strlen(trim($value)) == 0)) { |
| 810 |
return TRUE; |
return TRUE; |
| 811 |
} |
} |
| 812 |
|
|
| 813 |
return FALSE; |
return FALSE; |
| 814 |
} |
} |
| 815 |
|
|
| 856 |
} |
} |
| 857 |
$data['flat'][$type] = array_unique($data['flat'][$type]); |
$data['flat'][$type] = array_unique($data['flat'][$type]); |
| 858 |
} |
} |
| 859 |
|
|
| 860 |
} |
} |
| 861 |
return $data[$structure][$type]; |
return $data[$structure][$type]; |
| 862 |
} |
} |
| 869 |
if (!$data = conditional_fields_load_data($form['#type_name'])) { |
if (!$data = conditional_fields_load_data($form['#type_name'])) { |
| 870 |
return; |
return; |
| 871 |
} |
} |
| 872 |
|
|
| 873 |
foreach ($data as $field) { |
foreach ($data as $field) { |
| 874 |
if (in_array($field['control_field_name'], $form['#fields'])) { |
if (in_array($field['control_field_name'], $form['#fields'])) { |
| 875 |
$form[$field['control_field_name']]['field_name']['#value'] .= theme('conditional_fields_manage_marker', NULL, $field['field_name']); |
$form[$field['control_field_name']]['field_name']['#value'] .= theme('conditional_fields_manage_marker', NULL, $field['field_name']); |
| 947 |
} |
} |
| 948 |
|
|
| 949 |
/** |
/** |
| 950 |
* Implementation of hook_node_type() |
* Implementation of hook_node_type(). |
| 951 |
*/ |
*/ |
| 952 |
function conditional_fields_node_type($op, $info) { |
function conditional_fields_node_type($op, $info) { |
| 953 |
switch ($op) { |
switch ($op) { |
| 965 |
*/ |
*/ |
| 966 |
function conditional_fields_node_type_update($info) { |
function conditional_fields_node_type_update($info) { |
| 967 |
if (isset($info->old_type) && $info->type != $info->old_type) { |
if (isset($info->old_type) && $info->type != $info->old_type) { |
| 968 |
|
|
| 969 |
db_query("UPDATE {conditional_fields} SET type = '%s' WHERE type ='%s'", $info->type, $info->old_type); |
db_query("UPDATE {conditional_fields} SET type = '%s' WHERE type ='%s'", $info->type, $info->old_type); |
| 970 |
|
|
| 971 |
// Update variables |
// Update variables |
| 972 |
db_query("UPDATE {variable} SET name = 'c_fields_js_%s' WHERE name ='c_fields_js_%s'", $info->type, $info->old_type); |
db_query("UPDATE {variable} SET name = 'c_fields_js_%s' WHERE name ='c_fields_js_%s'", $info->type, $info->old_type); |
| 975 |
db_query("UPDATE {variable} SET name = 'c_fields_reset_default_%s' WHERE name ='c_fields_reset_default_%s'", $info->type, $info->old_type); |
db_query("UPDATE {variable} SET name = 'c_fields_reset_default_%s' WHERE name ='c_fields_reset_default_%s'", $info->type, $info->old_type); |
| 976 |
db_query("UPDATE {variable} SET name = 'c_fields_show_all_%s' WHERE name ='c_fields_show_all_%s'", $info->type, $info->old_type); |
db_query("UPDATE {variable} SET name = 'c_fields_show_all_%s' WHERE name ='c_fields_show_all_%s'", $info->type, $info->old_type); |
| 977 |
db_query("UPDATE {variable} SET name = 'c_fields_view_%s' WHERE name ='c_fields_view_%s'", $info->type, $info->old_type); |
db_query("UPDATE {variable} SET name = 'c_fields_view_%s' WHERE name ='c_fields_view_%s'", $info->type, $info->old_type); |
| 978 |
db_query("UPDATE {variable} SET name = 'c_fields_edit_%s' WHERE name ='c_fields_edit_%s'", $info->type, $info->old_type); |
db_query("UPDATE {variable} SET name = 'c_fields_edit_%s' WHERE name ='c_fields_edit_%s'", $info->type, $info->old_type); |
| 979 |
cache_clear_all('variables', 'cache'); |
cache_clear_all('variables', 'cache'); |
| 980 |
} |
} |
| 981 |
} |
} |
| 997 |
} |
} |
| 998 |
|
|
| 999 |
/** |
/** |
| 1000 |
* Implementation of hook_content_fieldapi |
* Implementation of hook_content_fieldapi(). |
| 1001 |
*/ |
*/ |
| 1002 |
function conditional_fields_content_fieldapi($ops, $field) { |
function conditional_fields_content_fieldapi($ops, $field) { |
| 1003 |
// Handle deletion of fields |
// Handle deletion of fields |
| 1019 |
* Use this function with caution, as it doesn't check if the content type and the fields actually exist. |
* Use this function with caution, as it doesn't check if the content type and the fields actually exist. |
| 1020 |
*/ |
*/ |
| 1021 |
function conditional_fields_save_field($type_name, $controlled_field, $controlling_fields) { |
function conditional_fields_save_field($type_name, $controlled_field, $controlling_fields) { |
| 1022 |
foreach ($controlling_fields as $controlling_field => $trigger_values) { |
foreach ($controlling_fields as $controlling_field => $trigger_values) { |
| 1023 |
// If the row already exists |
// If the row already exists |
| 1024 |
if (db_result(db_query("SELECT COUNT(*) FROM {conditional_fields} WHERE control_field_name = '%s' AND field_name = '%s' AND type = '%s'", $controlling_field, $controlled_field, $type_name))) { |
if (db_result(db_query("SELECT COUNT(*) FROM {conditional_fields} WHERE control_field_name = '%s' AND field_name = '%s' AND type = '%s'", $controlling_field, $controlled_field, $type_name))) { |
| 1025 |
// If no value is set, delete the entry, else update it |
// If no value is set, delete the entry, else update it |
| 1035 |
if (!empty($trigger_values) && !$trigger_values['conditional_field_no_value']) { |
if (!empty($trigger_values) && !$trigger_values['conditional_field_no_value']) { |
| 1036 |
conditional_fields_insert_field($type_name, $controlled_field, $controlling_field, $trigger_values); |
conditional_fields_insert_field($type_name, $controlled_field, $controlling_field, $trigger_values); |
| 1037 |
} |
} |
| 1038 |
} |
} |
| 1039 |
} |
} |
| 1040 |
} |
} |
| 1041 |
|
|
| 1042 |
/** |
/** |
| 1063 |
/** |
/** |
| 1064 |
* Wrapper for conditional_fields_set_required_field_recurse |
* Wrapper for conditional_fields_set_required_field_recurse |
| 1065 |
*/ |
*/ |
| 1066 |
function conditional_fields_set_required_field($item) { |
function conditional_fields_set_required_field($item) { |
| 1067 |
conditional_fields_set_required_field_recurse($item); |
conditional_fields_set_required_field_recurse($item); |
| 1068 |
return $item; |
return $item; |
| 1069 |
}; |
}; |
| 1074 |
* indicates they are required when visible. |
* indicates they are required when visible. |
| 1075 |
*/ |
*/ |
| 1076 |
function conditional_fields_set_required_field_recurse(&$item) { |
function conditional_fields_set_required_field_recurse(&$item) { |
| 1077 |
$item['#required'] = TRUE; |
$item['#required'] = TRUE; |
| 1078 |
foreach (element_children($item) as $child) { |
foreach (element_children($item) as $child) { |
| 1079 |
conditional_fields_set_required_field_recurse($item[$child]); |
conditional_fields_set_required_field_recurse($item[$child]); |
| 1080 |
} |
} |
| 1081 |
} |
} |
| 1082 |
|
|
| 1083 |
/** |
/** |
| 1129 |
/* Avoid unnecessary divs in other kinds of fields */ |
/* Avoid unnecessary divs in other kinds of fields */ |
| 1130 |
$item['#prefix'] = '<div id="conditional-' . conditional_fields_form_clean_id($id) . '" class="conditional-field controlled-field">'; |
$item['#prefix'] = '<div id="conditional-' . conditional_fields_form_clean_id($id) . '" class="conditional-field controlled-field">'; |
| 1131 |
$item['#suffix'] = '</div>'; |
$item['#suffix'] = '</div>'; |
| 1132 |
return drupal_render($item); |
return drupal_render($item); |
| 1133 |
} |
} |
| 1134 |
/* Normal fields */ |
/* Normal fields */ |
| 1135 |
else { |
else { |
| 1150 |
$output .= t('<div class="description">Controlled by <em>@controlling</em></div>', array('@controlling' => $controlling)); |
$output .= t('<div class="description">Controlled by <em>@controlling</em></div>', array('@controlling' => $controlling)); |
| 1151 |
} |
} |
| 1152 |
if ($controlled) { |
if ($controlled) { |
| 1153 |
$output .= t('<div class="description">Controlling <em>@controlled</em></div>', array('@controlled' => $controlled)); |
$output .= t('<div class="description">Controlling <em>@controlled</em></div>', array('@controlled' => $controlled)); |
| 1154 |
} |
} |
| 1155 |
return $output; |
return $output; |
| 1156 |
} |
} |