You are here

public function EntityAutocomplete::setDefaultValue in YAML Form 8

Set an element's default value using saved data.

Parameters

array $element: An element.

Overrides YamlFormElementBase::setDefaultValue

File

src/Plugin/YamlFormElement/EntityAutocomplete.php, line 39

Class

EntityAutocomplete
Provides a 'entity_autocomplete' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function setDefaultValue(array &$element) {
  if (isset($element['#default_value']) && (!empty($element['#default_value']) || $element['#default_value'] === 0)) {
    $target_storage = $this->entityTypeManager
      ->getStorage($element['#target_type']);
    if ($this
      ->hasMultipleValues($element)) {
      $entity_ids = $this
        ->getTargetEntityIds($element['#default_value']);
      $element['#default_value'] = $entity_ids ? $target_storage
        ->loadMultiple($entity_ids) : [];
    }
    else {
      $element['#default_value'] = $target_storage
        ->load($element['#default_value']) ?: NULL;
    }
  }
  else {
    $element['#default_value'] = NULL;
  }
}