You are here

function fapi_validation_filters_execute in Form API Validation 8

Same name and namespace in other branches
  1. 6 fapi_validation.module \fapi_validation_filters_execute()
  2. 7.2 fapi_validation.module \fapi_validation_filters_execute()
  3. 7 fapi_validation.module \fapi_validation_filters_execute()

Function for executing all filters

1 call to fapi_validation_filters_execute()
fapi_validate_element_filter in ./fapi_validation.module
Run element filter callbacks.

File

./fapi_validation.module, line 64
Form API validation module

Code

function fapi_validation_filters_execute(&$element, FormStateInterface $form_state) {
  $available_filters = _fapi_validation_data('filters');

  // If the element has no value, there is nothing to filter.
  if (!isset($element['#value'])) {
    return;
  }

  // Loop through all passed in filters from form element.
  foreach ($element['#filters'] as $element_filter) {

    // Make sure filter passed is available as a recognized filter by FAPI validation.
    if ($element_filter && !empty($available_filters[$element_filter])) {

      // Adjust element value based on calling the appropriate data filter callback.
      $element['#value'] = $available_filters[$element_filter]['callback']($element['#value']);
      $form_state
        ->setValue($element['#name'], $element['#value']);
    }
    else {
      drupal_set_message(t('Form item filter array with wrong structure on %field.', array(
        '%field' => $element['#name'],
      )), 'error');
    }
  }
}