getgrav / grav-plugin-admin

Grav Admin Plugin
http://getgrav.org
MIT License
355 stars 227 forks source link

Bug: "pages" selection/dropdown field does not save additional option values correctly #2269

Open hughbris opened 2 years ago

hughbris commented 2 years ago

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. If it makes any difference, it's within a list.

    .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 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:

        {% for key, label in field.options %}
            <option {% if key == value or (field.multiple and key in value) %}selected="selected"{% endif %} value="{{ key|e('html_attr') }}">{{ label|t }}</option>
        {% 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?