Clone entity data into existing entities in Drupal 8

Parent Feed: 

Creating a duplicate of an entity

Creating a duplicate of an entity is easily done via the entity API method Entity::createDuplicate(). This is a convenient method if the goal is to clone an entity into a new entity, as all identifiers of the previous entity get unset when using this method.

  1. $nid = 5;

  2. $entity = \Drupal::service('entity_type.manager')->getStorage('node')->load($nid); // Use dependency injection instead if in class context.

  3. $duplicate = $entity->createDuplicate();

  4. $duplicate->save();

Cloning data into an existing entity

However partially or fully cloning data into an existing entity is less straight forward (and rightfully so). Still, the ability to do so can be useful

  • in custom migration scripts where we want to overwrite old entities without creating new ones,
  • in cases where we need to overwrite the old entity as other internal or external data may reference it and creating a new entity would break these references.

The second case could be an entity reference field referencing the old entity in question (this could be technically solved by reassigning the reference), but it could also be 3rd party software referencing the old entity, which would complicate things.

This article is going to demonstrate a couple of possible ways of cloning entity data into existing entities.

Cloning field data 1:1

  1. $source_nid = 5;

  2. $destination_nid = 6;

  3. // Use dependency injection in class context instead.

  4. $source = \Drupal::service('entity_type.manager')->getStorage('node')->load($source_nid);

  5. $destination = \Drupal::service('entity_type.manager')->getStorage('node')->load($destination_nid);

  6. foreach ($source->getFields() as $name => $field) {

  7. $destination->set($name, $field->getValue());

  8. }

  9. $destination->save();

Importing new data only

To import new field data without overwriting existing data, just check if the destination field is empty before cloning into it like so:

  1. foreach ($source->getFields() as $name => $field) {

  2. if ($destination->get($name)->isEmpty()) {

  3. $destination->set($name, $field->getValue());

  4. }

  5. }

Putting it all together

A nifty method that could be used to clone field data of entities into other existing entitites could look like this:

  1. /**

  2.  * @param \Drupal\Core\Entity\Entity $source

  3.  * @param \Drupal\Core\Entity\Entity $destination

  4.  * @param string $mode

  5.  * Can be 'keep', 'overwrite' and 'clone'.

  6.  * @param array $skip_fields

  7.  * An array of fields not to be cloned into the destination entity.

  8.  */

  9. public function cloneFields(Entity $source, Entity &$destination, $mode, $skip_fields = []) {

  10. foreach ($source->getFields() as $name => $field) {

  11. // In this case clone only fields and leave out properties like title.

  12. if (strpos($name, 'field') === 0
  13. // Leave out certain fields.

  14. switch ($mode) {

  15. // Import only those fields from source that are empty in destination.

  16. case 'keep':

  17. default:

  18. if (!$destination->get($name)->isEmpty()) {

  19. continue 2;

  20. }

  21. break;

  22. // Import field data from source overwriting all destination fields.

  23. // Do not empty fields in destination if they are empty in source.

  24. case 'overwrite':

  25. if ($source->get($name)->isEmpty()) {

  26. continue 2;

  27. }

  28. break;

  29. // Import field data from source overwriting all destination fields.

  30. // Empty fields in destination if they are empty in source.

  31. case 'clone':

  32. break;

  33. }

  34. $destination->set($name, $field->getValue());

  35. }

  36. }

  37. $destination->save();

  38. }

There you go. Make sure to comment below in case of questions or if you know a better way of doing the above.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web