Jump to content

Return the contents of a field when editing a user


sgt.blikey
 Share

Recommended Posts

When on this page (editing a user):

post-2173-0-44121700-1449787026_thumb.jp

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

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.

  • Like 1
Link to comment
Share on other sites

 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

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
});
  • Like 1
Link to comment
Share on other sites

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...