getgrav / grav-plugin-admin

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

taxonomy not shown in admin panel after saving #1472

Open sebastianbaumann opened 6 years ago

sebastianbaumann commented 6 years ago

I created a custom page.collection for a listing page containing some products. After that, I created the respective blueprint file. Everything is perfectly fine, until I try to edit and save the taxonomy categories from the admin panel.

The markdown file is filled with the correct input, but after reloading the admin panel, the previously filled category field is empty.

This is my productlist.md

---
kollektion:
    -
        title: Umbau
        '@taxonomy':
            category:
                - umbau
routable: false
visible: false
title: Produkte
content:
    items: '@self.children'
    order:
        by: date
        dir: desc
---

This is my blueprint productlist.yaml

title: Seite
'@extends':
    type: default
    context: blueprints://pages

form:
    fields:
        tabs:
            type: tabs
            active: 1

            fields:
                content:
                    unset@: true
                    type: tab
                    title: Seite
                    fields:

                        header.headline:
                            type: text
                            label: Seitenüberschrift

                        header.kollektion:
                            name: kollektion
                            type: list
                            style: vertical
                            label: Kollektion
                            fields:
                                .title:
                                    type: text
                                    label: Title
                                .@taxonomy:
                                    type: taxonomy
                                    label: PLUGIN_ADMIN.TAXONOMY
                                    multiple: true
                                    validate:
                                        type: array

                route:
                    type: select
                    label: PLUGIN_ADMIN.PARENT
                    classes: fancy
                    '@data-options': '\Grav\Common\Page\Pages::parents'
                    '@data-default': '\Grav\Plugin\admin::route'
                    options:
                        '/': PLUGIN_ADMIN.DEFAULT_OPTION_ROOTs
                advanced:
                    unset@: true

As you can see, after saving from the admin panel, the markdown file is filled correctly. But the admin panel stays empty.

image

rhukster commented 6 years ago

Probably specific to it being inside a list. Can you test/confirm if it saves correctly outside the list?

sebastianbaumann commented 6 years ago
title: Seite
'@extends':
    type: default
    context: blueprints://pages

form:
    fields:
        tabs:
            type: tabs
            active: 1

            fields:
                content:
                    unset@: true
                    type: tab
                    title: Seite
                    fields:

                        header.headline:
                            type: text
                            label: Seitenüberschrift
                        # not working
                        header.test:
                           type: taxonomy
                           label: PLUGIN_ADMIN.TAXONOMY
                           multiple: true
                           validate:
                               type: array
                        # working
                        header.taxonomy:
                           type: taxonomy
                           label: PLUGIN_ADMIN.TAXONOMY
                           multiple: true
                           validate:

Sure! Works outside a list, when the field is named taxonomy otherwise it doesn't.

sebastianbaumann commented 6 years ago

Any news…?

grantholle commented 5 years ago

Outside of a list, I can confirm this bug. When trying to use the taxonomy field type, unless it's named header.taxonomy it will not populate. It does save if it has a different name, though.

My blueprint

header.taxes:
  type: taxonomy
  label: PLUGIN_ADMIN.TAXONOMY
  multiple: true
  style: vertical
  validate:
    type: array

It creates and saves the fields correctly for the page frontmatter.

taxes:
    category:
        - staff
    tag:
        - corporate

It doesn't populate the field from the admin page, though. It even has the correct name for the select on the admin page name="data[header][taxes][category][]", but it won't actually populate the field.

Naming the header field taxonomy will populate the field, though.

Regaez commented 4 years ago

@rhukster If I understand this correctly, it appears that the taxonomy field is hardcoded to pull its data from header.taxonomy instead of using data that matches the blueprint's field name.

See this line of code: https://github.com/getgrav/grav-plugin-admin/blob/develop/themes/grav/templates/forms/fields/taxonomy/taxonomy.html.twig#L11

I have also encountered this issue. As a hacky workaround, I created a static function in my plugin that will return some "default" data. e.g. on my taxonomy field I add:

header.example:
  type: taxonomy
  label: PLUGIN_ADMIN.TAXONOMY
  # DEFINE YOUR OWN STATIC PHP FUNCTION AS DEFAULT
  default@: '\Grav\Plugin\MyPlugin::getDefaultTaxonomyData'
  multiple: true
  validate:
    type: array

In this function, I have to figure out the form page's context (i.e. which page you are editing. I did this by using the Uri class and parsing the url -- not super reliable) and then fetching the correct data manually in my function and returning this array. The taxonomy field will then populate the form correctly with the custom "default" data.

It would be much nicer if the taxonomy.html.twig template could be updated to allow different data sources, however!

ricardo118 commented 4 years ago

I agree it probably needs an update to allow more dynamic ways of setting the taxonomy

P.S. @Regaez there's easier ways to figure out what page you are editing - you can message me on discord if you want to know more