Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default dropdown value is overriding saved value when in a Matrix block #40

Open
johndwells opened this issue Oct 29, 2020 · 0 comments

Comments

@johndwells
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant