XF 2.2 Is there a way to see if a profile field has been updated on preSave or postSave of a user?

robdog

Well-known member
I know there is an isUpdated() function to check for specific entity values. But any suggestions on something similar for a profile field that has been updated? Thanks.
 

Chris D

XenForo developer
Staff member
The simplest approach is probably to extend the XF\Entity\UserProfile class and the _preSave or _postSave methods or use the entity_pre_save or entity_post_save code events, ideally.

In either case, you can do a number of things:

PHP:
if ($entity->isChanged('custom_fields'))
{
    $previousValues = $entity->getPreviousValue('custom_fields');
    $newValues = $entity->getValue('custom_fields');

    // do stuff to compare values
}

Another approach is to extend the XF\CustomField\Set class and the set method. This happens as the values are set, but this is before the UserProfile::_preSave method is called.
 

Chris D

XenForo developer
Staff member
Using the code event listener is preferred as it is less susceptible to mistakes that might prevent other pre save code from running. However, if you already have to extend the class, for example to add additional methods to the entity, then just extending the class and the required methods is acceptable.
 
Top