You are here

function conditional_fields_dependency_edit_form_submit in Conditional Fields 7.3

Dependency edit form submit.

See also

conditional_fields_dependency_edit_form()

File

includes/conditional_fields.admin.inc, line 746
Administration of dependencies.

Code

function conditional_fields_dependency_edit_form_submit($form, &$form_state) {
  $dependee = $form_state['values']['dependee'];
  $widget_type = $form_state['field'][$dependee][LANGUAGE_NONE]['instance']['widget']['type'];

  // TODO: validate values against allowed values.
  // TODO: unify 'value' and 'values' fields.
  $dependency = array(
    'id' => $form['#dependency']['id'],
    'dependee' => $form['#dependency']['dependee'],
    'dependent' => $form['#dependency']['dependent'],
    'options' => array(
      'state' => $form_state['values']['state'],
      'condition' => $form_state['values']['condition'],
      'grouping' => $form_state['values']['grouping'],
      'effect' => isset($form_state['values']['effect']) ? $form_state['values']['effect'] : FALSE,
      'effect_options' => isset($form_state['values']['effect'], $form_state['values']['effect_options'][$form_state['values']['effect']]) ? $form_state['values']['effect_options'][$form_state['values']['effect']] : array(),
      'element_view' => $form_state['values']['element_view'],
      'element_view_per_role' => $form_state['values']['element_view_per_role'],
      'element_view_roles' => $form_state['values']['element_view_roles'],
      'element_edit' => $form_state['values']['element_edit'],
      'element_edit_per_role' => $form_state['values']['element_edit_per_role'],
      'element_edit_roles' => $form_state['values']['element_edit_roles'],
      'selector' => $form_state['values']['selector'],
    ),
  );

  // Store values only if the condition requires them.
  if ($form_state['values']['condition'] == 'value') {
    $dependency['options'] += array(
      'values_set' => $form_state['values']['values_set'],
      // Raw user input is needed to build actual value conditions in the
      // #states array, so we store it in 'value_form'.
      'value_form' => $form_state['input']['value']['field'][$dependee][LANGUAGE_NONE],
    );
    if ($dependency['options']['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET) {
      $value = array();
      field_default_extract_form_values(NULL, NULL, array(
        'field_name' => $dependee,
      ), NULL, LANGUAGE_NONE, $value, $form['value']['field'], $form_state);
      $dependency['options']['value'] = $value;
    }
    elseif ($dependency['options']['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX) {

      // The regular expression is inserted in an associative array, so it will
      // be converted to a Javascript object.
      // In states_enhancements.js we will then use the "RegExp" key to
      // distinguish the object and evaluate it as a regular expression.
      $dependency['options']['value'] = array(
        'RegExp' => $form_state['values']['regex'],
      );
    }
    else {
      $values = explode("\n", $form_state['values']['values']);
      $values = array_map('trim', $values);
      $values = array_filter($values, 'strlen');
      $dependency['options']['values'] = $values;
    }
  }
  conditional_fields_dependency_update($dependency);
  drupal_set_message(t('Saved dependency configuration.'));
}