Detect Panel Path after page:changeStatus:after Hook

Hello everyone. :slight_smile:

In my plugin is use a ‘page.changeStatus:after’ hook to move the page if her template is article-page to a subfolder that was created before.

 'page.changeStatus:after' => function ($newPage) use ($yearPageBuilder, $blogArticleSorter) {
            if ($newPage->intendedTemplate() == 'blog-article') {
                if ($page->isListed()) {
                    if ($PARENT->intendedTemplate() == 'blog') {

                        $ARTICLE_YEAR_PAGE = $PARENT->findPageOrDraft($ARTICLE_YEAR)->root() . '/' . $page->dirname();
                        $REDIRECT_URL = Kirby()->url() . '/panel/pages/' . $PARENT . '+' . $ARTICLE_YEAR . '+' . $page->slug() . '?language=' . Kirby()->language()->code();

                        Dir::move($ARTICLE_PATH, $ARTICLE_YEAR_PAGE);
                        Panel::go($REDIRECT_URL);
                    }
                }
            }
        }

It works like a charm. The only thing is i dont only get redirected when i am editing the Subpage itself but also when i change the status inside the Parentpage. But I am not able to get an If-else statement to work, because I dont know how to get differences.

I thought about testing the path by using Kirby()->path() for example. But its always the path

panel/dialogs/pages/.../changeStatus

Hans anyone an Idea how I can test where I am editing to make an If-else?

Kindest regards
Mo

$page and $parent are not defined, so you either left something out here or the code will not work.

Why is this check even necessary? Is the blog-article template used in multiple parents?

Hey @texnixe thanks for your quick answer. You are totally right, something is missing. This is the complete hook

'page.changeStatus:after' => function ($newPage) use ($yearPageBuilder, $blogArticleSorter) {
     if ($newPage->intendedTemplate() == 'blog-article') {
        $PARENT = $newPage->parent();
        $ARTICLE_YEAR = $newPage->published()->toDate('Y');
        $ARTICLE_PATH = $newPage->root();

        if ($newPage->isListed()) {
            if ($PARENT->intendedTemplate() == 'blog') {
                $ARTICLE_YEAR_PAGE = $PARENT->findPageOrDraft($ARTICLE_YEAR)->root() . '/' . $newPage->dirname();
                $REDIRECT_URL = Kirby()->url() . '/panel/pages/' . $PARENT . '+' . $ARTICLE_YEAR . '+' . $newPage->slug() . '?language=' . Kirby()->language()->code();

                Dir::move($ARTICLE_PATH, $ARTICLE_YEAR_PAGE);
                Panel::go($REDIRECT_URL);
            }
        }
        if ($newPage->isDraft()) {
            if ($PARENT->intendedTemplate() == 'year') {

                $BLOG_PAGE = page('blog');
                $BLOG_PAGE_DRAFTS_PATH = $BLOG_PAGE->root() . '/_drafts/' . $newPage->dirname();
                $REDIRECT_URL = Kirby()->url() . '/panel/pages/' . $BLOG_PAGE . '+' . $newPage->slug() . '?language=' . Kirby()->language()->code();


                Dir::move($ARTICLE_PATH, $BLOG_PAGE_DRAFTS_PATH);
                Panel::go($REDIRECT_URL);
            }
        }
    }
}

The Blogarticle gets moved into a “Year” Subpage that gets build in another hook. So I have the folder structure

Blog
    _article_drafts
        unpublished-article 1
    Year2022
        20221109_published-article1
        20221004_published-article2
    Year2021
    Year2020

Unpublished articles move back to Blog->_drafts therefore I ask for the Parent-Intended Template.
This is just “project” code and not necessarily finally.

This is my first plugin with such a complexity and I am not yet very familiar with PHP. So it might be that if clauses are a little over the top or something.

Now I need to figure out if I change status from context menu on a parent page (e.g. ‘year’) in the panel, or if I change the status inside of the article itself to decide if I want to redirect the panel or not.