[This is a cross-post of a Github Admin plugin issue which has no response yet. I must have posted that just before I figured out that Discourse here is still active. Not being able to do this is starting to hold me back, so trying here.]
In a plugin I’m building, I want to set up a page with a form that performs actions that don’t change the content, so a blueprint isn’t appropriate. I could implement this form as an AJAX or server-processed form, preferably the latter. Either way, I need a second page in Admin for either the JSON response or the thankyou/success page.
I’ve looked for inspiration in a few plugins and there seem to be several approaches. It seems like there should be a standard way to achieve this. In an old forum post from 2016, one of the core team even mentioned creating a tutorial and making such pages easier for developers to build (if I understand correctly).
So what I have working is this, which runs off onAdminPage
and uses page->init()
. It works for the first route I try to match. It even matches the route of the second page I try to create, but serves the first. I don’t get it. My code contains inline comments that might explain what’s happening better:
$page = new Page;
switch($uri->path()) {
case $publish_route: // <-- works and serves correct page
$page->init(new \SplFileInfo(__DIR__ . "/admin/pages/publish.md"));
$pages->addPage($page);
$twig = $this->grav['twig'];
$twig->twig_vars['git_index'] = $this->git->statusSelect($path_filter=TRUE, $env='index', $select='MTDRCA');
break;
case "{$publish_route}/commit": // <-- this is being reached but serves the above page; keeping this simple HTML for routing troubleshooting, will want to serve JSON for an AJAX response
$page->init(new \SplFileInfo(__DIR__ . "/admin/pages/commit.md")); // file is found and template defined under admin/templates/commit.html.twig
$pages->addPage($page);
break;
default:
return;
}
I’ll update the Github issue as required.