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 (#313)
  • Loading branch information
seandelaney authored Sep 12, 2024
1 parent 5249173 commit 543fb73
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/plugin/src/Controllers/CalendarsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,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 @@ -172,15 +181,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 543fb73

Please sign in to comment.