Jump to content

Redirect to Lister after Save + Exit via Hook


gebeer
 Share

Recommended Posts

Hello,

for quite a while I found it quite annoying that when you edit a page from a lister view and then save+exit you will end up at the page tree and not at the lister view where you came from. I haven't found a setting that lets you return to the lister view.

I wrote a little hook that does just that for a project where I am using Lister Pro. The lister is called Events and has a fixed url. It lists only one type of pages with template event.
Here's the working hook code that I placed in admin.php:

// redirect to events list on save+exit
wire()->addHookBefore('ProcessPageEdit::processSaveRedirect', function($event) {
	$id  = (int) $this->input->post('id');
	$page = wire('pages')->get($id);
	if($this->input->post('submit_save') == 'exit' && $page->template->name == 'event' && wire('user')->hasRole('member')) $this->session->redirect(wire('config')->urls->admin . 'events/');
});

Now I would like to make this more generic so that it works for any lister.

How can I get the referring URL (other than from $_SERVER['HTTP_REFERER']) to use it instead of the hardcoded URL I have now in the redirect?
And is there any way that I can access properties of the instance of ProcessPageEdit that called processSaveRedirect? The $event->object is the processSaveRedirect method and $this is ProcessWire. How can I access properties of ProcessPageEdit? E.g. $id or $page. ATM I am getting the page id from the $input->post. Obviously I am not a savvy PHP OOP dev and have no idea how to do such things in a proper way :huh:

EDIT:

I know I could open pages from the lister view in a modal to avoid having to redirect. But I don't want to use that option.

$_SERVER['HTTP_REFERER'] gives me the URL of the page that I edited and not the URL of the lister.

Link to comment
Share on other sites

8 hours ago, gebeer said:

for quite a while I found it quite annoying that when you edit a page from a lister view and then save+exit you will end up at the page tree and not at the lister view where you came from. I haven't found a setting that lets you return to the lister view.

Lister Pro has an option to edit links in a modal window.

lister.png

If you don't have Lister Pro I think it would be possible to turn the edit links of the core lister into modal links with a hook or JS.

 

Edit: for non-Pro lister you can make edit links modal using Admin Custom Files. Enable for process "ProcessPageLister" and add file "ProcessPageLister.js"...

$(document).ajaxComplete(function(){
    var $a = $('.PageListerActions .PageEdit').not('.PageAdd');
    $a.addClass('pw-modal pw-modal-large');
    $a.attr('data-buttons', '#ProcessPageEdit > .Inputfields > .InputfieldSubmit .ui-button');
});

 

  • Like 1
Link to comment
Share on other sites

13 hours ago, adrian said:

$event->object->getPage();

should get you the page being edited.

 

@adrian works like a charm, thank you!

@Robin S thank you for the suggestions and for the code! I have Lister Pro but I don't want to edit pages in a modal. But surely your code will come in handy for other projects. Much appreciated.

Only thing that I still need to figure out is how to get the referring URL of the lister that the page was opened from for editing. $_SERVER['HTTP_REFERER'] gives me the edit URL of the page itself.

Any ideas would be much appreciated.

Link to comment
Share on other sites

3 minutes ago, gebeer said:

Only thing that I still need to figure out is how to get the referring URL of the lister that the page was opened from for editing.

The problem with redirecting back to the Lister is that you will lose your current page in the pagination and any filters that were applied, which IMO would be pretty annoying. The nice thing about the modal editing is that everything is just as you left it after you save.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...