1

I'm attempting to allow a user to edit other users via a front-end form. The "Edit users" permission has been checked in the admin but when I submit the form the data doesn't save. For this example, I'd prefer not to use hidden inputs within the form to manipulate the data.

Event::on(
 User::class,
 User::EVENT_BEFORE_SAVE,
 function (ModelEvent $event) {

    //User id is the id of another user, not the sender.

    $userId = $this->request->getBodyParam('userId');
    $user = Craft::$app->users->getUserById($userId);

    $fieldName = 'exampleTextField';
    $user->setFieldValue($fieldName, 'I want this text to populate exampleTextField');

});

1 Answer 1

1

Could you try this at the end of the function::

    $user->setFieldValue($fieldName, 'I want this text to populate exampleTextField')
    // ... more code
    Craft::$app->getElements()->saveElement($user);

Also, I think it's better to use AFTER SAVE rather than BEFORE(It really depends on how your module works)

UPD: Try these variants and also see this question, maybe it will help you

#1

$fields = $user->getFieldValues();

$fields['customHandle'] = 'Custom value';
                    
$user->setFieldValues($fields);

#2

$fields = $user->getSerializedFieldValues();

$fields['customHandle'] = serialize('Custom value');
                    
$user->setFieldValues($fields);

#3

$user->customHandle = 'Custom value';

P.S. By the way, try wrapping the value in an array, maybe that will help you too.

 $user->setFieldValue('userAvatar', [$avatarAssetId]);

UPD2: You have an error in logic. You are trying to save a user (element) that is not a sender. You won't be able to save it in BEFORE/AFTER element save event, as any attempt to save it via:

Craft::$app->getElements()->saveElement($user);

will result in an endless loop. You need to send data to a custom controller and handle the data there as you need. By the way, you will need to check permissions of users manually, if it's necessary.

if ($user->can('editUsers')) {
....
}
6
  • I've attempted to use Craft::$app->getElements()->saveElement($user); in the past but each time I do, it send the module into an infinite loop. This occurs on both BEFORE SAVE and AFTER SAVE, unfortunately. Commented Jul 11, 2022 at 13:24
  • Oh yes, of course I was wrong, never do that, because after saving you try to save again, and then save again... Check update of my answer again
    – RomanAvr
    Commented Jul 11, 2022 at 18:29
  • Much appreciated but I attempted 1-3 using BEFORE and AFTER save, no luck. I can log the original stored value from the variable so it's accessing the correct variable and value assigned to the user but $user->setFieldValues($fields); or $user->setFieldValue('fieldHandle', ['new copy']); can't seem to overwrite the original value even with admin permissions . Commented Jul 11, 2022 at 22:06
  • I saw your mistake. You are trying to find the user and write it down to a new variable altogether, but instead you have to use the user that passes in the variable from the event. You also write in the comments that the user you are trying to save is another user, not the sender. Therefore, as I think, the best solution would be to send your form to your custom controller, where you will process the user as you need. Otherwise, you cannot save another user, as this will cause an infinite loop.
    – RomanAvr
    Commented Jul 12, 2022 at 4:54
  • 1
    Thank you for the explanation. I thought I could simply mimic the hidden input behavior within the module on save (i was able to achieve the desired result with hidden inputs, just didn't want the field data to be visible on the front end). I'll use a custom controller instead. Commented Jul 13, 2022 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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