Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep track of source and target site id when duplicating #316

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/controllers/CkeditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,17 @@ public function actionEntryCardHtml(): Response
*/
public function actionDuplicateNestedEntry(): Response
{
$entryId = $this->request->getRequiredBodyParam('entryId');
$siteId = $this->request->getBodyParam('siteId');
$entryId = $this->request->getRequiredBodyParam('entryId'); // id of the entry we're going to duplicate
$sourceSiteId = $this->request->getBodyParam('sourceSiteId');
$targetSiteId = $this->request->getBodyParam('targetSiteId');
$targetEntryTypeIds = $this->request->getBodyParam('targetEntryTypeIds');
$targetOwnerId = $this->request->getBodyParam('targetOwnerId');
$targetLayoutElementUid = $this->request->getBodyParam('targetLayoutElementUid');
$targetFieldId = null;

$entry = Craft::$app->getEntries()->getEntryById($entryId, null, [
$entry = Craft::$app->getEntries()->getEntryById($entryId, $sourceSiteId, [
'status' => null,
'revisions' => null,
'preferSites' => [$siteId],
]);

if (!$entry) {
Expand All @@ -137,7 +137,7 @@ public function actionDuplicateNestedEntry(): Response
// get ID of the field we're duplicating (e.g. pasting) into
if ($targetLayoutElementUid !== null) {
if ($targetOwnerId !== null && $entry->primaryOwnerId !== $targetOwnerId) {
$owner = Craft::$app->getElements()->getElementById($targetOwnerId);
$owner = Craft::$app->getElements()->getElementById($targetOwnerId, null, $targetSiteId);
} else {
$owner = $entry->getOwner();
}
Expand All @@ -149,8 +149,8 @@ public function actionDuplicateNestedEntry(): Response
}

$newAttrs = [];
if ($siteId !== null && $entry->siteId !== $siteId) {
$newAttrs['siteId'] = $siteId;
if ($targetSiteId !== null && $entry->siteId !== $targetSiteId) {
$newAttrs['siteId'] = $targetSiteId;
}
if ($targetOwnerId !== null && $entry->primaryOwnerId !== $targetOwnerId) {
$newAttrs['primaryOwnerId'] = $targetOwnerId;
Expand All @@ -169,6 +169,7 @@ public function actionDuplicateNestedEntry(): Response

return $this->asJson([
'newEntryId' => $newEntry->id,
'newSiteId' => $newEntry->siteId,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/ckeditor/dist/ckeditor5-craftcms.js.map

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions src/web/assets/ckeditor/src/ckeditor5-craftcms.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,15 @@ const handleClipboard = function (editor, plugins) {
) {
let duplicatedContent = pasteContent;
let errors = false;
const siteId = Craft.siteId;
const targetSiteId = Craft.siteId;
let ownerId = null;
let layoutElementUid = null;
const editorData = editor.getData();
const matches = [...pasteContent.matchAll(/data-entry-id="([0-9]+)/g)];
const matches = [
...pasteContent.matchAll(
/data-entry-id="([0-9]+)[^>]*data-site-id="([0-9]+)/g,
),
];

// Stop the event emitter from calling further callbacks for this event interaction
// we need to get duplicates and update the content snippet that's being pasted in
Expand Down Expand Up @@ -449,6 +453,10 @@ const handleClipboard = function (editor, plugins) {
if (matches[i][1]) {
entryId = matches[i][1];
}
let sourceSiteId = null;
if (matches[i][2]) {
sourceSiteId = matches[i][2];
}

if (entryId !== null) {
// check if we're copying to a different field and if this entry ID is in the field already
Expand Down Expand Up @@ -484,7 +492,8 @@ const handleClipboard = function (editor, plugins) {
{
data: {
entryId: entryId,
siteId: siteId,
targetSiteId: targetSiteId,
sourceSiteId: sourceSiteId,
targetEntryTypeIds: targetEntryTypeIds,
targetOwnerId: ownerId,
targetLayoutElementUid: layoutElementUid,
Expand All @@ -494,8 +503,14 @@ const handleClipboard = function (editor, plugins) {
.then((response) => {
if (response.data.newEntryId) {
duplicatedContent = duplicatedContent.replace(
entryId,
response.data.newEntryId,
'data-entry-id="' + entryId + '"',
'data-entry-id="' + response.data.newEntryId + '"',
);
}
if (response.data.newSiteId) {
duplicatedContent = duplicatedContent.replace(
'data-site-id="' + sourceSiteId + '"',
'data-site-id="' + response.data.newSiteId + '"',
);
}
})
Expand Down
Loading