Wednesday, May 2nd 2012


Extending core events in concrete5
posted @ 7:09 am in [ concrete5 -PHP -Technology -Web Design ]

We’re building all of our content based sites on concrete5 as it fits in with our LAMP architecture and Zend Framework architecture which we implement a lot with our Magento eCommerce websites.

The platform is ready to go out of the box, but it’s a bit hard to find how to do what you want sometimes so here’s how to extend the core events (add user, login etc.) with your own code

1. Extending the core events

Events are extended using the /config/site_events.php file and contain the event you want to extend, along with the class and method you want to call when this happens, and finally the model that contains that information

Here’s my example extending the user add event, and calling my own class ‘ApplicationUser’ and the method (function) ‘setupUserJoinInfo’

Obviously you only need the PHP tags the first time you create the file and you can overwrite many events in the same file.

2. Create your class

New file outside of the core, so we’re going to create /models/application_user.php and add in our basic class definition


3. Create our method

So in my case I'm going to hook into my method 'setupUserJoinInfo' pass it the new user object (as we know this is being triggered by the 'on_user_add' event)

class ApplicationUser extends Object {

/**
* @param User $uI
*/
public static function setupUserJoinInfo($ui) {
/* Your own code goes here */
}
}

4. Make it do something

In my case I wanted to email the user a one time hash password when their account was registered so I used the User object and the Mail object with a template in the '/mail/' folder called 'account_creation.php' (you can borrow the hash generation code from the core user.php file/class)

It's not that scary once you get your files installed and the Helpers for Mail and Users make it pretty flexible. Good luck!

References:

concrete5

1. System events

http://www.concrete5.org/documentation/developers/system/events/

2. Helpers -> Mail

http://www.concrete5.org/documentation/developers/helpers/mail/

3. Permissions -> Users

http://www.concrete5.org/documentation/developers/permissions/users


Leave a Reply