You are here

function conditional_fields_node_editing_form in Conditional Fields 6

Same name and namespace in other branches
  1. 5 conditional_fields.module \conditional_fields_node_editing_form()

Alteration of the node editing form

1 call to conditional_fields_node_editing_form()
conditional_fields_form_alter in ./conditional_fields.module
Implementation of hook_form_alter().

File

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

Code

function conditional_fields_node_editing_form(&$form, $form_state) {
  $type_name = $form['type']['#value'];

  // Do nothing if there are no conditional fields
  if (!($data = conditional_fields_load_data($type_name))) {
    return;
  }

  // Remove from data fields that user can't edit
  // and apply orphaned fields settings
  $orphaned_settings = variable_get('c_fields_edit_' . $type_name, C_FIELDS_ORPHANED_SHOW_TRIGGERED);
  foreach ($data as $key => $field) {

    // Store group name or FALSE if no group
    $field['in_group'] = conditional_fields_get_group($type_name, $field['field_name']);

    // First check access
    if (!$field['in_group'] && $form[$field['field_name']]['#access'] === FALSE || $field['in_group'] && $form[$field['in_group']][$field['field_name']]['#access'] == FALSE) {
      unset($data[$key]);
      continue;
    }

    // Check if controlling field is present. If not, apply orphaned fields settings
    $show_triggered = FALSE;
    switch ($orphaned_settings) {
      case C_FIELDS_ORPHANED_SHOW_TRIGGERED:

        // We will only hide untriggered fields
        // E.g.: fields whose controlling fields have a triggering default value
        // or a triggering value set by other users with permissions
        $show_triggered = TRUE;
      case C_FIELDS_ORPHANED_HIDE:

        // Check if the controlling field is in a group
        $field['control_field_in_group'] = conditional_fields_get_group($type_name, $field['control_field_name']);

        // Check if the controlling field is in form
        // If not, unset controlled field
        if ($field['control_field_in_group']) {
          if (!$form[$field['control_field_in_group']][$field['control_field_name']] || $form[$field['control_field_in_group']][$field['control_field_name']]['#type'] == 'markup' || $form[$field['control_field_in_group']][$field['control_field_name']]['#access'] == FALSE) {
            if (!$show_triggered || !conditional_fields_is_triggered($form[$field['control_field_name']]['#value'], $field['trigger_values'])) {
              unset($form[$field['control_field_in_group']][$field['field_name']]);
              unset($data[$key]);
            }
          }
        }
        else {
          if (!$form[$field['control_field_name']] || $form[$field['control_field_name']]['#type'] == 'markup' || $form[$field['control_field_name']]['#access'] == FALSE) {
            if (!$show_triggered || !conditional_fields_is_triggered($form[$field['control_field_name']]['#value'], $field['trigger_values'])) {
              unset($form[$field['field_name']]);
              unset($data[$key]);
            }
          }
        }
        break;
      case C_FIELDS_ORPHANED_SHOW_ALL:

        // Do nothing: the default behavior is ok
        break;
    }
  }

  // Data could be empty by now
  if (empty($data)) {
    return;
  }

  // We build a javascript variable:
  // - 'controlling_fields' -> An object contaninig all ids of controlling fields, with their controlled fields and groups
  // To do: look if we should make this themeable
  foreach ($data as $row) {

    // Add javascript settings for this field
    $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'],
    );

    // To do: feature, add an array of controlled fields to js to allow for multiple controlling fields for a field.
    // Build helper arrays
    $controlling_fields[$row['control_field_name']] = $row['control_field_name'];
    $controlled_fields[$row['field_name']] = $row['field_name'];
  }

  // Controlled fields and fields inside controlled groups should only be required when user triggers them.
  // Since required input check is hardcoded in _form_validate, we need to unset it here.
  // We will check triggered fields in a custom validation form.
  // Here we also add enclosing divs for easier javascript handling to controlling fields and to controlled fields and groups
  $required_fields = array();
  foreach (element_children($form) as $element) {

    // Fields
    if (strpos($element, 'field_') === 0) {
      if ($form[$element]['#theme'] == 'content_multiple_values') {
        $form[$element]['#conditional_fields_multiple'] = TRUE;
      }
      if ($controlling_fields[$element]) {
        $form[$element]['#controlling_field'] = $element;
        $form[$element]['#theme'] = array(
          'conditional_fields_form_item',
        );
      }
      elseif ($controlled_fields[$element]) {
        if ($form[$element]['#required']) {
          conditional_fields_unset_required_field($form[$element]);
          $required_fields[$element] = array(
            'field' => $element,
          );
        }
        $form[$element]['#controlled_field'] = $element;
        $form[$element]['#theme'] = array(
          'conditional_fields_form_item',
        );
      }
    }
    elseif (strpos($element, 'group_') === 0) {

      // Groups
      if ($controlled_fields[$element]) {

        // Group markup is still hardcoded.
        $form[$element]['#prefix'] = '<div id="conditional-' . conditional_fields_form_clean_id($element) . '" class="conditional-field controlled-field">' . $form[$element]['#prefix'];
        $form[$element]['#suffix'] = $form[$element]['#suffix'] . '</div>';
      }

      // Fields in groups
      foreach (element_children($form[$element]) as $group_element) {

        // All required fields inside a conditional group must be handled by conditional fields
        if ($controlled_fields[$element] && $form[$element][$group_element]['#required']) {
          conditional_fields_unset_required_field($form[$element][$group_element]);
          $required_fields[$group_element] = array(
            'field' => $group_element,
            'in_group' => $element,
          );
          $form[$element][$group_element]['#theme'] = array(
            'conditional_fields_form_item',
          );
        }

        // Manage also conditional fields inside normal groups
        if ($controlling_fields[$group_element]) {
          $form[$element][$group_element]['#controlling_field'] = $group_element;
          $form[$element][$group_element]['#theme'] = array(
            'conditional_fields_form_item',
          );
        }
        elseif ($controlled_fields[$group_element]) {

          // Manage multiple ahah fields
          if ($form[$element][$group_element]['#theme'] == 'content_multiple_values') {
            $form[$element][$group_element]['#conditional_fields_multiple'] = TRUE;
          }
          if ($form[$element][$group_element]['#required']) {
            conditional_fields_unset_required_field($form[$element][$group_element]);
            $required_fields[$group_element] = array(
              'field' => $group_element,
              'in_group' => $element,
            );
          }
          $form[$element][$group_element]['#controlled_field'] = $group_element;
          $form[$element][$group_element]['#theme'] = array(
            'conditional_fields_form_item',
          );
        }
      }
    }
  }

  // Apply user interface settings
  $ui_settings = variable_get('c_fields_js_' . $type_name, C_FIELDS_JS_HIDE);
  switch ($ui_settings) {
    case C_FIELDS_JS_DISABLE:
      $settings['ui_settings'] = 'disable';
      break;
    case C_FIELDS_JS_HIDE:
      $settings['ui_settings']['animation'] = (int) variable_get('c_fields_animation_' . $type_name, C_FIELDS_ANIMATION_NO);
      $settings['ui_settings']['anim_speed'] = variable_get('c_fields_anim_speed_' . $type_name, "normal");
      break;
  }
  if ($ui_settings != C_FIELDS_JS_NO) {
    conditional_fields_add_js($settings);
  }

  // Pass variables for validation
  $form['#conditional_fields']['data'] = $data;
  $form['#conditional_fields']['required_fields'] = $required_fields;
  $form['#conditional_fields']['settings'] = $settings;

  // Add extra validation function
  $form['#validate'] = array_merge(array(
    'conditional_fields_node_editing_form_validate',
  ), (array) $form['#validate']);
}