Problem/Motivation

Let's say I have a node (ID: 1) with some paragraphs set on it.
I cloned the node 1 with the paragraphs. And i have a new node 2, the cloned node.

Then if I un-publish the node 2, all the paragraphs set on node 1 are not visible for anonymous users. (because anonymous users can't view unpublished node).

After debugging, it appears that on the node 1, all the original paragraphs have as parent ID the node 2 ID.

If I edit node 1, edit all the paragraphs, then the right parent ID is set on the node 1 's paragraphs, after saving the original node.

Proposed resolution

TODO : Find the root cause. I guess that original paragraphs have the parent ID set on the new node cloned ID, before being cloned themself.

Remaining tasks

Confirm the bug
Write failing tests
Fix the bug

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/A

Comments

flocondetoile created an issue. See original summary.

flocondetoile’s picture

Issue summary: View changes
flocondetoile’s picture

Not sure if this bug is related to entity_clone or entity_reference_revision module.

flocondetoile’s picture

The thing happens in the postsave() method of EntityReferenceRevisionsItem class

// If the parent id has changed then set it.
    if ($entity->get($parent_id)->value != $parent_entity->id()) {
      $entity->set($parent_id, $parent_entity->id());
      $needs_save = TRUE;
    }

The $parent_entity is the new node cloned and so the parent_id is updated. But this shouldn't occurs as, theorically, the original paragraph shouldn't be saved.

flocondetoile’s picture

As a workaround, with an event subscriber you can re-save the original entity to trigger again the postsave() method and set the right parent_id on the original paragraphs.

/**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      EntityCloneEvents::POST_CLONE => 'onPostClone'
    ];
  }


  /**
   * On entity clone post event.
   *
   * @param \Drupal\entity_clone\Event\EntityCloneEvent $event
   *   The entity clone event instance.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function onPostClone(EntityCloneEvent $event) {
    $entity = $event->getEntity();
    if ($entity instanceof ContentEntityInterface) {
      $entity->save();
    }
  }
dpavon’s picture

I would say this is related to the "corrupt" paragraph issue in #3060223

idebr’s picture

Status: Active » Closed (duplicate)

It is. Let's close this issue as a duplicate, so we can focus our efforts in the related issue.