class EntityReferenceRevisionsRelationshipSubscriber in Entity Share 8.3
Same name and namespace in other branches
- 8 modules/entity_share_client/src/EventSubscriber/EntityReferenceRevisionsRelationshipSubscriber.php \Drupal\entity_share_client\EventSubscriber\EntityReferenceRevisionsRelationshipSubscriber
- 8.2 modules/entity_share_client/src/EventSubscriber/EntityReferenceRevisionsRelationshipSubscriber.php \Drupal\entity_share_client\EventSubscriber\EntityReferenceRevisionsRelationshipSubscriber
Allows entity reference revisions fields to be supported.
@package Drupal\entity_share_client
Hierarchy
- class \Drupal\entity_share_client\EventSubscriber\EntityReferenceRevisionsRelationshipSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
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
File
- modules/
entity_share_client/ src/ EventSubscriber/ EntityReferenceRevisionsRelationshipSubscriber.php, line 16
Namespace
Drupal\entity_share_client\EventSubscriberView 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 = [];
$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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityReferenceRevisionsRelationshipSubscriber:: |
protected | property | The entity type manager. | |
EntityReferenceRevisionsRelationshipSubscriber:: |
public | function | Set the revision target ID to the last revision of the entity. | |
EntityReferenceRevisionsRelationshipSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
EntityReferenceRevisionsRelationshipSubscriber:: |
public | function | Constructor. |