Reflexa

$user->name in a hook for pages

Recommended Posts

Reflexa    0

Hello,

I've got users this way:
user01
user02
...
user99

And I've got a template "Verkaeufergruppen" with the fields "title" and "bezei" this way
01 Adams
02 Brown
03 Smith

The rule is: user01 = 01 Adams, user02 = 02 Brown. 

When a new page with the template "Bericht" is created:
The field "ADM_Name" (=text) should be set automatically to "Adams" if user01 has created the page.

That's why I use the PHP function substr, so user01 --> 01, user02 --> 02, ...

I made a hook in ready.php:

  $wire->addHookAfter('Pages::saveReady', function($event) {
    $page = $event->arguments(0);
    if($page->template != "Bericht") return;
    $admnr = substr($user->name, 4, 2);
	$admname = $pages->findOne("template=Verkaeufergruppen, title=$admnr")->bezei;
	if(trim($page->ADM_Name) == '') $page->ADM_Name = $admname;	

But it doesn't work. I seems it's not possible to take "$user->name". 

Perhaps somebody can help

Many thanks.

Share this post


Link to post
Share on other sites
adrian    7,645

You're running into PHP scope issues. You will need to use:

$this->wire('user')->name;

Same goes for $pages - use: $this->wire('pages')->findOne .......

  • Like 1
  • Thanks 1

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.