Issue #2698425: Do not reimport existing entities
Issue #2698425 by kalpaitch, das-peter, jts86, andreyks, andypost, timmillwood, Karsa, robert-os, RumyanaRuseva, lammensj, e0ipso, morsok, r.nabiullin, Yaron Tal, plopesc, jamedina97, diegodalr3, robpowell, saramato, larowlan, eelkeblok, Manuel Garcia, vijaycs85, keesje, yannisc, chr.fritsch, ao2, youfei.sun, agentrickard, neclimdul, SocialNicheGuru, getu-lar, bircher: Do not reimport existing entities
Closes #2698425
Merge request reports
Activity
added 5 commits
-
4ba12a9a...674b4caf - 4 commits from branch
project:2.0.x
- c4934cf5 - Merge remote-tracking branch 'origin/2.0.x' into 2698425-do-not-reimport
-
4ba12a9a...674b4caf - 4 commits from branch
mentioned in merge request !5
added 1 commit
- f8dde94c - Introduce a flag to update or ignore existing content.
added 1 commit
- 15585e21 - Do not proceed with import operation if the updating of existing entities is disabled.
added 1 commit
- 8cc35696 - Test whether we can control if existing menu links are updated.
added 1 commit
- ec50803f - Test that we can control whether existing paragraph entities are updated.
added 1 commit
- 4783523d - Test that we can control if existing content is updated when importing content.
178 178 $values[$entity_type->getKey('langcode')] = $data['_meta']['default_langcode']; 179 179 } 180 180 181 // Load the entity by UUID and check if it exists. 182 $existing = $this->entityTypeManager->getStorage($entity_type->id())->loadByProperties(['uuid' => $values['uuid']]); 178 178 $values[$entity_type->getKey('langcode')] = $data['_meta']['default_langcode']; 179 179 } 180 180 181 // Load the entity by UUID and check if it exists. 182 $existing = $this->entityTypeManager->getStorage($entity_type->id())->loadByProperties(['uuid' => $values['uuid']]); 181 183 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ 182 $entity = $this->entityTypeManager->getStorage($entity_type->id())->create($values); 184 if (!empty($existing)) { 185 $entity = reset($existing); 186 if (!$update_existing) { 187 // Do not override the existing entity. 188 return $entity; 189 } Found a bug here, because there is a use case when
dependencies
won't reset, then next call will won't fill from datadepends
, then it will cause NULL loaded entity, so if we return then we must check the same logic as we do in the end of this function:if ($is_root) { $this->dependencies = NULL; }
So the code should like something like this:
if (!$update_existing) { if ($is_root) { $this->dependencies = NULL; } // Do not override the existing entity. return $entity; }
added 1 commit
- c42f7c38 - Pass update_existing flag to setFieldValues and nested entities. Set needsSave where necessary.
189 208 if ($this->languageManager->getLanguage($langcode)) { 190 209 $translation = $entity->addTranslation($langcode, $entity->toArray()); 191 210 foreach ($translation_data as $field_name => $values) { 192 $this->setFieldValues($translation, $field_name, $values); 211 $this->setFieldValues($translation, $field_name, $values, $update_existing); 178 178 $values[$entity_type->getKey('langcode')] = $data['_meta']['default_langcode']; 179 179 } 180 180 181 // Load the entity by UUID and check if it exists. 182 $existing = $this->entityTypeManager->getStorage($entity_type->id())->loadByProperties(['uuid' => $values['uuid']]); 181 183 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ 182 $entity = $this->entityTypeManager->getStorage($entity_type->id())->create($values); 184 if (!empty($existing)) { 185 $entity = reset($existing); 186 // @todo Use instanceof EntityNeedSaveInterface once 187 // https://www.drupal.org/project/entity_reference_revisions/issues/3336752 188 // is fixed. 189 if (method_exists($entity, 'setNeedsSave')) { 190 $entity->setNeedsSave(TRUE); changed this line in version 11 of the diff
We can already check for the ERR interface right now, once the upstream issue [#3336752] is fixed we can drop the
method_exists()
check.
207 207 if (!empty($data['translations'])) { 208 208 foreach ($data['translations'] as $langcode => $translation_data) { 209 209 if ($this->languageManager->getLanguage($langcode)) { 210 $translation = $entity->addTranslation($langcode, $entity->toArray()); 210 $translation = $entity->hasTranslation($langcode) 211 ? $entity->getTranslation($langcode) 212 : $entity->addTranslation($langcode, $entity->toArray()); - Comment on lines -210 to +212
This is needed to be able to re-import entities which have existing translations. There was a separate issue for this, but it makes sense to integrate it here. Ref. [#3176839]
added 7 commits
-
a4ab874a...9efd1d54 - 5 commits from branch
project:2.0.x
- 730d7659 - Merge remote-tracking branch 'origin/2.0.x' into 2698425-do-not-reimport
- a7e85efd - Do a more robust check for entities using the Entity Reference Revision module.
-
a4ab874a...9efd1d54 - 5 commits from branch
17 17 use Drupal\Core\TypedData\PrimitiveInterface; 18 18 use Drupal\Core\TypedData\TypedDataInterface; 19 19 use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper; 20 use Drupal\entity_reference_revisions\EntityNeedsSaveInterface; Won't this
use
statement fail ifentity_reference_revisions
module is not installed? It is not a dependency ofdefault_content
module.Edited by Artem Dmitriiev
added 9 commits
-
a7e85efd...b85c0192 - 8 commits from branch
project:2.0.x
- abcb7f4d - Merge branch default_content:2.0.x into 2698425-do-not-reimport
-
a7e85efd...b85c0192 - 8 commits from branch