You are here

public function EntityReferenceRevisionsRelationshipSubscriber::alterRelationshipValue in Entity Share 8

Same name and namespace in other branches
  1. 8.3 modules/entity_share_client/src/EventSubscriber/EntityReferenceRevisionsRelationshipSubscriber.php \Drupal\entity_share_client\EventSubscriber\EntityReferenceRevisionsRelationshipSubscriber::alterRelationshipValue()
  2. 8.2 modules/entity_share_client/src/EventSubscriber/EntityReferenceRevisionsRelationshipSubscriber.php \Drupal\entity_share_client\EventSubscriber\EntityReferenceRevisionsRelationshipSubscriber::alterRelationshipValue()

Set the revision target ID to the last revision of the entity.

Last revision of the entity on the client site.

Parameters

\Drupal\entity_share_client\Event\RelationshipFieldValueEvent $event: The event containing the field value.

File

modules/entity_share_client/src/EventSubscriber/EntityReferenceRevisionsRelationshipSubscriber.php, line 49

Class

EntityReferenceRevisionsRelationshipSubscriber
Class DynamicEntityReferenceRelationshipSubscriber.

Namespace

Drupal\entity_share_client\EventSubscriber

Code

public function alterRelationshipValue(RelationshipFieldValueEvent $event) {
  $field = $event
    ->getField();
  $field_type = $field
    ->getFieldDefinition()
    ->getType();
  if ($field_type == 'entity_reference_revisions') {
    $field_storage_definition = $field
      ->getFieldDefinition()
      ->getFieldStorageDefinition();
    $entity_type = $field_storage_definition
      ->getSetting('target_type');
    $main_property = $field
      ->getItemDefinition()
      ->getMainPropertyName();
    $field_value = $event
      ->getFieldValue();

    /** @var \Drupal\Core\Entity\ContentEntityInterface $referenced_entity */
    $referenced_entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->load($field_value[$main_property]);
    $last_revision_id = $referenced_entity
      ->getRevisionId();
    $field_value['target_revision_id'] = $last_revision_id;
    $event
      ->setFieldValue($field_value);
  }
}