diff --git a/modules/entity_share_client/src/Entity/Remote.php b/modules/entity_share_client/src/Entity/Remote.php index a425d10..4c55716 100644 --- a/modules/entity_share_client/src/Entity/Remote.php +++ b/modules/entity_share_client/src/Entity/Remote.php @@ -74,4 +74,31 @@ class Remote extends ConfigEntityBase implements RemoteInterface { */ protected $basic_auth_password; + /** + * Returns the Remote ID + * + * @return string + */ + public function getId() { + return $this->id; + } + + /** + * Returns the Remote label + * + * @return string + */ + public function getLabel() { + return $this->label; + } + + /** + * Returns the Remote URL + * + * @return string + */ + public function getUrl() { + return $this->url; + } + } diff --git a/modules/entity_share_client/src/Event/EntityAlterEvent.php b/modules/entity_share_client/src/Event/EntityAlterEvent.php new file mode 100644 index 0000000..2bad1d7 --- /dev/null +++ b/modules/entity_share_client/src/Event/EntityAlterEvent.php @@ -0,0 +1,62 @@ +entity = $entity; + $this->remote = $remote; + } + + /** + * Returns the Entity object. + * @return EntityInterface + */ + public function getEntity() { + return $this->entity; + } + + /** + * @return \Drupal\entity_share_client\Entity\Remote + */ + public function getRemote(){ + return $this->remote; + } + + /** + * Sets the Entity object. + * @param EntityInterface $entity + */ + public function setEntity(EntityInterface $entity) { + $this->entity = $entity; + } + +} diff --git a/modules/entity_share_client/src/Event/EntityDataAlterEvent.php b/modules/entity_share_client/src/Event/EntityDataAlterEvent.php new file mode 100644 index 0000000..c704481 --- /dev/null +++ b/modules/entity_share_client/src/Event/EntityDataAlterEvent.php @@ -0,0 +1,61 @@ +entity_data = $entity_data; + $this->remote = $remote; + } + + /** + * Returns the Entities data. + * @return array + */ + public function getEntitiesData() { + return $this->entity_data; + } + + /** + * Sets the entities data. + * @param array $entity_data + */ + public function setEntitiesData(array $entity_data) { + $this->entity_data = $entity_data; + } + + + /** + * @return \Drupal\entity_share_client\Entity\RemoteInterface + */ + public function getRemote(){ + return $this->remote; + } + +} diff --git a/modules/entity_share_client/src/Event/EntityInsertEvent.php b/modules/entity_share_client/src/Event/EntityInsertEvent.php new file mode 100644 index 0000000..5f7481d --- /dev/null +++ b/modules/entity_share_client/src/Event/EntityInsertEvent.php @@ -0,0 +1,62 @@ +entity = $entity; + $this->remote = $remote; + } + + /** + * Returns the Entity object. + * @return EntityInterface + */ + public function getEntity() { + return $this->entity; + } + + /** + * @return \Drupal\entity_share_client\Entity\Remote + */ + public function getRemote(){ + return $this->remote; + } + + /** + * Sets the Entity object. + * @param EntityInterface $entity + */ + public function setEntity(EntityInterface $entity) { + $this->entity = $entity; + } + +} diff --git a/modules/entity_share_client/src/Service/JsonapiHelper.php b/modules/entity_share_client/src/Service/JsonapiHelper.php index 90e5e43..6d08008 100644 --- a/modules/entity_share_client/src/Service/JsonapiHelper.php +++ b/modules/entity_share_client/src/Service/JsonapiHelper.php @@ -22,6 +22,10 @@ use Drupal\jsonapi\ResourceType\ResourceTypeRepository; use GuzzleHttp\Exception\ClientException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Serializer\SerializerInterface; +use Drupal\entity_share_client\Event\EntityDataAlterEvent; +use Drupal\entity_share_client\Event\EntityInsertEvent; +use Drupal\entity_share_client\Event\EntityAlterEvent; + /** * Class JsonapiHelper. @@ -315,6 +319,11 @@ class JsonapiHelper implements JsonapiHelperInterface { * {@inheritdoc} */ public function importEntityListData(array $entity_list_data) { + // Allow other modules to alter the entity data with an EventSubscriber. + $event = new EntityDataAlterEvent($entity_list_data, $this->remote); + $this->eventDispatcher->dispatch(EntityDataAlterEvent::EVENT_NAME, $event); + $entity_list_data = $event->getEntitiesData(); + $imported_entity_ids = []; foreach ($this->prepareData($entity_list_data) as $entity_data) { $parsed_type = explode('--', $entity_data['type']); @@ -353,6 +362,10 @@ class JsonapiHelper implements JsonapiHelperInterface { // New entity. if (empty($existing_entities)) { + // Allow other modules to alter the entity with an EventSubscriber. + $event = new EntityInsertEvent($entity, $this->remote); + $this->eventDispatcher->dispatch(EntityInsertEvent::EVENT_NAME, $event); + $entity->save(); $imported_entity_ids[] = $entity->id(); // Prevent the entity of being reimported. @@ -390,12 +403,18 @@ class JsonapiHelper implements JsonapiHelperInterface { $entity->get($field_name)->getValue() ); } + // Allow other modules to alter the entity with an EventSubscriber. + $event = new EntityAlterEvent($existing_translation, $this->remote); + $this->eventDispatcher->dispatch(EntityAlterEvent::EVENT_NAME, $event); $existing_translation->save(); } // Create the new translation. else { $translation = $entity->toArray(); $existing_entity->addTranslation($data_langcode, $translation); + // Allow other modules to alter the entity translation with an EventSubscriber. + $event = new EntityAlterEvent($existing_entity->getTranslation($data_langcode), $this->remote); + $this->eventDispatcher->dispatch(EntityAlterEvent::EVENT_NAME, $event); $existing_entity->save(); $existing_translation = $existing_entity->getTranslation($data_langcode); }