Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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);
        }
    }
}
share|improve this question
    
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 at 8:51

1 Answer 1

up vote 3 down vote accepted

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...

share|improve this answer
    
Thanks very much! –  Matt Wilcox Jul 17 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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