Commit 7b3c2ed1 authored by Jose Reyero's avatar Jose Reyero
Browse files

Issue #2173083 by Jose Reyero: Add submit callback and better validate...

Issue #2173083 by Jose Reyero: Add submit callback and better validate callback for variables in form.
parent a2080f3e
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -153,6 +153,23 @@ function variable_form_element_options($variable, $options = array()) {
  return $element;
}

/**
 * Execute submit callbacks for variables in form.
 */
function variable_form_submit_callback($form, &$form_state) {
  if (isset($form['#variable_edit_form'])) {
    // This may contain some realm options.
    $options = isset($form['#variable_options']) ? $form['#variable_options'] : array();
    foreach ($form['#variable_edit_form'] as $name) {
      $variable = variable_get_info($name);
      if ($variable && isset($variable['submit callback'])) {
        variable_include($variable);
        $variable['submit callback']($variable, $options, $form, $form_state);
      }
    }
  }
}

/**
 * Form to select variables
 */
+30 −6
Original line number Diff line number Diff line
@@ -415,16 +415,35 @@ function variable_hook_info() {
 * Form for variable list
 *
 * @param $list
 *   Variable name or list of variable names
 *   Variable name or list of variable names.
 * @param $options
 *   Optional array with variable options.
 */
function variable_edit_form($form, $form_state, $list, $options = array()) {
  // Pass on the values on the form for further reference.
  $form['#variable_edit_form'] = $list;
  form_load_include($form_state, 'form.inc', 'variable');
function variable_edit_form($form, &$form_state, $list, $options = array()) {
  $list = is_array($list) ? $list : array($list);
  $form = variable_base_form($form, $form_state, $list, $options);
  $form += variable_edit_subform($list, $options);
  return variable_settings_form($form, $options);
}

/**
 * Build base form for variable list without fields.
 *
 * @param $list
 *   List of variable names.
 * @param $options
 *   Optional array with variable options.
 */
function variable_base_form($form, &$form_state, $list, $options = array()) {
  form_load_include($form_state, 'form.inc', 'variable');
  // Pass on the values on the form for further reference.
  $form['#variable_edit_form'] = $list;
  $form['#variable_options'] = $options;
  // Run submit callback for variables in form.
  $form['#submit'][] = 'variable_form_submit_callback';
  return $form;
}

/**
 * Form elements for variable list.
 *
@@ -688,10 +707,14 @@ function variable_form_alter(&$form, &$form_state, $form_id) {
 * This needs to be in the module as it may be needed by form ajax callbacks.
 */
function variable_form_element_validate($element, &$form_state, $form) {
  $options = isset($form['#variable_options']) ? $form['#variable_options'] : array();
  $variable = $element['#variable'];
  variable_include($variable);
  $variable['value'] = isset($element['#value']) ? $element['#value'] : NULL;
  if ($error = call_user_func($variable['validate callback'], $variable)) {

  $error = $variable['validate callback']($variable, $options, $element, $form, $form_state);

  if ($error) {
    form_error($element, $error);
  }
}
@@ -777,6 +800,7 @@ function variable_settings_form_submit($form, &$form_state) {
  form_state_values_clean($form_state);
  // This may contain some realm options.
  $options = isset($form['#variable_options']) ? $form['#variable_options'] : array();

  // Now run regular settings submission but using variable_set_value()
  foreach ($form_state['values'] as $key => $value) {
    if (is_array($value) && isset($form_state['values']['array_filter'])) {
+3 −0
Original line number Diff line number Diff line
@@ -86,7 +86,10 @@ function variable_realm_edit_variables_form($form, &$form_state, $realm_name, $r
  $form['realm_name'] = array('#type' => 'value', '#value' => $realm_name);
  $form['realm_key'] = array('#type' => 'value', '#value' => $realm_key);
  $options['realm'] = variable_realm($realm_name, $realm_key);

  if ($variable_list = $controller->getEnabledVariables()) {
    $form = variable_base_form($form, $form_state, $variable_list, $options);

    // Group variables by variable group for vertical tabls
    $group_list = array();
    foreach ($variable_list as $variable_name) {