sgt.blikey Posted December 10, 2015 Share Posted December 10, 2015 When on this page (editing a user): How does one access the data that is being displayed here from within a module? For example I might want to test whether 'of the roles presented here, which are selected/not selected'. I thought that I would need to test whether the template=user, but the template=admin. public function displayMessage(HookEvent $event) { $p = wire('page'); echo($p->template->name); } Why is that? Link to comment Share on other sites More sharing options...
LostKobrakai Posted December 10, 2015 Share Posted December 10, 2015 All the pages in the backend are admin templates, which hold various process modules, which in turn manipulate or work with other pages. In this case you're just editing an user, but the page you're on isn't the user page, but an instance of ProcessPageEdit on an admin page. Depending on which class and method you're hooking there are different ways on retrieving the actual edited page, which is probably more of your interest. As soon as you've the user object you can just iterate over all available roles and use hasRole() to verify if a user is of that role or not. 1 Link to comment Share on other sites More sharing options...
sgt.blikey Posted December 11, 2015 Author Share Posted December 11, 2015 I see, that's helpful. Depending on which class and method you're hooking there are different ways on retrieving the actual edited page, which is probably more of your interest. I'm exploring at the moment. My intention was to have processwire echo to me something like "Yes, this user has role X", or "No, this user does not have role X". I defined(?) this hook: wire()->addHookAfter('Page::render', $this, 'displayMessage'); i.e. "after this page has been rendered, execute displayMessage", and I was hoping that my funcition would tell me which template is in use, and (mistakenly assuming that it would return the user template) I would then be able to ask further questions and get the data I wanted. Link to comment Share on other sites More sharing options...
LostKobrakai Posted December 11, 2015 Share Posted December 11, 2015 The most obvious hook would be after ProcessPageEdit::loadPage, which does load the page, which will be edited or if you also want to manipulate the form itself ProcessPageEdit::buildForm. $wire->addHookAfter('ProcessPageEdit::loadPage', null, function(HookEvent $event){ $page = $event->return; // loadPage() does return the page }); // or $wire->addHookAfter('ProcessPageEdit::buildForm', null, function(HookEvent $event){ $process = $event->object; // get the ProcessPageEdit instance $page = $process->getPage(); // Method in the ProcessPageEdit module to get the edited page (not global) $form = $event->return; // buildForm() does return the form }); 1 Link to comment Share on other sites More sharing options...
sgt.blikey Posted December 11, 2015 Author Share Posted December 11, 2015 Thank you so much. That's set me on the right path: public function init() { parent::init(); // always remember to call the parent init wire()->addHookAfter('ProcessPageEdit::loadPage', null, function(HookEvent $event) { $page = $event->return; // loadPage() does return the page if($page->template->name == "user"){echo($page->name);} }); } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now