Creating a Gav site page from a Form?

Would anyone know where I might start (if it is even possible) with having a Grav site page created based on a Form submission?

You might want to test with the Grav API Plugin by Flavio Copes. It contains the functionality to create a page (and more). Maybe you can re-use that in a new plugin which processes your form?

Hi Paul, using a custom form action you can have a plugin do anything, in this case creating a page. There is some code in https://github.com/flaviocopes/grav-plugin-api/blob/develop/resources/pages.php#L72-L89 but you can also do it differently by using PHP functions that create a folder and write to a .md file.

Thanks very much @bleutzinn and @flaviocopes ! This might be beyond my skills right now, but gives me some very helpful starting points. Anyone else interested in plugin to do this?

If about a week from now is okay for you I would love to give it a try.

That’s really kind of you to consider that, I look forward to hearing how things go! If I can be of any help re: specifics please contact me directly via my Blog hibbittsdesign.org

Thanks again,
Paul

Now that @bleutzinn is generously exploring the possibility of a ‘CreatePage’ plugin, I thought it would be helpful to share here our initial specification notes for comments/feedback:

  1. It would allow non-authenticated users to use a Grav form to create a Grav page at a specified route (i.e. ‘/Home/Blog/’) using a specified Twig template (i.e. ‘page’ for page.html.twig)

  2. The form would provide two text fields: page title and page content

  3. The results page would use the page title as the basis for the page folder name, and name the markdown file as needed to use the chosen template (i.e. page.md). If the page exists, added needed # for unique name, like the Admin Panel ‘Add Page’ does. If this is tricky, perhaps a dialog saying something like ‘Page exists, please use another page title’ and going back to the form?

  4. The page front-matter would automatically include published: false so the page would not appear before reviewed and approved by the Grav Admin (and set published:true)

Would anyone be able to comment if passing the page route and template name via hidden fields in the Form is a security risk? If so, what might be alternatives?

BTW, @flaviocopes, he is hoping to leverage your API plugin🙂

Thanks very much,
Paul

Did anything come of this? Interested in solving a similar challenge.

So it did, I think. You can have a look at the Add Page By Form plugin I created.

As it is my first plugin I’m not very proud of the code but it gets the job done of adding a page by filling in a form.
You can pass frontmatter variables on to the new page in a very flexible way. For example, by setting the new page’s template variable to item.html.twig you can easily create a new blog item.

Please experiment with it. If you have any questions I’m here to answer them.

@bleutzinn Thanks for the plugin! Works for me. Question: This is a frontend form. How can I do the same thing via a form that is only visible inside the Admin interface?

Well, as far as I understand from the docs, a front end form and a back end form are very similar. I suggest you try to make a working form with it’s own user interface in the Admin. Then change that form to include the content of the ‘Add New Page’ example in the ReadMe. My guess is that the plugin will process the form submission just like it does when the form was submitted from the front end.

“try to make a working form with it’s own user interface in the Admin”
^ Yes thats the part I cant figure out.
Essentially, I’m trying to modify the Item template to include more than just Title, Content, and Media. I need to include a few additional text fields. I reviewed antimatter/templates/partials$ vi blog_item.html.twig where I saw Twig variables like page.title and page.content. I added {{page.myfield1}} then added myfield1: ‘foo’ to the frontmatter in an item page. When displayed in the frontend, that item page does not render the new field. I must be missing something. Any tips?

SOLVED: See https://getgrav.org/forum#!/general:newbie-customization-questi thread.