Skip to content

Commit

Permalink
Stop updating name properties if all are already set
Browse files Browse the repository at this point in the history
Resolves #14665
  • Loading branch information
brandonkelly committed Apr 8, 2024
1 parent db53003 commit 025c5e4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added `craft\services\Sites::getSitesByLanguage()`.
- Added `craft\web\ErrorHandler::exceptionAsArray()`.
- Added `craft\web\ErrorHandler::showExceptionDetails()`.
- `craft\base\NameTrait::prepareNamesForSave()` no longer updates the name properties if `fullName`, `firstName`, and `lastName` are already set. ([#14665](https://github.com/craftcms/cms/issues/14665))

### System
- Craft now calls `setlocale()` based on the target language, so that `SORT_LOCALE_STRING` behaves as expected. ([#14509](https://github.com/craftcms/cms/issues/14509), [#14513](https://github.com/craftcms/cms/pull/14513))
Expand Down
6 changes: 6 additions & 0 deletions src/base/NameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ protected function normalizeNames(): void
protected function prepareNamesForSave(): void
{
if ($this->fullName !== null) {
// if firstName/lastName are also set, just leave them alone
// (https://github.com/craftcms/cms/issues/14665)
if ($this->firstName !== null || $this->lastName !== null) {
return;
}

$generalConfig = Craft::$app->getConfig()->getGeneral();
$languages = [
// Load our custom language file first so config settings can override the defaults
Expand Down
7 changes: 6 additions & 1 deletion src/controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2695,15 +2695,20 @@ private function populateNameAttributes(object $model): void

if ($fullName !== null) {
$model->fullName = $fullName;

// Unset firstName and lastName so NameTrait::prepareNamesForSave() can set them
$model->firstName = $model->lastName = null;
} else {
// Still check for firstName/lastName in case a front-end form is still posting them
$firstName = $this->request->getBodyParam('firstName');
$lastName = $this->request->getBodyParam('lastName');

if ($firstName !== null || $lastName !== null) {
$model->fullName = null;
$model->firstName = $firstName ?? $model->firstName;
$model->lastName = $lastName ?? $model->lastName;

// Unset fullName so NameTrait::prepareNamesForSave() can set it
$model->fullName = null;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/elements/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ public function setAttributes($values, $safeOnly = true): void
}

if (array_key_exists('firstName', $values) || array_key_exists('lastName', $values)) {
// Unset fullName so NameTrait::prepareNamesForSave() can set it
$this->fullName = null;
} elseif (array_key_exists('fullName', $values)) {
// Unset firstName and lastName so NameTrait::prepareNamesForSave() can set them
$this->firstName = $this->lastName = null;
}

parent::setAttributes($values, $safeOnly);
Expand Down
4 changes: 4 additions & 0 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,11 @@ public function setAttributes($values, $safeOnly = true): void
}

if (array_key_exists('firstName', $values) || array_key_exists('lastName', $values)) {
// Unset fullName so NameTrait::prepareNamesForSave() can set it
$this->fullName = null;
} elseif (array_key_exists('fullName', $values)) {
// Unset firstName and lastName so NameTrait::prepareNamesForSave() can set them
$this->firstName = $this->lastName = null;
}

parent::setAttributes($values, $safeOnly);
Expand Down

0 comments on commit 025c5e4

Please sign in to comment.