on_before_render in Package - how to get current page (5.7)
Permalink
following situation:
in the "on_before_render" event (in a package), I want to access the current page object , check permissions and do some own logic. But the current page is not available in "on_before_render" event by default.
If I choose another event (and page permissions are set "not visible to guest") there is already done a forward to the login page (if I call the page as a guest).
So question is: which event should I use to check permissions and do some own logic?
What I want to do or why I ask: if a guest doesn't have access to a page, I want to show a 404, not redirect to the login page.
in the "on_before_render" event (in a package), I want to access the current page object , check permissions and do some own logic. But the current page is not available in "on_before_render" event by default.
If I choose another event (and page permissions are set "not visible to guest") there is already done a forward to the login page (if I call the page as a guest).
So question is: which event should I use to check permissions and do some own logic?
What I want to do or why I ask: if a guest doesn't have access to a page, I want to show a 404, not redirect to the login page.
Thank you... but I am working on 5.7.5.7
Have you tried doing something similar? Many things have not changed from 5.6 to 5.7
Create a "on_start" function in the controller.php file of your package.
Within this function, use this:
Should work, right?
Make sure you didn't forget use Events; and use Page; at the start of the file!
public function on_start() { // the on before render code here }
Within this function, use this:
Events::addListener('on_before_render', function () { $c = Page::getCurrentPage(); }
Should work, right?
Make sure you didn't forget use Events; and use Page; at the start of the file!
Hello ramonleenders!
Thank you for your reply. This does not work if the guest has no permission to view the page.
Then this command is not executed. :-(
So I search for a way to get the current / called page, even if there is no permission for the guest.
Thank you for your reply. This does not work if the guest has no permission to view the page.
Then this command is not executed. :-(
So I search for a way to get the current / called page, even if there is no permission for the guest.
OK, I see. A workaround could be to search for the page using the slug. Or look up how the core searches the page? I guess there is no API for what you're looking for. Perhaps some of the core team developers could answer your question though.
you can get the page object like this
public function on_start() { Events::addListener('on_before_render', function($event) { $cID = $event['view']->controller->c->cID; $c = Page::getByID($cID); }); }
According to the docs "If a function hooks into this event, the function is passed the page being rendered as the argument."
This is from the 5.6 docs.