Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Json filetype in (Contact)form with flex

Solved by pamtbaau View solution

Started by Peter Berkel 5 years ago · 12 replies · 1317 views
5 years ago

I was wondering if it is possible to create a contactform with

TWIG
process:
    - save:
        filename: feedback.json
        body: "{% include 'forms/data.txt.twig' %}"
        operation: add

(in json / flex format ) so that I can manage (in the backoffice) the feedback with a flex solution. I would be happy if there some example because I am not that good in creating a twig ans so.

Or is it not possible at all?
Peter

5 years ago

You should create a JSON template in your theme. Eg. /user/themes/myTheme/templates/forms/data.json.twig with content:

TWIG
{{ fields|json_encode|raw }}

(TBH, not sure about content of the template though - would need to test properly)

Then change your save action body to:

TWIG
        body: "{% include 'forms/data.json.twig' %}"
5 years ago

@Karmalakas thanks for the answer. I took this as a start and finally found a solution.
The twig makes from a contact form a json record. I generate a unique random code as key.

TWIG
{% macro render_field(form, fields) %}
{% set record = [ ] %}
{% for index, field in fields %}
{% set value = form.value(field.name) %}
{% set veld = field.label ~ ':' %} 
{% set record = record|merge([veld, value]) %}
{% endfor %}
{% set code = random_string(15)|md5 %}
{{ { (code): record}|json_encode|raw }}  
{% endmacro %}
{{ _self.render_field(form, form.fields) }}

However, when I try to read these records as a database by a flex object it fails.
I don't know the solution for this or is the twig not good? Who does?

Or is there another solution that can be created FI a twig file where a front-end form can be presented as a flex-object?

5 years ago

Not sure what you mean by:

However, when I try to read these records as a database by a flex object

Could you explain in more detail how you want to access this JSON file?

5 years ago

As start I used https://github.com/trilbymedia/grav-plugin-flex-objects. Instead of “Contacts”, referred in this document, I created an own json file with records. I can add, edit, delete records in admin (the back office) and present them with pages in the front office.
What I want is that I can add records with a contact page ( front office) to this json file. With Admin I can edit the records or even delete then when action has taken place.
Hopefully this is more clear?

5 years ago

OK, so you want to save an object when someone fills the form on a page in front-end (front office)? If so, there are some bad news - flex objects still have some limitations:

Frontend only has a basic routing; for your custom tasks, such as saving, you need your own implementation

5 years ago

Basically yes. I think I start using the database plugins. I had hoped using Grav a database solution was not necessary.

5 years ago

How would database plugin help you there? You'd still need to implement custom save solution.

5 years ago

I probably don't understand your problem at all. You asked how to save form data to JSON. I answered, but then you say that's not what you actually need 😕 You need to read the data and not save it.. I'm confused

5 years ago

Sorry that I comfused you. Probably I did not explain my case very well.
An ultimate try. My idea was:

  • a frontend form must generate a json file with records.
  • with an another admin form (created with flex) you can edit, delete these records from the created json file.
    Thats all.
5 years ago Solution

@epb,

a frontend form must generate a json file with records.

Any front-end request can be caught by a plugin which writes the posted form into a json file:

PHP
$file = File::instance($filename);
$file->save(json_encode($data));

For a complete example you can take a look at the Comments plugin which writes the contents of a comments form into a Yaml file.

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 47 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 72 2 months ago