function EntityReferenceFieldItemList::defaultValuesFormSubmit

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php \Drupal\Core\Field\EntityReferenceFieldItemList::defaultValuesFormSubmit()
  2. 10 core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php \Drupal\Core\Field\EntityReferenceFieldItemList::defaultValuesFormSubmit()
  3. 11.x core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php \Drupal\Core\Field\EntityReferenceFieldItemList::defaultValuesFormSubmit()

Overrides FieldItemList::defaultValuesFormSubmit

File

core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php, line 107

Class

EntityReferenceFieldItemList
Defines an item list class for entity reference fields.

Namespace

Drupal\Core\Field

Code

public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
    $default_value = parent::defaultValuesFormSubmit($element, $form, $form_state);
    // Convert numeric IDs to UUIDs to ensure config deployability.
    $ids = [];
    foreach ($default_value as $delta => $properties) {
        if (isset($properties['entity']) && $properties['entity']->isNew()) {
            // This may be a newly created term.
            $properties['entity']->save();
            $default_value[$delta]['target_id'] = $properties['entity']->id();
            unset($default_value[$delta]['entity']);
        }
        $ids[] = $default_value[$delta]['target_id'];
    }
    $entities = \Drupal::entityTypeManager()->getStorage($this->getSetting('target_type'))
        ->loadMultiple($ids);
    foreach ($default_value as $delta => $properties) {
        unset($default_value[$delta]['target_id']);
        $default_value[$delta]['target_uuid'] = $entities[$properties['target_id']]->uuid();
    }
    return $default_value;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.