diff --git a/multiversion.module b/multiversion.module index 144be4d..17b521a 100644 --- a/multiversion.module +++ b/multiversion.module @@ -67,7 +67,7 @@ function multiversion_entity_type_alter(array &$entity_types) { $entity_type->setHandlerClass('storage', "$namespace\\MediaStorage"); $entity_type->setHandlerClass('storage_schema', "$namespace\\ContentEntityStorageSchema"); break; - + case 'paragraph': $entity_type->setHandlerClass('storage', "$namespace\\ParagraphsStorage"); $entity_type->setHandlerClass('storage_schema', "$namespace\\ParagraphsStorageSchema"); diff --git a/src/Entity/Storage/Sql/ParagraphsStorage.php b/src/Entity/Storage/Sql/ParagraphsStorage.php index 6032cfc..80235d1 100644 --- a/src/Entity/Storage/Sql/ParagraphsStorage.php +++ b/src/Entity/Storage/Sql/ParagraphsStorage.php @@ -13,6 +13,9 @@ class ParagraphsStorage extends SqlContentEntityStorage implements ContentEntity save as saveEntity; } + /** + * {@inheritdoc} + */ public function save(EntityInterface $entity) { $result = $this->saveEntity($entity); $this->updateRevIds($entity); @@ -20,10 +23,12 @@ public function save(EntityInterface $entity) { return $result; } - // change revision id the parent entity links to + /** + * Assign the revision ID to the value the parent entity uses. + */ private function updateRevIds($entity) { - // test if entity has parent - if($parent_entity = $entity->getParentEntity()){ + // Test if entity has parent. + if ($parent_entity = $entity->getParentEntity()){ $parent_entity_id = $parent_entity->id(); $parent_entity_rev_id = $parent_entity->getRevisionId(); $parent_entity_type = $parent_entity->getEntityTypeId(); @@ -32,23 +37,25 @@ private function updateRevIds($entity) { $entity_id = $entity->id(); $entity_rev_id = $entity->getRevisionId(); - if(!$parent_entity_id || - !$parent_entity_type || - !$parent_entity_field_name || - !$entity_id || - !$entity_rev_id) return NULL; + if (empty($parent_entity_id) + || empty($parent_entity_type) + || empty($parent_entity_field_name) + || empty($entity_id) + || empty($entity_rev_id)) { + return NULL; + } - // set newest revision id in parent field table + // Set newest revision ID in parent field table. $db = \Drupal::database(); - // address normal and revision tables + // Address normal and revision tables. foreach (['', '_revision'] as $table) { $query = $db ->update($parent_entity_type . $table . '__' . $parent_entity_field_name) ->fields(["{$parent_entity_field_name}_target_revision_id" => $entity_rev_id]) ->condition("{$parent_entity_field_name}_target_id", $entity_id); - if($table === '_revision') { + if ($table === '_revision') { $query ->condition('entity_id', $parent_entity_id) ->condition('revision_id', $parent_entity_rev_id); @@ -60,4 +67,5 @@ private function updateRevIds($entity) { \Drupal::service('cache_tags.invalidator')->invalidateTags((array) $parent_entity->getCacheTagsToInvalidate()); } } + } diff --git a/src/EntityReferenceRevisionsItem.php b/src/EntityReferenceRevisionsItem.php index 0f0f190..4d8bcc1 100644 --- a/src/EntityReferenceRevisionsItem.php +++ b/src/EntityReferenceRevisionsItem.php @@ -5,16 +5,16 @@ use \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem as BaseEntityReferenceRevisionsItem; class EntityReferenceRevisionsItem extends BaseEntityReferenceRevisionsItem { + /** * {@inheritdoc} */ public function preSave() { - if ($this->hasNewEntity()) { // As part of a bulk or replication operation there might be multiple // parent entities wanting to auto-create the same reference. So at this - // point this entity might already be saved, so we look it up by UUID and - // map it correctly. + // point this entity might already be saved, so look it up by UUID and map + // it correctly. // @see \Drupal\relaxed\BulkDocs\BulkDocs::save() if ($this->entity->isNew()) { $uuid = $this->entity->uuid(); @@ -25,7 +25,7 @@ public function preSave() { $entity_type_manager = \Drupal::service('entity_type.manager'); $entity_type_id = $this->entity->getEntityTypeId(); - // Now we have to decide what revision to use. + // Now decide what revision to use. $id_key = $entity_type_manager ->getDefinition($entity_type_id) ->getKey('id'); @@ -65,7 +65,6 @@ public function preSave() { $this->target_id = $this->entity->id(); $this->target_revision_id = $this->entity->getRevisionId(); } - } }