Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
You are not allowed to update the values in allowed_values for list type fields if the values are in use. This is ensured in list_field_update_forbid. This is good. I have a case though where I want to swap allowed_values for an allowed_values_function.
This patch allowed removing values from allowed_values IF there is an allowed_values_function AND the keys are kept using the existing logic.
Comment | File | Size | Author |
---|---|---|---|
#12 | allow-allowed-values-function-update-D9-2873353.patch | 1.87 KB | koenstrien |
| |||
#7 | allow-allowed-values-function-update-2873353-6.patch | 1.42 KB | odegard |
Comments
Comment #2
odegard CreditAttribution: odegard at Norweb AS commentedPatch
Comment #3
joelpittetWe don't need to do isset() with !empty() because it does it implicitly but this patch looks good otherwise! Going to give it a try.
Comment #4
joelpittet#3 could be fixed on commit(or I can roll that in). This works though.
Comment #5
odegard CreditAttribution: odegard at Norweb AS commentedThanks for checking it out.
Could you help me understand why we don't need isset and !empty?
I understand it's not necessary for this particular case when you want to actually switch between the two, but this will function run every time you try to update a list field. I don't think you can guarantee that these keys exists always?
Comment #6
knyshuk.vova CreditAttribution: knyshuk.vova at Internetdevels, Drupal Ukraine Community commented+1 to RTBC
Comment #7
odegard CreditAttribution: odegard at Norweb AS commentedYou are of course entirely correct.
Patch updated.
The patch have been in use since 2017 on multiple installations.
Comment #8
joelpittetSame here, using this patch in a few places, thanks @odegard.
RTBC++
Comment #9
koenstrien CreditAttribution: koenstrien commentedI have found that this patch does not work for D9 as the list module has been removed. The function has moved to the Options module. I have changed the patch for D9 and would appreciate if anyone can test whether they agree this patch works. I have also added extra information to the error message to notify which keys are causing the import error.
Comment #10
koenstrien CreditAttribution: koenstrien commentedComment #11
koenstrien CreditAttribution: koenstrien commentedComment #12
koenstrien CreditAttribution: koenstrien commentedSorry for reuploading a few times, I have improved the patch to pass the drupal 9.0-dev test.
Comment #13
very_random_man CreditAttribution: very_random_man commentedI can confirm the D9 patch is working for me.
Comment #14
rwam CreditAttribution: rwam commentedConfirmed that patch #12 works like a charm on Drupal 9.3.13 Therefore I switched version and component to match the requirements.
Comment #15
rwam CreditAttribution: rwam commentedNote:
If the allowed values are dynamic, based on the entity or should not be cached statically you have to use default parameters for your custom _my_module_allowed_values function to avoid an ArgumentCountError because of the following line:
Maybe an issue of this patch but you can solve this using:
Comment #17
catchThis could use some automated test coverage.