Skip to content

[5.x]: Changed behaviour for duplicateElement when duplicating nested entries after 5.6 #16659

@aelvan

Description

@aelvan

What happened?

Description

We have a plugin that let's users clone nested entries into other sites. For instance, if you have a matrix field that have propagation method "Only save entries to the site they were created in", it let's you duplicate a nested entry to a different site.

The essence of what is does is $targetElement = \Craft::$app->elements->duplicateElement($element, ['siteId' => $newSite->id]);, and then $targetElement is added to the matrix field of the element in the other site as a new block.

Prior to 5.6, this worked perfectly. After 5.6, it throws an UnsupportedSiteException with message "'Attempting to duplicate an element in an unsupported site.".

I've tracked this down to getPrimaryOwner/getOwner in NestedElementTrait. The owner element has the site id of the duplicated element prior to 5.6, which means that the check for $supportedSites in duplicateElement is a-ok, whereas the owners site id after 5.6 is the site id that the element was duplicated from. But, the underlying issue seems to be that $this->siteId refers to the different sites, depending on whether I'm on 5.5 or 5.6.

I'm not able to track down why this happens though, so I'm not quite sure what changed that resulted in this. And, not quite sure if this is an intended or unintended change? But, it would be nice if it was possible somehow to duplicate nested entries to new sites.

Steps to reproduce

  1. Create a blank install on Craft 5.5.10.
  2. Set up two sites, site1 and site2.
  3. Create a matrix field that has propagation method "Only save entries to the site they were created in".
  4. Create an entry with a nested entry.
  5. Query for the nested entry in site1.
  6. Do $clonedNestedEntry = \Craft::$app->elements->duplicateElement($nestedEntry, ['siteId' => $site2->id]);
  7. Upgrade to 5.6 and do the same.

Expected behavior

You should get a new duplicated element in site2.

Actual behavior

It throws an UnsupportedSiteException with message "'Attempting to duplicate an element in an unsupported site.".

Craft CMS version

5.6.5.1 and 5.5.10

PHP version

8.3

Operating system and version

No response

Database type and version

Mysql 8.0

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions