Jump to content

Get the 'running' page in hooked property.


Recommended Posts

I'm trying to modify the property 'path' for a specific Page (User, actually) using the following code: 

$this->addHookAfter("Page::path",$this,"modifyUserPagePath");

AND

public function modifyUserPagePath(HookEvent $event){

	$p = $event->object;

	if($p->template == "user" && $p->hasRole("ambassador")){

		die("MODIFY THE PATH HERE WITH PREFIX FOR THE CURRENT VIEWING PAGE");
	}

}

But i'm not able to retrieve the page that is currently running (as in, the page in the browser url).

Please note I'm also using the render() method to render subpages content. I don't need the subpages url but only the original $page requested.

Is there a pretty way to do this (besides looking it up via $_SERVER['REQUEST_URI'] )?

Link to comment
Share on other sites

If you need to add a hook only to the current page, you don't need to add it on all Page instances. Instead, you can attach it only to the current page instance:

$page = wire('page');
$page->addHookAfter('path', 'modifyUserPagePath');

Then you don't need your check for the template.

Where are you defining this hook? In an autoload module in the init() method?

Edited by Wanze
See LostKobrakai's post below
Link to comment
Share on other sites

If you need to add a hook only to the current page, you don't need to add it on all Page instances. Instead, you can attach it only to the current page instance.

While this is most of the time right it's not working in this case. You can only hook the path function for all pages, but not for single instances.

Edit: Link to Ryan's statement about the fact: https://github.com/ryancramerdesign/ProcessWire/issues/1108#issuecomment-95933754

  • Like 1
Link to comment
Share on other sites

@MichaMichaMicha

Reading your question again, I think I didn't fully understand it before ;) You can grab the actual page with

$page = wire('page');

Hmm, sorry. That doesn't work. This still returns the User. To be clear:

I'm echoing $someUser->url on a 'normal' page/template. I want the hook to return the url from the 'normal' page appended with the name field for the user. I'm using urlSegments on the 'normal' template to receive the users name.

There are multiple 'normal' pages that can have the same user as a urlSegment (having them in a Page selecting field on the 'normal' template)

These are the some example values that I would like to return in the hook.

/just-a-page/john-doe/ (when the calling Page is "just-a-page")
/just-a-page/jane-doe/ (when the calling Page is "just-a-page")
/another-page/jane-doe/ (when the calling Page is "another-page")
/yet-another-page/lorem-subpage/john-doe/ (when the calling Page is "yet-another-page/lorem-subpage/")

Of course I could just use this in my 'normal' template:

echo $page->url.$theUser->name;

but I'm always trying to learn, so would like to use the Hook :)

Link to comment
Share on other sites

but I'm always trying to learn, so would like to use the Hook :)

I'm not sure if this is such a good idea in this case, as hooking the path can be quite a performance hit, but maybe you could do it this way, so normal pages aren't hooked:

$this->addHookAfter("User::path", $this, "modifyUserPagePath");

AND

public function modifyUserPagePath(HookEvent $event){

	$user = $event->object;
        $page = wire('page');

	if($p->hasRole("ambassador")){

		$event->return = $page->url.$user->name."/";

	}

}
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...