Skip to content

[4.x]: Re-saving an address can mutate the first and last name on the address #14665

@a-solaris

Description

@a-solaris

What happened?

Description

If you have an address element with the middle name stuffed in either firstName or lastName for any reason, saving the address element can yield unpredictable results.

This is because of slightly circular logic in NameTrait. For example, if you have an Address element with fullName empty, but firstName and lastName set, on save the fullName field will become populated. Then, if you save the Address instead, the fullName field gets parsed into firstName and lastName fields, losing any middle names, if they were stored in those fields.

For customers that strongly prefer avoiding using the fullName field, this can become problematic when dealing with Addresses in Commerce, especially considering that selecting billing address to be the same as shipping address will duplicate the element, in essence re-saving it and potentially mutating the data already.

Steps to reproduce

In Craft shell

$address = new \craft\elements\Address(['firstName' => 'One Two', 'lastName' => 'Three Four']);
Craft::$app->elements->saveElement($address, false);
echo sprintf('%s + %s = %s', $address->firstName, $address->lastName, $address->fullName);
Craft::$app->elements->saveElement($address, false);
echo sprintf('%s + %s = %s', $address->firstName, $address->lastName, $address->fullName);

Expected behavior

One Two + Three Four = One Two Three Four is printed twice

Actual behavior

One Two + Three Four = One Two Three Four
One + Four = One Two Three Four

is printed instead, as the simple act of saving the Address element again changed the data.

Craft CMS version

4.7.2

Installed plugins and versions

  • Commerce (not affecting this bug, but useful for context)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions