Generating virtual page from local page + set template and parent page

Hi,

I’m trying to use routing and the virtual page system in order to display a local page with a different template and a different parent if a route match the desired pattern.

So far here is what I in my routes :

    [
        'pattern' => 'artists/(:any)/highlights/(:all)',
        'language' => '*',
        'action' => function ($language, $artistId, $pageId) {
            $page = page($pageId) ?? null;
            if (!$page) {
                return site()->visit(site()->errorPage());
            }

            $pageDatas = $page->toArray();
            $pageDatas['parent'] = site()->find("artists/$artist/highlights");
            $pageDatas['template'] = 'artist-'.$page->intendedTemplate();

            return site()->visit(new Page($pageDatas));
        }
    ],

With this routing, if I have a route like /artists/artist-1/highlights/articles/article-1, my aim is to transform the exiting page /articles/article-1 into a virtual page, to change its template (from article to artist-article) and to change it’s parent so I can use my virtual page to generate a breadcrumb as if it was a descendent of /artists/artist-1/highlights.

It was working well so far but now I have a local page which has files, and it generates this ErrorException : Illegal string offset 'collection' which happen when the new Page() is created. I guess it is related to the fact the virtual page files somehow doesn’t exist but I’m not sure…

Any hints or advices on how to better virtually clone a page from an existing one (which has files) and change its parent and template?

Thanks!

I wonder if it would make more sense to create the virtual pages as children of the highlights page type rather than on the fly.

Or you would need a page model for the artist-article template where you redefine the files of the original page as belonging to the virtual page.

Another problem I see with your approach (independent of how you create the virtual pages) is duplicate content when your pages is available as both a standard page and virtual page.