Wagtail set default page explorer view location for PageChooserBlock in admin

445 Views Asked by At

Way to browse pages from particular location in PageChooserBlock, like browse "sub pages or same level pages" from current editing page in which this block is used. I observed that when any page is selected in PageChooserBlock then browsing starts from the same current selected page. I wanted to start default browsing from current edting page (in which this PageChooserBlock block is used) if no page is already selected. Is there any way to do it? I googled for some way or direction for doing it but no success. I also checked chooser hook but there is no way to get current editing page in this hook. So can't use it. Also tried to search in source code of PageChooserBlock for such possibility but no success for me or may be I missed the possible way. Your help is appreciated.

2

There are 2 best solutions below

0
On

I am able to achive the expected result using suggestion of @rgs258 with some modifications done in JavaScript. I subclassed AdminPageChooser and implemented media method and added some sort of hack for getting pageid of editing page as below in JavaScript. I think(know) this is not the perfect way but at least I am able to achive my expected result. Please suggest if there is any better way to do it.

I created new JavaScript file with the below coad and added it in newly implemented media method after page-chooser.js

if(typeof createPageChooser === 'function'){

    let  originalCreatePageChooser = createPageChooser;
    createPageChooser = function(id, pageTypes, openAtParentId, canChooseRoot, userPerms){
        if (!openAtParentId){
            let path = window.location.pathname;
            openAtParentId = path.replace('/admin/pages/', '').replace('/edit/', ''); //Know it is not the best way but ...
        }
        originalCreatePageChooser(id, pageTypes, openAtParentId, canChooseRoot, userPerms);
    }
}
1
On

I've not done exactly what you seek to do, but I think you may want to subclass AdminPageChooser and implement render_js_init in order to define a default parent of your choosing.

You can then subclass PageChooserBlock and implement widget in order to return an instance of your subclass of AdminPageChooser.

I gave this a quick go and it seemed to work.