Problem/Motivation
Since 2.0.4 the widget never preselects stored values. Edit forms show
"- None -", and the next save wipes the value.
#3517342 made getOptions() return an empty array, assuming the options
are never used. They are: OptionsSelectWidget::formElement() sets
'#default_value' => $this->getSelectedOptions($items), and
OptionsWidgetBase::getSelectedOptions() filters stored values against
that list. Empty list, nothing matches, no default value.
Affects 2.0.4 - 2.0.7. Not 2.0.3, not 3.0.x.
Same bug as #17 in #3613802 (that issue is otherwise about the D10
getBundleListCacheTags() fatal).
Steps to reproduce
1. Entity reference field to a vocabulary, widget "Simple hierarchical select".
2. Create node, select a term, save.
3. Edit: widget shows "- None -".
4. Save: value gone.
Proposed resolution
Override getSelectedOptions() to return the stored values unfiltered.
The #3517342 optimisation stays intact - the option list is still never
loaded.
Patch attached with a kernel test. Drupal 11.4.5 / PHP 8.3, shs 2.0.7,
phpcs clean.
| Comment | File | Size | Author |
|---|---|---|---|
| shs_preselect_stored_values.patch | 6.59 KB | carlitus |
Issue fork shs-3620147
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
greenskin commentedPatch applies and looks to work.
Comment #3
serverjohn commentedI am having the same issue as described. The patch resolves the issue.
Comment #6
malcomio commentedThanks for the patch - it does seem to address the issue, from my initial tests.
Not aware of the history on #3517342: Widget loads references that it never uses, causing increased memory usage
Patches are now deprecated on drupal.org, see Creating merge requests instead.
I've taken the patch and put it into https://git.drupalcode.org/project/shs/-/merge_requests/58
Comment #7
philltran commented@malcomio Thanks for filing MR!58
Confirming the patch worked on four Drupal 10.6.16 / PHP 8.3 sites.
I believe this issue is 2.0.7 only, not 2.0.4 - 2.0.7.
The empty `getOptions()` from #3517342 has been there since 2.0.4, but shs also carried its own `getSelectedOptions()` override returning the stored `target_id`s unfiltered. The override is present in 2.0.3, 2.0.4, 2.0.5 and 2.0.6, and absent only in 2.0.7.
This caused a silent data loss in addition to a display problem. The widget renders "- None -", and the next save writes an empty value and deletes the stored term. On our site every product save erased its category tag causing products to lose their taxonomy references.
Comment #8
joe huggansThe patch also worked for me Drupal 11.4.7
Thank you @philltran for confirming this issue only arose in version 2.0.7, that is very helpful for when I need to advise my client.