function EntityReferenceUuidItem::onChange

Overrides EntityReferenceItem::onChange

1 call to EntityReferenceUuidItem::onChange()
EntityReferenceUuidItem::setValue in core/modules/jsonapi/tests/modules/jsonapi_test_field_type/src/Plugin/Field/FieldType/EntityReferenceUuidItem.php

File

core/modules/jsonapi/tests/modules/jsonapi_test_field_type/src/Plugin/Field/FieldType/EntityReferenceUuidItem.php, line 143

Class

EntityReferenceUuidItem
Defines the 'entity_reference_uuid' entity field type.

Namespace

Drupal\jsonapi_test_field_type\Plugin\Field\FieldType

Code

public function onChange($property_name, $notify = TRUE) : void {
  // Make sure that the target UUID and the target property stay in sync.
  if ($property_name === 'entity') {
    $property = $this->get('entity');
    if ($target_uuid = $property->isTargetNew() ? NULL : $property->getValue()
      ->uuid()) {
      $this->writePropertyValue('target_uuid', $target_uuid);
    }
  }
  elseif ($property_name === 'target_uuid') {
    $property = $this->get('entity');
    $entity_type = $property->getDataDefinition()
      ->getConstraint('EntityType');
    $entities = \Drupal::entityTypeManager()->getStorage($entity_type)
      ->loadByProperties([
      'uuid' => $this->get('target_uuid')
        ->getValue(),
    ]);
    if ($entity = array_shift($entities)) {
      assert($entity instanceof EntityInterface);
      $this->writePropertyValue('target_uuid', $entity->uuid());
      $this->writePropertyValue('entity', $entity);
    }
  }
  parent::onChange($property_name, $notify);
}

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