You are here

public function DynamicEntityReferenceItem::onChange in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php \Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem::onChange()

Parameters

bool $notify: (optional) Whether to forward the notification to the parent. Defaults to TRUE. By passing FALSE, overrides of this method can re-use the logic of parent classes without triggering notification.

Overrides EntityReferenceItem::onChange

1 call to DynamicEntityReferenceItem::onChange()
DynamicEntityReferenceItem::setValue in src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php
Sets the data value.

File

src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php, line 125

Class

DynamicEntityReferenceItem
Defines the 'dynamic_entity_reference' entity field type.

Namespace

Drupal\dynamic_entity_reference\Plugin\Field\FieldType

Code

public function onChange($property_name, $notify = TRUE) {

  /** @var \Drupal\dynamic_entity_reference\Plugin\DataType\DynamicEntityReference $entity_property */
  $entity_property = $this
    ->get('entity');
  if ($property_name == 'target_type' && !$entity_property
    ->getValue()) {
    $entity_property
      ->getTargetDefinition()
      ->setEntityTypeId($this
      ->get('target_type')
      ->getValue());
  }
  elseif ($property_name == 'entity') {
    $this
      ->writePropertyValue('target_type', $entity_property
      ->getValue()
      ->getEntityTypeId());
  }
  elseif ($property_name == 'target_id') {

    // Just in case target_id is set before target_type then set it to empty
    // string instead of NULL so that
    // \Drupal\Core\Entity\Plugin\DataType\EntityReference::setValue
    // doesn't throw "InvalidArgumentException: Value is not a valid entity".
    $entity_property
      ->getTargetDefinition()
      ->setEntityTypeId($this
      ->get('target_type')
      ->getValue() ?: '');
  }
  parent::onChange($property_name, $notify);
}