3

I'm trying my first plug-in; it's to automatically create an entry in a section when a user belonging to a specific user group is activated. However I'm not that hot at PHP and I can't figure out how I would check that the user being activated belongs to a given user group. Here's my basic code from in the main plugin file's init() function:

public function init() {
    craft()->on('users.onActivateUser', function(Event $event) {
        // check the user being activated belongs to the user group 'artist'
        // ???

        // now save an entry on their behalf
        $entry = new EntryModel();
        ...

        $success = craft()->entries->saveEntry($entry);

        if (!$success)
        {
            Craft::log('Couldn’t save the entry "'.$entry->title.'"', LogLevel::Error);
        }
    }
}
  • I'm pretty sure I need to call isInGroup - but I don't understand what I'm calling that on, or how... $event->isInGroup('artists')? – Matt Wilcox Jul 17 '14 at 8:51
3

Within the context of that event, you will have access to:

$event->params['user']

... which is:

A UserModel instance representing the user that has just been activated.

Once you've got your user model, you can do something like this:

$event->params['user']->isInGroup('artists')

On a separate note, I believe the correct syntax is:

craft()->on('users.activateUser' ...

You can ditch the "on" in onActivateUser. Check out the official example...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.