Project

General

Profile

Revision 76bdcd04

Added by Assos Assos almost 6 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/webform_validation/webform_validation.module
82 82
}
83 83

  
84 84
/**
85
 * Implements hook_form_alter().
85
 * Implements hook_form_BASE_FORM_ID_alter().
86 86
 */
87
function webform_validation_form_alter(&$form, &$form_state, $form_id) {
88
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {
89
    $form['#validate'][] = 'webform_validation_validate';
90
  }
87
function webform_validation_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
88
  $form['#validate'][] = 'webform_validation_validate';
91 89
}
92 90

  
93 91
/**
......
150 148
  }
151 149

  
152 150
  if ($rules) {
153
    $component_definitions = webform_validation_prefix_keys($node->webform['components']);
154

  
155 151
    // Remove hidden components.
156 152
    if (defined('WebformConditionals::componentShown')) {
157 153
      // New conditionals system.
......
161 157
      if (!$sorter->isExecuted()) {
162 158
        $sorter->executeConditionals(array(), 0);
163 159
      }
164
      foreach ($component_definitions as $key => $component) {
160
      foreach ($node->webform['components'] as $key => $component) {
165 161
        if ($sorter->componentVisibility($component['cid'], $component['page_num']) !== WebformConditionals::componentShown) {
166 162
          unset($flat_values[$key]);
167 163
        }
......
174 170
      if (!defined('WEBFORM_CONDITIONAL_INCLUDE')) {
175 171
        define('WEBFORM_CONDITIONAL_INCLUDE', 1);
176 172
      }
177
      foreach ($component_definitions as $key => $component) {
173
      foreach ($node->webform['components'] as $key => $component) {
178 174
        // In Webform 7.x-3.x, _webform_client_form_rule_check() returns
179 175
        // boolean.
180 176
        // Cast to int so that the function behaves as it does in 7.x-4.x.
......
193 189
          $items[$cid] = $flat_values[$cid];
194 190
        }
195 191
      }
196
      // Prefix array keys to avoid reindexing by the module_invoke_all function
197
      // call.
198
      $items = webform_validation_prefix_keys($items);
199 192
      $rule['sid'] = $sid;
200 193
      // Have the submitted values validated.
201
      $errors = module_invoke_all("webform_validation_validate", $rule['validator'], $items, $component_definitions, $rule);
194
      $errors = module_invoke_all("webform_validation_validate", $rule['validator'], webform_validation_prefix_keys($items), webform_validation_prefix_keys($node->webform['components']), $rule);
202 195
      if ($errors) {
203 196
        $errors = webform_validation_unprefix_keys($errors);
204
        $components = webform_validation_unprefix_keys($component_definitions);
197
        // Create hook_webform_validation_validate_alter(). Allow other modules
198
        // to alter error messages.
199
        $context = array(
200
          'validator_name' => $rule['validator'],
201
          'items' => $items,
202
          'components' => $node->webform['components'],
203
          'rule' => $rule,
204
        );
205
        drupal_alter('webform_validation_validate', $errors, $context);
206

  
205 207
        foreach ($errors as $item_key => $error) {
206 208
          // Do not set error message if an identical message has already been
207 209
          // set.
......
212 214

  
213 215
          // Build the proper form element error key, taking into account
214 216
          // hierarchy.
215
          $error_key = 'submitted][' . webform_validation_parent_tree($item_key, $components) . $components[$item_key]['form_key'];
217
          $error_key = 'submitted][' . webform_validation_parent_tree($item_key, $node->webform['components']) . $node->webform['components'][$item_key]['form_key'];
216 218
          if (is_array($error)) {
217 219
            foreach ($error as $sub_item_key => $sub_error) {
218 220
              form_set_error($error_key . '][' . $sub_item_key, $sub_error);
......
256 258
}
257 259

  
258 260
/**
259
 * Recursively add the parents for the element, to be used as first argument to form_set_error.
261
 * Recursively add the parents for the element.
262
 *
263
 * These are used as the first argument to form_set_error().
260 264
 */
261 265
function webform_validation_parent_tree($cid, $components) {
262 266
  $output = '';
......
283 287
}
284 288

  
285 289
/**
286
 * Prefix numeric array keys to avoid them being reindexed by module_invoke_all.
290
 * Prefix numeric array keys to avoid them being reindexed.
291
 *
292
 * Reindexing done in module_invoke_all().
293
 *
294
 * Opposite of webform_validation_unprefix_keys().
287 295
 */
288 296
function webform_validation_prefix_keys($arr) {
289 297
  $ret = array();
......
294 302
}
295 303

  
296 304
/**
297
 * Undo prefixing numeric array keys to avoid them being reindexed by module_invoke_all.
305
 * Undo prefixing numeric array keys.
306
 *
307
 * Opposite of webform_validation_prefix_keys().
298 308
 */
299 309
function webform_validation_unprefix_keys($arr) {
300 310
  $ret = array();
......
323 333
    $output = '<h3>' . t('Add a validation rule') . '</h3>';
324 334
    $output .= '<dl>';
325 335
    foreach ($validators as $validator_key => $validator_info) {
326
      $url = 'node/' . $nid . '/webform/validation/add/' . $validator_key;
327
      if (array_intersect($types, $validator_info['component_types'])) {
328
        $title = l($validator_info['name'], $url, array('query' => drupal_get_destination()));
336
      $validator_types = webform_validation_valid_component_types($validator_key);
337
      $title = $validator_info['name'];
338
      if (array_intersect($types, $validator_types)) {
339
        $url = 'node/' . $nid . '/webform/validation/add/' . $validator_key;
340
        $title = l($title, $url, array('query' => drupal_get_destination()));
329 341
        $component_list_postfix = '';
330 342
      }
331 343
      else {
332
        $title = $validator_info['name'];
333 344
        $component_list_postfix = '; ' . t('none present in this form');
334 345
      }
335 346
      $item = '<dt>' . $title . '</dt>';
336 347
      $item .= '<dd>';
337 348
      $item .= $validator_info['description'];
338
      $item .= ' ' . t('Works with: @component_types.', array('@component_types' => implode(', ', $validator_info['component_types']) . $component_list_postfix)) . '</dd>';
349
      $item .= ' ' . t('Works with: @component_types.', array('@component_types' => implode(', ', $validator_types) . $component_list_postfix)) . '</dd>';
339 350
      $output .= $item;
340 351
    }
341 352
    $output .= '</dl>';

Also available in: Unified diff