Jump to content


Photo

How do I extend $user?


  • Please log in to reply
6 replies to this topic

#1 Hani

Hani

    Full Member

  • Members
  • PipPipPip
  • 60 posts
  • 11

  • LocationSanta Barbara, CA

Posted 11 April 2012 - 12:20 PM

I'd like to add some functions to $user. For example I'd like to be able to do the following:

$user->sendThankYou();

I tried creating the following module:


class UserExtended extends User implements Module{

		public static function getModuleInfo() {
				return array(
						'title' => 'Extended User Module',
						'version' => 100,
						'summary' => 'Extends Processwire\'s Core User Module',
						'singular' => true,
						'autoload' => true
						);
		}

		public function sendThankYou() {
				// Do something
		}
}

But I get the following error:

Class UserExtended contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Module::init) (line 18 of /home/hani/public_html/site/modules/UserExtended.module)


At that point, I thought, "Of course. "User" isn't a really a module. It's not in the Modules list. It extends Page."

I'd really appreciate any direction on how to do this.

#2 netcarver

netcarver

    Sr. Member

  • Members
  • PipPipPipPip
  • 428 posts
  • 341

  • LocationUK

Posted 11 April 2012 - 01:27 PM

If you are implementing a module you need to provide a method called "init". If you take a look at an existing module, you should see what arguments are needed for that function.
Steve ☧

#3 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 11 April 2012 - 01:42 PM

/site/modules/UserExtension.module
<?php
class UserExtension extends WireData implements Module {
public static function getModuleInfo() {
  return array(
   'title' => 'User Extension',
   'version' => 101,
   'summary' => 'Extends $user object.',
   'singular' => true,
   'autoload' => true,
   );
}
public function init() {
  $this->addHook('User::sendThankYou', $this, 'sendThankYou');
}
public function sendThankYou($event) {
  $user = $event->data['object'];
  $event->return = "Thank you, $user->name";
}
}

Usage is dead simple:
<?php echo $user->sendThankYou() ?>


#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • LocationAtlanta, GA

Posted 11 April 2012 - 01:44 PM

I suggest not extending the User class, and instead plugin to it with a module

class UserExtended extends WireData implements Module {
    public static function getModuleInfo() { /* return your array here */ }
    public function init() { $this->addHook('User::sendThankYou', $this, 'sendThankYou'); 
    public function sendThankYou($event) { /* do something */ }
}

The above would add a sendThankYou method to all User instances.

Your sendThankYou method can gain access to the $user the method was called on like this:

$user = $event->object;

Your sendThankYou method can also have one or more arguments if you want it to:

$arg = $event->arguments[0];

If you want your sendThankYou method to return a value, do it like this:

$event->return = 'value you want to return';

Looks like Antti beat me to it. :)

#5 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 11 April 2012 - 01:50 PM

Looks like Antti beat me to it. :)


O0 Haha, I knew I had to be fast, this was such a great question.

I don't want anyone to believe building modules for PW is hard, it's not.

#6 Hani

Hani

    Full Member

  • Members
  • PipPipPip
  • 60 posts
  • 11

  • LocationSanta Barbara, CA

Posted 11 April 2012 - 02:30 PM

Ah ha! Thanks guys!

This is fantastic and really helps me to make better modules with better integration. Thanks, again. :)

#7 Soma

Soma

    Hero Member

  • Moderators
  • 3,197 posts
  • 1751

  • LocationSH, Switzerland

Posted 11 April 2012 - 05:44 PM

And I was not here! :D

Very nice example of usage of ProcessWire modules. Even after months, I love it!

@somartist | modules created | support me, flattr my work flattr.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users