You are here

public static function SynonymsEntitySelect::valueCallback in Synonyms 2.0.x

Determines how user input is mapped to an element's #value property.

Parameters

array $element: An associative array containing the properties of the element.

mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

mixed The value to assign to the element.

Overrides Select::valueCallback

File

modules/synonyms_select/src/Element/SynonymsEntitySelect.php, line 40

Class

SynonymsEntitySelect
Form element for synonyms-friendly entity select.

Namespace

Drupal\synonyms_select\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  $return = parent::valueCallback($element, $input, $form_state);
  if (is_null($return) && isset($element['#default_value'])) {
    $return = $element['#default_value'];
  }

  // Force default value (entity ID(-s)) to be strings. Otherwise we are
  // hitting the situation when all synonyms are highlighted as selected.
  // This code snippet explains the problem:
  // $a = [25];
  // $k = '25:25';
  // in_array($k, $a); // Yields TRUE, because PHP seems to compare
  // int to int and not string-wise.
  if (is_array($return)) {
    $return = array_map(function ($item) {
      return (string) $item;
    }, $return);
  }
  elseif (!is_null($return)) {
    $return = (string) $return;
  }
  return $return;
}