You are here

function conditional_fields_node_editing_form in Conditional Fields 5

Same name and namespace in other branches
  1. 6 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 454

Code

function conditional_fields_node_editing_form(&$form) {

  // Do nothing if we are in a content type administration page
  if ($form['#node']->cck_dummy_node_form == TRUE) {
    return;
  }
  $type_name = $form['#node']->type;
  $type = content_types($type_name);

  // Do nothing if there are no conditional fields
  if (!($data = conditional_fields_load_data($form['type']['#value']))) {
    return;
  }

  // Apply oprhaned fields settings
  switch (variable_get('c_fields_edit_' . $form['type']['#value'], C_FIELDS_ORPHANED_SHOW_TRIGGERED)) {
    case C_FIELDS_ORPHANED_SHOW_TRIGGERED:

      // We will only hide untriggered fields
      $show_triggered = TRUE;
    case C_FIELDS_ORPHANED_HIDE:

      // Hide controlled fields whose controlling field is not present
      foreach ($data as $field) {

        // Check if the controlling field is in a group
        if (module_exists('fieldgroup')) {
          $group = fieldgroup_get_group($form['type']['#value'], $field['control_field_name']);
        }
        else {
          $group = FALSE;
        }

        // Check if the controlling field is in form
        // If not, unset controlled field
        if ($group) {
          if (!$form[$group][$field['control_field_name']] || $form[$group][$field['control_field_name']]['#type'] == 'markup') {
            if (!$show_triggered || !in_array($form['#node']->{$field['control_field_name']}[0]['value'], $field['trigger_values'])) {
              unset($form[$group][$field['field_name']]);
            }
          }
        }
        else {
          if (!$form[$field['control_field_name']] || $form[$field['control_field_name']]['#type'] == 'markup') {
            if (!$show_triggered || !in_array($form['#node']->{$field['control_field_name']}[0]['value'], $field['trigger_values'])) {
              unset($form[$field['field_name']]);
            }
          }
        }
      }
      break;
    case C_FIELDS_ORPHANED_SHOW_ALL:

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

  // 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-' . form_clean_id($row['control_field_name'])]['#conditional-' . form_clean_id($row['field_name'])] = array(
      'field_id' => '#conditional-' . 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
  foreach (element_children($form) as $element) {

    // Fields
    if (substr($element, 0, 6) == 'field_') {
      if ($controlling_fields[$element]) {
        $form[$element]['#controlling_field'] = $element;
        $form[$element]['#theme'] = 'conditional_fields_form_item';
      }
      else {
        if ($controlled_fields[$element]) {
          if (conditional_fields_find_required_field($form[$element])) {
            $required_fields[$element] = array(
              'field' => $element,
            );
          }
          $form[$element]['#controlled_field'] = $element;
          $form[$element]['#theme'] = 'conditional_fields_form_item';
        }
      }
    }
    else {
      if (substr($element, 0, 6) == 'group_') {

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

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

        // Fields in groups
        foreach (element_children($form[$element]) as $group_element) {
          if ($controlled_fields[$element] || $controlled_fields[$group_element]) {
            if (conditional_fields_find_required_field($form[$element][$group_element])) {
              $required_fields[$group_element] = array(
                'field' => $group_element,
                'in_group' => $element,
              );
            }
          }
          if ($controlling_fields[$group_element]) {
            $form[$element][$group_element]['#controlling_field'] = $group_element;
            $form[$element][$group_element]['#theme'] = 'conditional_fields_form_item';
          }
          else {
            if ($controlled_fields[$group_element]) {
              $form[$element][$group_element]['#controlled_field'] = $group_element;
              $form[$element][$group_element]['#theme'] = 'conditional_fields_form_item';
            }
          }
        }
      }
    }
  }

  // Add extra validation funcion
  $form['#validate'] = array_merge(array(
    'conditional_fields_node_editing_form_validate' => array(
      $data,
      $required_fields,
    ),
  ), (array) $form['#validate']);

  // 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';
    case C_FIELDS_JS_HIDE:

      // Add javascript
      conditional_fields_add_js($settings);
      break;
  }
}