Bug 40459

Summary: Preferred name is lost when editing partial record
Product: Koha Reporter: Andrew Fuerste-Henry <andrew>
Component: PatronsAssignee: Imani <imani.thomas>
Status: Pushed to main --- QA Contact: Marcel de Rooy <m.de.rooy>
Severity: normal    
Priority: P5 - low CC: cj.lynce, gmcharlt, kyle, laura, m.de.rooy, mspinney, mteal
Version: Main   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=40462
GIT URL: Change sponsored?: ---
Patch complexity: Small patch Documentation contact:
Documentation submission: Text to go in the release notes:
Version(s) released in:
25.11.00
Circulation function:
Attachments: Bug 40459: Preferred name is lost when editing partial record to test: * on the patron page click edit at the top, entered a preferred name, and hit save * we should see the preferred name being displayed instead of the first name for the user * go back
Bug 40459: Preferred name is lost when editing partial record to test:
Bug 40459: Preferred name is lost when editing partial record
Bug 40459: Preferred name is lost when editing partial record

Description Andrew Fuerste-Henry 2025-07-21 14:21:40 UTC
To recreate:
- have a patron with a preferred_name
- click the big Edit button at the top of the record to edit the whole patron record, change something other than preferred name, click save
- confirm preferred name is retained
- click the edit button in the Contact Information section of the record to edit part of patron record, change something other than preferred name, click save
- confirm preferred name is retained

- click the edit button in **any other** section of the record to edit part of patron record, change something other than preferred name, click save
- confirm preferred name is lost
Comment 1 mteal 2025-07-21 14:41:49 UTC
It'd be great to have this patched on 24.11. While we can ask staff to not use the sectional edit buttons, they inevitably will, so we risk losing data.
Comment 2 Imani 2025-07-21 17:00:53 UTC
Created attachment 184449 [details] [review]
Bug 40459: Preferred name is lost when editing partial record to test: * on the patron page click edit at the top, entered a preferred name, and hit save 	* we should see the preferred name being displayed instead of the first name for the user * go back
Comment 3 CJ Lynce 2025-07-21 17:38:53 UTC
@Imani: Commenting out the test for preferred_name entirely causes some unexpected behavior if any preferred_names are set and then you change BorrowerUnwantedField to exclude preferred_name. In this case you will no longer be able to edit the preferred_name, and changing the patron's firstname will no change the preferred name,

A better solution would be to wrap the test for preferred_name in an if clause based on the $step variable passed from the form. $step = 0 if the full form is being edited. $step = 1 if the Contact Info portion is being edited. Anything else is a partial edit of the form elsewhere.
Comment 4 Andrew Fuerste-Henry 2025-07-21 19:25:36 UTC
(In reply to CJ Lynce from comment #3)
> @Imani: Commenting out the test for preferred_name entirely causes some
> unexpected behavior if any preferred_names are set and then you change
> BorrowerUnwantedField to exclude preferred_name. In this case you will no
> longer be able to edit the preferred_name, and changing the patron's
> firstname will no change the preferred name,

This is odd because elsewhere in the patron record any value set in a field that gets hidden via BorrowerUnwantedField is saved forever and cannot be edited from the patron record. But following that behavior for preferred_name would cause problems for patrons who change their firstnames, as their preferred_name would not update --and as soon as firstname and preferred_name do not match, Koha starts displaying preferred_name around the intranet and OPAC.
Comment 5 CJ Lynce 2025-07-21 19:33:33 UTC
(In reply to Andrew Fuerste-Henry from comment #4)
> This is odd because elsewhere in the patron record any value set in a field
> that gets hidden via BorrowerUnwantedField is saved forever and cannot be
> edited from the patron record. But following that behavior for
> preferred_name would cause problems for patrons who change their firstnames,
> as their preferred_name would not update --and as soon as firstname and
> preferred_name do not match, Koha starts displaying preferred_name around
> the intranet and OPAC.

Ideally, there would be a test to see if preferred_name is hidden BorrowerUnwantedField, and in that case, ignores, but doesnt change, the preferred_name field. This way, anything stored in preferred_name is preserved in the event its hidden then unhidden in the future. That said, without knowing exactly how preferred_name is handled throughout Koha, it is potentially a big undertaking to change this behavior.

For the time being, though it doesn't really follow the behavior of other hidden/stored patron fields, the 
    newdata{preferred_name} = undef unless defined $newdata{preferred_name}
test helps to prevent a wrong preferred_name from being locked in if the field is hidden.
Comment 6 Imani 2025-07-21 20:43:01 UTC
Created attachment 184458 [details] [review]
Bug 40459: Preferred name is lost when editing partial record to test:

* follow test plan of previous patch 
* add preferred_name to BorrowerUnwantedField 
* updating the full patron or contact information field should update the preferred_name to the current first name
Comment 7 CJ Lynce 2025-07-21 21:40:56 UTC
Created attachment 184459 [details] [review]
Bug 40459: Preferred name is lost when editing partial record

To test:
* on the patron page click edit at the top, entered a preferred name, and hit save
  * we should see the preferred name being displayed instead of the first name for the user
* go back to the main edit page and change the preferred name to a blank space
  * We should revert back to the first name being used
* if we open the edit page back up we should see the first name in the preferred name field
* Edit the preferred name again and click on details
* open up any of the sections other then contact information
* hit the save button on the section without changing anything
  * you should still see the preferred name you set being used.
* open up an individual section other then contact method, change a value, then save
  * you should still see the preferred name you set being used.
* open up the contact information section and save without changing anything
  * the preferred name should be intact
* open up the contact information and change the preferred name, and save.
  * you should see the new preferred name reflected.
* Open up the contact information section, change the preferred name field to a blank and save.
  * you should see the first name being used now.
* Open up the contact information section, and put any number of spaces in the preferred name with nothing else and then save.
  * you should still see the first name being used.

Signed-off-by: CJ Lynce <cj.lynce@westlakelibrary.org>
Comment 8 CJ Lynce 2025-07-21 21:44:16 UTC
Patch works as intended both when preferred_name is not hidden, and when it is. 

I squashed both patches into one and cleaned up the test plan for good measure.
Comment 9 Marcel de Rooy 2025-07-25 09:45:21 UTC
Created attachment 184624 [details] [review]
Bug 40459: Preferred name is lost when editing partial record

To test:
* on the patron page click edit at the top, entered a preferred name, and hit save
  * we should see the preferred name being displayed instead of the first name for the user
* go back to the main edit page and change the preferred name to a blank space
  * We should revert back to the first name being used
* if we open the edit page back up we should see the first name in the preferred name field
* Edit the preferred name again and click on details
* open up any of the sections other then contact information
* hit the save button on the section without changing anything
  * you should still see the preferred name you set being used.
* open up an individual section other then contact method, change a value, then save
  * you should still see the preferred name you set being used.
* open up the contact information section and save without changing anything
  * the preferred name should be intact
* open up the contact information and change the preferred name, and save.
  * you should see the new preferred name reflected.
* Open up the contact information section, change the preferred name field to a blank and save.
  * you should see the first name being used now.
* Open up the contact information section, and put any number of spaces in the preferred name with nothing else and then save.
  * you should still see the first name being used.

Signed-off-by: CJ Lynce <cj.lynce@westlakelibrary.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 10 Lucas Gass (lukeg) 2025-07-25 20:40:29 UTC
Nice work everyone!

Pushed to main for 25.11
Comment 11 Laura O'Neil 2025-07-25 20:44:38 UTC
Thanks Lucas! Can this be backported to 24.11.xx please?