XF 2.0 Add custom user profile field on addon install

Looks to be fairly similar, but using an entity instead of data-writer. I think something like this should work:

PHP:
$field = $this->em()->create('XF:UserField');
$field->field_id = 'fieldId';
// set other fields

$title = $field->getMasterPhrase(true);
$title->phrase_text = 'Title';
$field->addCascadedSave($title);

$description = $field->getMasterPhrase(false);
$description->phrase_text = 'Description';
$field->addCascadedSave($description);

$field->save();
 
Looks to be fairly similar, but using an entity instead of data-writer. I think something like this should work:

PHP:
$field = $this->em()->create('XF:UserField');
$field->field_id = 'fieldId';
// set other fields

$title = $field->getMasterPhrase(true);
$title->phrase_text = 'Title';
$field->addCascadedSave($title);

$description = $field->getMasterPhrase(false);
$description->phrase_text = 'Description';
$field->addCascadedSave($description);

$field->save();
Thoughts on code to check if the field already exists?
 
sorry folks, I know I am resurrecting a rather old thread....


what is em()? in my setup step (XF2.2) I get an undefined on it. I cannot quite figure out how to create it.
I'm trying to add a custom user field as part of the setup. I can do it through data but that's not polite :)
 
as is always the case - hours spent searching, post a question, find the answer on the next read....


Code:
        $em = \XF::app()->em();

What I guess confuses me still is why all examples I see online assume it is part of $this but not in my case. 🤔
 
It always depends on the context. A lot of XenForo objects have quick access methods or properties for the app and entity manager available. If not, you can always get it via \XF::em() when you require it, as the post before your first gave as example.
 
Back
Top Bottom