You are here

class EntityReferenceRevisionsRelationshipSubscriber 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
  2. 8.2 modules/entity_share_client/src/EventSubscriber/EntityReferenceRevisionsRelationshipSubscriber.php \Drupal\entity_share_client\EventSubscriber\EntityReferenceRevisionsRelationshipSubscriber

Class DynamicEntityReferenceRelationshipSubscriber.

@package Drupal\entity_share_client

Hierarchy

Expanded class hierarchy of EntityReferenceRevisionsRelationshipSubscriber

1 string reference to 'EntityReferenceRevisionsRelationshipSubscriber'
entity_share_client.services.yml in modules/entity_share_client/entity_share_client.services.yml
modules/entity_share_client/entity_share_client.services.yml
1 service uses EntityReferenceRevisionsRelationshipSubscriber
entity_share_client.entity_reference_revisions_relationship_subscriber in modules/entity_share_client/entity_share_client.services.yml
Drupal\entity_share_client\EventSubscriber\EntityReferenceRevisionsRelationshipSubscriber

File

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

Namespace

Drupal\entity_share_client\EventSubscriber
View source
class EntityReferenceRevisionsRelationshipSubscriber implements EventSubscriberInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager..
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[RelationshipFieldValueEvent::EVENT_NAME] = [
      'alterRelationshipValue',
      100,
    ];
    return $events;
  }

  /**
   * Set the revision target ID to the last revision of the entity.
   *
   * Last revision of the entity on the client site.
   *
   * @param \Drupal\entity_share_client\Event\RelationshipFieldValueEvent $event
   *   The event containing the field value.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceRevisionsRelationshipSubscriber::$entityTypeManager protected property The entity type manager.
EntityReferenceRevisionsRelationshipSubscriber::alterRelationshipValue public function Set the revision target ID to the last revision of the entity.
EntityReferenceRevisionsRelationshipSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityReferenceRevisionsRelationshipSubscriber::__construct public function Constructor.