Skip to content

Commit

Permalink
Really ignore URI validation errors when duplicating an element
Browse files Browse the repository at this point in the history
Fixes #4759
  • Loading branch information
brandonkelly committed Aug 16, 2019
1 parent 807e491 commit 43caabc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Fixed
- Fixed an error that could occur if garbage collection was run while Craft 3.2 migrations were pending. ([#4720](https://github.com/craftcms/cms/issues/4720))
- Fixed a validation error that occurred when duplicating an entry, if the URI format was based on a custom field value. ([#4759](https://github.com/craftcms/cms/issues/4759))

## 3.2.10 - 2019-08-13

Expand Down
15 changes: 8 additions & 7 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,18 +703,19 @@ public function duplicateElement(ElementInterface $element, array $newAttributes
throw new Exception('Attempting to duplicate an element in an unsupported site.');
}

// Set a unique URI on the clone
try {
ElementHelper::setUniqueUri($mainClone);
} catch (OperationAbortedException $e) {
// Oh well, not worth bailing over
// Validate, ignoring any URI errors
$mainClone->setScenario(Element::SCENARIO_ESSENTIALS);
$mainClone->validate();
$mainClone->clearErrors('uri');

if ($mainClone->hasErrors()) {
throw new InvalidElementException($mainClone, 'Element ' . $element->id . ' could not be duplicated because it doens\'t validate.');
}

$transaction = Craft::$app->getDb()->beginTransaction();
try {
// Start with $element's site
$mainClone->setScenario(Element::SCENARIO_ESSENTIALS);
if (!$this->_saveElementInternal($mainClone, true, false)) {
if (!$this->_saveElementInternal($mainClone, false, false)) {
throw new InvalidElementException($mainClone, 'Element ' . $element->id . ' could not be duplicated for site ' . $element->siteId);
}

Expand Down

0 comments on commit 43caabc

Please sign in to comment.