11 votes
Accepted

User events in Craft 3

There aren't specific User element save events in Craft 3, but you can listen to EVENT_AFTER_SAVE_ELEMENT and check if the element is a user like so: Event::on(\craft\services\Elements::class, \craft\...
  • 66.5k
5 votes

How to add addition attribute and fields in public registration form in Craft CMS

Before you can allow any type of user groups you will need at a minimum of the Client version of Craft. To allow public registration, you will need the Professional version. Only the Professional ...
  • 4,696
4 votes
Accepted

RelatedTo with users

You can just use: {% for entry in craft.entries.section('downloads').relatedTo(currentUser) %} {{ entry.title }} {% endif %} This is essentially returning an ElementCriteriaModel of reverse ...
4 votes
Accepted

Sending email by key to non-user

Just because EmailService::sendEmailByKey() requires you to pass in a UserModel, doesn’t mean that the model has to represent an actual saved user. You can create one on the spot for them: $user = ...
4 votes
Accepted

How do I set the user password when using saveUser service method?

Use newPassword instead: $user->newPassword = '123456';
  • 6,401
4 votes

User events in Craft 3

You can also try User::EVENT_BEFORE_SAVE. I'm not sure that it's documented anywhere, or if it was added after Brad responded, but it might work too. use yii\base\Event; use craft\elements\User use ...
3 votes

Saving/assigning User Group via a plugin

So it seems the only way to achieve this is to assign the user to the group, not the other way round (assign the group to the user). So once my user has saved I can call following: craft()->...
  • 1,731
3 votes
Accepted

Sharing files with logged-in users

Welcome to the community Adam. Yes. Completely possible with vanilla Craft. There's always more than one way to achieve things but here's a first round. Start by creating two User Groups; ...
  • 1,538
3 votes
Accepted

`onBeforeSaveUser` - Access existing user data

Not totally sure, but I believe this may be because the UsersService actually caches UserModels fetched by getUserById() locally in a private class variable. Instead of using getUserById(), try ...
3 votes
Accepted

Fetching "Pending" Users Related To Entry Within Plugin

As always, spent hours trying to figure this out and came upon the answer within an hour after posting the question on Stack. Here's the working code: // get entry $entry = craft()->...
  • 346
3 votes

CometChat integration

The Matt's answer was pretty complete and worked almost, but for the benefit of others, there is the changes required files to make it works: in integration.php find define('SET_SESSION_NAME',''); ...
  • 147
3 votes

Accessing custom fields from User Model

Response via Slack from Brandon Kelly: Accessing custom field values from PHP is the same as in Twig - once you have the UserModel it's just $user->myCustomFieldHandle or $user->...
  • 1,731
3 votes
Accepted

Trying to capture categories field type data in user profile save/update

Usually, its very easy to pull data from a relational fieldtype (Categories, Entries, Assets etc). E.g. if your Categories field handle is memberGroups, you can simply do this: $categories = $user-&...
3 votes
Accepted

Searching for users from a plugin

Find a single user by his/her affiliateId (int). $criteria = craft()->elements->getCriteria(ElementType::User); $criteria->affiliateId = 1; $users = $criteria->find(); Query all users ...
  • 66.5k
3 votes

I cannot get the email address to properly update from my user account management form

As of Craft 2.3, the rules for changing an existing password are: If you're editing your existing account OR you're an administrator OR you have the 'changeUserEmails' permission set on your user ...
  • 66.5k
3 votes
Accepted

Remove username from photoUrl path

Since I use email addresses as usernames, I don’t think it’s a good idea to expose the email addresses of all users on the web. I agree, and that's something we overlooked when we added the '...
  • 66.5k
3 votes
Accepted

How do I access the properties of an object which triggered an event?

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 ...
  • 23.6k
3 votes

Update User Profile with Plugin

I'm not sure if this is causing your error, but it's recommended to use setContentFromPost() or getContent()->setAttributes() so that craft has a chance to prep the field values, as discussed in this ...
3 votes
Accepted

Add Checked Attribute to Custom User Fields on Frontend User Form

Solved! {% for letter in currentUser.newsletters.options %} <input type="checkbox" value="{{ letter.value }}" name="{{ letter.value }}" id="{{ letter.value }}" {% if letter.selected == ...
3 votes
Accepted

Saving Custom Field Values to User Profile

I used August's answer, but fixed it: Put this at the top: use craft\elements\User; Then the updating code $myUser = Craft::$app->getUser()->getIdentity(); $myUser->setFieldValue('...
3 votes
Accepted

Batch saving a custom field to multiple users via controller

For such large amounts of operations it's best to use a Job. You should only make sure to batch your results and don't fetch every user separately. use Craft; use craft\base\Element; use craft\db\...
2 votes
Accepted

Processing Business Logic on user.saveUser

The user has already been saved at this point and is available as $event->params['user']. If you modify data and save again, however, you will need to be careful of creating an endless loop — not ...
2 votes

Getting the UID of a user model

If you want to get a user model based on UID you can do this. For current user.. Get current user $currentUser = craft()->userSession->getUser(); Get user record $currentUserRecord = $this-&...
2 votes
Accepted

Getting the UID of a user model

Currently, Craft doesn't expose the UID in any of its models. Kind of a pain, but the only thing I can think of as as a workaround is to write a plugin that has a UserModel with the UID and mimics ...
  • 66.5k
2 votes
Accepted

How can I return all user custom profile fields in an array?

Custom field data is accessible via currentUser.getContent(). Using dump: {{ dump(currentUser.getContent()) }} Or to output as string: {{ currentUser.getContent()|join(', ') }}
2 votes
Accepted

Require password from current user on front-end form?

You'd create a Craft plugin with a controller that has an action, say actionSaveForm(). Your controller would look for a password POST variable and if that existed, validate that against the ...
  • 66.5k
2 votes

Conditional for user registration fields

Looks like I needed to set $user->username = craft()->request->getPost('email'); for my attempt to work.
  • 1,128
2 votes

How to activate a user in a plugin?

You'll need to save the new user before you can activate them. This code should do the trick: if (craft()->users->saveUser($newUser)) { craft()->users->activateUser($newUser); }
  • 6,401
2 votes
Accepted

CometChat integration

So, the Comet Chat is not able to read the userid, even if I made a Craft plugin who writes UserID in $_SESSION['userid']. To access Craft's session variables outside Craft, you need to pull the ...
2 votes

currentUser global is null. I can't figure out why

Per Brad's request. I am answering my own question. I figured out that I was logging in via non-www, and accessing the site at www. So the official answer is to choose either www or non-www. I chose ...
  • 306

Only top scored, non community-wiki answers of a minimum length are eligible