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)
What happened?
Description
If you have an address element with the middle name stuffed in either
firstNameorlastNamefor 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
fullNameempty, butfirstNameandlastNameset, on save thefullNamefield will become populated. Then, if you save the Address instead, thefullNamefield gets parsed intofirstNameandlastNamefields, losing any middle names, if they were stored in those fields.For customers that strongly prefer avoiding using the
fullNamefield, 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
Expected behavior
One Two + Three Four = One Two Three Fouris printed twiceActual behavior
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