You are here

public static function LocalizeFields::preElementValidate in Localize Fields 7

Custom element validator which performs no validation but translates the labels used in element validation by field types having an #element_validate function.

Examples of #element_validate implementations:

NB: number_field_widget_validate() checks for invalid chars.

Does not translate the instance' description.

For Date fields, Implementing hook_date_combo_pre_validate_alter() would _seem_ the right way to do this. But date_combo_validate() fetches instance properties (like labels) _before_ invoking the date_combo_pre_validate_alter hook. Thus changes in a hook_date_combo_pre_validate_alter() would have no effect.

Attaching this validator is another challenge, Date disregards for instance implementations of hook_element_info_alter(); Date simply overrides (resets) the info in date_field_widget_form(). So this custom validator is attached (prepended) in our hook_field_widget_form_alter() implementation instead.

Parameters

array $element:

array &$form_state:

See also

number_field_widget_validate()

1 call to LocalizeFields::preElementValidate()
localize_fields_pre_element_validate in ./localize_fields.module
Custom element validator which performs no validation but translates the labels used in element validation by field types having an #element_validate function.

File

./LocalizeFields.inc, line 757
Drupal Localize Fields module

Class

LocalizeFields
@file Drupal Localize Fields module

Code

public static function preElementValidate($element, &$form_state) {
  if (($localize = self::$localize) == -1) {

    // Save a method call if possible.
    $localize = self::localize();
  }
  if (!$localize) {
    return;
  }

  // Non-standard field types may have weird structure - let's not break
  // things further.
  if (!isset($element['#field_parents']) || empty($element['#field_name']) || empty($element['#language'])) {
    return;
  }
  $field_name = $element['#field_name'];

  // Change the element by changing its' properties in $form_state.
  $field_state = field_form_get_state($element['#field_parents'], $field_name, $element['#language'], $form_state);
  if (array_key_exists('instance', $field_state) && array_key_exists('label', $field_state['instance']) && ($source = $field_state['instance']['label'])) {
    $cntxtDelim = self::CONTEXT_DELIMITER;

    // Not escaped with check_plain() due to double encoding.
    $field_state['instance']['label'] = self::translateInternal($source, 'field_instance' . $cntxtDelim . $element['#bundle'] . self::CONTEXT_DELIMITER_BUNDLE . $field_name . $cntxtDelim . 'label', TRUE);
    field_form_set_state($element['#field_parents'], $field_name, $element['#language'], $form_state, $field_state);
  }
}