craft-dynamic-fields
craft-dynamic-fields copied to clipboard
Default dropdown value is overriding saved value when in a Matrix block
Setup: I have a Dropdown (dynamic)
field in a Matrix block, configured with the following options:
{ "value":"jim", "label":"Jim Beam" },
{ "value":"jack", "label":"Jack Daniels", "default":true },
{ "value":"mark", "label":"Maker's Mark" },
{ "value":"rebel", "label":"Rebel Yell" }
As you can see, "Jack Daniels" is set as the default value.
The first time I create an entry, and add the matrix block, the dropdown appears and correctly has "Jack Daniels" selected.
Let's say I change the value to "Jim Beam", and hit save and continue editing.
When the publishing page is refreshed and the matrix block re-rendered, the Dropdown is erroneously set back to the default "Jack Daniels". However if I check in the DB, the correct value, jim
, is being stored correctly.
I have tracked it down to lines 53-59 in Dropdown.php of the method Dropdown::normalizeValue()
:
if ($this->isFresh($element) ) :
foreach ($options as $key => $option) :
if (!empty($option['default'])) :
$value = $option['value'];
endif;
endforeach;
endif;
For some reason the element is being considered "fresh", yet $value
was passed to the method. This seems to be an identical issue I encountered in issue #37, for which I raised a pull request #38, which is to change that conditional statement to also check if $value
is non-null, e.g.:
if ( ! $value && $this->isFresh($element) ) :
foreach ($options as $key => $option) :
if (!empty($option['default'])) :
$value = $option['value'];
endif;
endforeach;
endif;
As I quickly scan the source code, I'm sure that the other field types are afflicted with this same malady - in each case, the check for "freshness" isn't necessary is $value
has been set.
It would really be great if this could be addressed soon. Not being able to set default values on the dropdown is a setback to crafting a user-friendly UX for my content editors. Let me know if you'd like me to issue a new PR that addresses this issue for all fieldtypes.
Thanks! -John
Craft CMS 3.5.14 LJ Dynamic Fields 3.0.11