How to add a set of custom pages in Admin

[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.

@hughbris, I’m afraid I do not quite understand what you are trying to achieve, but…

To have your code redirect to the ‘commit’ page, try the following:

...
$pages->addPage($page);

// Add the following to replace the current page with the newly created page. 
// The new page will be displayed instead.
unset($this->grav['page']);
$this->grav['page'] = $page;

Since the Form action ‘display’ does a redirect, I looked up how the redirect is performed. See user/plugins/form/form.php line 586

Note: I did not test this in event onAdminPage but in onTwigSiteVariables.

Sure, I’ll see if I can think of a clearer way to explain it. The plugin in context is on Github but needs a lot of cleaning up.

@hughbris, The term “page” is a bit ambiguous…

  • It can be a Grav’s Page
  • A webpage

Are you creating, in Admin, a webpage to create a Page, or do you need a custom webpage, with an entry in Admin’s menu. Eg https://mydomain/admin/mycustompage, which is not Page/Plugin/Theme/Config related, and:

1 Like

That’s what I’m trying to do, yes thank you.