getgrav / getgrav/grav-plugin-admin
Bug: "pages" selection/dropdown field does not save additional option values correctly
- Dominant language
- PHP
- Stars
- 377
- Forks
- 225
- Avg merge
- 11h 51m
- Merged PRs (30d)
- 4
Description
Apologies if this has been logged already, it's pretty difficult to precisely search issues for "pages".
I have added some custom external pages to my [Pages field](https://learn.getgrav.org/17/forms/blueprints/fields-available#pages-field). If it makes any difference, it's within a list.
```yaml
.target:
type: pages
label: Link target
show_all: false
show_modular: false
show_root: false
multiple: false
limit_levels: 3
options:
'https://example.org': '+ ORG'
'https://example.com': '+ COM'
'https://example.net': '+ NET'
validate:
required: true
```
The extra options render at the top of the site pages list just fine. However, for the custom options, the option value doesn't match the field value, so the select shows the first option in the list and that is saved next time the page is saved.
I looked at the [template on line 8](https://github.com/getgrav/grav-plugin-admin/blob/5a8e0ede20a2cfd47df4422c4d80ca6aef5f60e0/themes/grav/templates/forms/fields/pages/pages.html.twig#L8) and noticed that there is no check for the existing field value on custom options, so its `selected` attribute would never be selected.
I changed this to:
```twig
{% for key, label in field.options %}
{{ label|t }}
{% endfor %}
```
expecting it to work. (I renamed the iterated value to `label` because `value` was messing with my head!)
Then I noticed the `and depth == 0` above, couldn't see where `depth` was, and eliminated it too. Still not working.
When I add `dump(key, value)` inside the loop to see which values are being compared, everything seems in order. The values equal. I also added a `dump` inside the test and it did output when the values are equal. So the `selected` attribute is likely being added. Perhaps something is happening in the selectize JS??
So I thought it might be a simple oversight bug that I could fix, but there's more to it. :confounded: Any ideas?
Contributor guide
Research direction
Start with themes/grav/templates/forms/fields/pages/pages.html.twig, especially the custom-options loop and its selected-value checks, then inspect the selectize JavaScript behavior mentioned in the report. Reproduce the Pages field with the provided external options; done means the chosen custom option remains selected and is preserved after saving and reopening the page.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, php
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100