XF 2.2 Edit custom user field in cron job using admin privileges?

jb-net

Active member
Hi,

I'm creating an addon that is using a cron job to update user's custom field with individual values. I'm doing this using:

PHP:
$user->Profile->custom_fields->field_id = 'individual_value';
$user->Profile->save();

But running the cron I get this error message:

Field field_name is not editable.

I think this is because the custom user field is not user editable. Options of my user custom field:

1622570442308.webp

My Users shouldn't be able to edit it. I just want to store individual additional user data in the field using my newly created cron job.

After some research I found this:

$writer->setOption(XenForo_DataWriter_User::OPTION_ADMIN_EDIT, true);

But this is for Xenforo 1. How can I solve this problem in Xenforo 2?

Thanks!
 
Not sure, but this might do the trick:

PHP:
$user->Profile->custom_fields->field_id = 'individual_value';
$user->Profile->setOption('admin_edit', true);
$user->Profile->save();
 
Code:
$user->Profile->custom_fields->set('field_id', 'value', $editMode);

$editMode can be one of a handful of values depending on the context/restrictions you want:
  • admin
  • user
  • user_pre_reg
  • moderator
  • moderator_user
In this case, you probably want admin which will basically override everything and always be editable.
 
Back
Top Bottom