You are here

public static function EntityAutocomplete::valueCallback in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  2. 8.2 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  3. 8.3 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  4. 8.4 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  5. 8.5 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  6. 8.6 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  7. 8.7 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  8. 8.8 modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  9. 10.3.x modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  10. 10.0.x modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  11. 10.1.x modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()
  12. 10.2.x modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php \Drupal\social_core\Entity\Element\EntityAutocomplete::valueCallback()

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 EntityAutocomplete::valueCallback

File

modules/social_features/social_core/src/Entity/Element/EntityAutocomplete.php, line 46

Class

EntityAutocomplete
Provides an entity autocomplete form element.

Namespace

Drupal\social_core\Entity\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  $hide_id = !empty($element['#selection_settings']['hide_id']);

  // Process the #default_value property.
  if ($input === FALSE && isset($element['#default_value']) && $element['#process_default_value']) {
    if (is_array($element['#default_value']) && $element['#tags'] !== TRUE) {
      throw new \InvalidArgumentException('The #default_value property is an array but the form element does not allow multiple values.');
    }
    elseif (!empty($element['#default_value']) && !is_array($element['#default_value'])) {

      // Convert the default value into an array for easier processing in
      // static::getEntityLabels().
      $element['#default_value'] = [
        $element['#default_value'],
      ];
    }
    if ($element['#default_value']) {
      if (!reset($element['#default_value']) instanceof EntityInterface) {
        throw new \InvalidArgumentException('The #default_value property has to be an entity object or an array of entity objects.');
      }

      // Extract the labels from the passed-in entity objects, taking access
      // checks into account.
      return static::getEntityLabels($element['#default_value'], $hide_id);
    }
  }

  // Potentially the #value is set directly, so it contains the 'target_id'
  // array structure instead of a string.
  if ($input !== FALSE && is_array($input)) {
    $entity_ids = array_map(function (array $item) {
      return $item['target_id'];
    }, $input);
    $entities = \Drupal::entityTypeManager()
      ->getStorage($element['#target_type'])
      ->loadMultiple($entity_ids);
    return static::getEntityLabels($entities, $hide_id);
  }
}