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

Unexpected results from form blueprint #1287

Closed
paulhibbitts opened this issue Feb 1, 2017 · 10 comments
Closed

Unexpected results from form blueprint #1287

paulhibbitts opened this issue Feb 1, 2017 · 10 comments
Assignees

Comments

@paulhibbitts
Copy link

I have the following blueprint:

        -
            name: taxonomy.tag
            type: checkboxes
            label: 'Tag for Blog Post'
            options:
                tag1: 'Tag 1'
                tag2: 'Tag 2'
                tag3: 'Tag 3'
                tag4: 'Tag 4'

And the front matter generated looks like this:

---
taxonomy:
    tag:
        taxonomy.tag-tag1: tag1
        taxonomy.tag-tag3: tag3
---

Is this an issue with my blueprint formatting or an issue with Grav blueprint processing?

Thanks very much,
Paul

@flaviocopes flaviocopes self-assigned this Feb 1, 2017
@flaviocopes
Copy link
Contributor

Add the use: keys (explained in https://learn.getgrav.org/forms/forms/fields-available#the-checkboxes-field) to the field definition:

            header.taxonomy.tag:
              type: checkboxes
              label: 'Tag for Blog Post'
              options:
                tag1: 'Tag 1'
                tag2: 'Tag 2'
                tag3: 'Tag 3'
                tag4: 'Tag 4'
              use: keys

It will store as

taxonomy:
    tag:
        tag1: true
        tag2: true
        tag3: true
        tag4: false

@paulhibbitts
Copy link
Author

Thank you very much for the info @flaviocopes (I did not know about keys) but I am still unable to get the results I am trying for - setting the tags for a taxonomy as recognized by Grav.

I am trying to get this final result:

taxonomy:
  tag: [tag1,tag2,tag3]

Is this even possible then? Might there be another format accepted by Grav when building taxonomy tags?

Thanks again,
Paul

@flaviocopes
Copy link
Contributor

I tried on a page blueprint. Did you use it on a page blueprint too? Or if somewhere else, where?

@paulhibbitts
Copy link
Author

I am using it in a blueprint along with the Add Page by Form plugin (https://github.com/bleutzinn/grav-plugin-add-page-by-form), perhaps I also need to loop back with the plugin author and do some more tests. Thanks again for your help!

@flaviocopes
Copy link
Contributor

Oh ok thanks, in a frontend form. I thought in an Admin blueprint. Need to test as well.

@flaviocopes
Copy link
Contributor

Might as well be unique to that plugin, as the frontmatter is created upon form submit.

@bleutzinn
Copy link
Contributor

In the Add Page plugin I merge the preset frontmatter with the submitted field values and format the YAML like this:
$page->frontmatter(Yaml::dump((array) $page->header(), 10, 2, false));.

I was "inspired" by the exact same line of code in the Admin plugin.
Might that be the cause of the problem with the taxonomy formatting? If so what should the format be?

@bleutzinn
Copy link
Contributor

I've looked into the issue a bit. Why the Add Page plugin does is that it inserts the form field values as frontmatter of the newly created page. This goes well for simple field types such as text fields. However, with more complex field types such as checkbox this is not as straightforward.
So, I think the issue has nothing to do with the YAML formatting itself but requires a more clever way of processing input fields by the Add Page plugin.
@flaviocopes, can you confirm this?

@flaviocopes
Copy link
Contributor

@bleutzinn try doing this https://github.com/getgrav/grav-plugin-admin/blob/develop/classes/admincontroller.php#L1707-L1708 which is in the method that prepares the page for storage after saving.

@bleutzinn
Copy link
Contributor

Thanks flaviocopes. I've changed $page->frontmatter(Yaml::dump((array) $page->header(), 10, 2, false)); to simply $page->frontmatter(Yaml::dump((array) $page->header()));. Actually, the YAML frontmatter output reflects the data structure of $form->value()->toArray().

With four checkboxes in the form named tag1 to tag4 with only checkboxes tag2 and tag3 checked and the other two unchecked, when not using use keys, as the OP reported, the output is:

taxonomy:
    tag:
        taxonomy.tag-tag2: tag2
        taxonomy.tag-tag3: tag3

Pages with frontmatter in the above format are found when using {% for post in taxonomy.findTaxonomy({'tag':['tag3']}) %} in a template. It's not a pretty sight but it works.

With the addition of use: keys as you suggested, the YAML output becomes:

taxonomy:
    tag:
        tag2: true
        tag3: true

However, with this format pages with this tags can not be found using {% for post in taxonomy.findTaxonomy({'tag':['tag3']}) %}. Nor by {'tag3':['true']} or {'tag3':[true]}.

The above format is initially displayed as such in the Admin. Things get really weird when you edit this frontmatter in Admin and, for instance, change tag3: true to tag4: true. After doing so and saving the page in Admin suddenly the frontmatter is shown as:

taxonomy:
    tag:
        - '1'
        - '1'

bleutzinn added a commit to bleutzinn/grav-plugin-add-page-by-form that referenced this issue Feb 16, 2017
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

4 participants