Skip to content

Commit

Permalink
fix(SFT-1353) issue with UIDs of field layout elements when duplicati…
Browse files Browse the repository at this point in the history
…ng calendars (#314)

* fix(SFT-1353) duplicating calendars

* fix(SFT-1353) adding `use craft\helpers\StringHelper as CraftStringHelper;` to the `CalendarsController.php`

---------

Co-authored-by: kjmartens <[email protected]>
  • Loading branch information
seandelaney and kjmartens authored Sep 12, 2024
1 parent 1d26986 commit 719111c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/plugin/src/Controllers/CalendarsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\db\Table;
use craft\helpers\Db;
use craft\helpers\Queue;
use craft\helpers\StringHelper as CraftStringHelper;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use craft\queue\jobs\UpdateElementSlugsAndUris;
Expand Down Expand Up @@ -116,11 +117,20 @@ public function actionDuplicate(): Response
$oldLayoutTabs = $oldLayout->getTabs();

foreach ($oldLayoutTabs as $oldLayoutTab) {
$newElements = [];
foreach ($oldLayoutTab->getElements() as $oldElement) {
unset($oldElement['uid']);

$oldElement->uid = CraftStringHelper::UUID();
$newElements[] = $oldElement;
}

$newLayoutTab = new FieldLayoutTab();
$newLayoutTab->uid = CraftStringHelper::UUID();
$newLayoutTab->name = $oldLayoutTab->name;
$newLayoutTab->sortOrder = $oldLayoutTab->sortOrder;
$newLayoutTab->setLayout($newLayout);
$newLayoutTab->setElements($oldLayoutTab->getElements());
$newLayoutTab->setElements($newElements);

$newLayoutTabs[] = $newLayoutTab;
}
Expand Down Expand Up @@ -150,15 +160,15 @@ public function actionDuplicate(): Response

$iterator = 0;
foreach ($handles as $handle) {
if (preg_match('/-(\d+)$/', $handle, $matches)) {
$iterator = max($iterator, (int) $matches[1]);
if (preg_match('/^(.*)(\d+)$/', $handle, $matches)) {
$iterator = max($iterator, (int) $matches[2]);
}
}

++$iterator;

$clone->name = preg_replace('/^(.*) \d+$/', '$1', $clone->name).' '.$iterator;
$clone->handle = preg_replace('/^(.*)-\d+$/', '$1', $clone->handle).$iterator;
$clone->handle = preg_replace('/^(.*)\d+$/', '$1', $clone->handle).$iterator;

if ($this->getCalendarService()->saveCalendar($clone)) {
return $this->asJson(['success' => true]);
Expand Down

0 comments on commit 719111c

Please sign in to comment.