You are here

public static function AutocompleteDeluxeElement::afterBuild in Autocomplete Deluxe 2.0.x

Same name and namespace in other branches
  1. 8 src/Element/AutocompleteDeluxeElement.php \Drupal\autocomplete_deluxe\Element\AutocompleteDeluxeElement::afterBuild()

Form API after build callback for the duration parameter type form.

Fixes up the form value by applying the multiplier.

File

src/Element/AutocompleteDeluxeElement.php, line 181

Class

AutocompleteDeluxeElement
Provides an Autocomplete Deluxe Form API element.

Namespace

Drupal\autocomplete_deluxe\Element

Code

public static function afterBuild(array $element, FormStateInterface $form_state) {

  // By default Drupal sets the maxlength to 128 if the property isn't
  // specified, but since the limit isn't useful in some cases,
  // we unset the property.
  unset($element['textfield']['#maxlength']);

  // Set the elements value from either the value field or text field input.
  $element['#value'] = isset($element['value_field']) ? $element['value_field']['#value'] : $element['textfield']['#value'];
  if (isset($element['value_field'])) {
    $element['#value'] = trim($element['#value']);

    // Replace all cases of double double quotes and one or more spaces with a
    // comma. This will allow us to keep entries in double quotes.
    $element['#value'] = preg_replace('/"" +""/', ',', $element['#value']);

    // Remove the double quotes at the beginning and the end from the first
    // and the last term.
    $element['#value'] = substr($element['#value'], 2, strlen($element['#value']) - 4);
    unset($element['value_field']['#maxlength']);
  }
  $form_state
    ->setValueForElement($element, $element['#value']);
  return $element;
}