function EntityReferenceItem::preSave

Overrides FieldItemBase::preSave

1 call to EntityReferenceItem::preSave()
ImageItem::preSave in core/modules/image/src/Plugin/Field/FieldType/ImageItem.php
Defines custom presave behavior for field values.
1 method overrides EntityReferenceItem::preSave()
ImageItem::preSave in core/modules/image/src/Plugin/Field/FieldType/ImageItem.php
Defines custom presave behavior for field values.

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 256

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function preSave() {
  if ($this->hasNewEntity()) {
    // Save the entity if it has not already been saved by some other code.
    if ($this->entity
      ->isNew()) {
      $this->entity
        ->save();
    }
    // Make sure the parent knows we are updating this property so it can
    // react properly.
    $this->target_id = $this->entity
      ->id();
  }
  if (!$this->isEmpty() && $this->target_id === NULL) {
    $this->target_id = $this->entity
      ->id();
  }
}

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