XF 2.0 How to override existing repository and entity entry

abdfahim

Well-known member
I have added a new group for custom user field with below code
Code:
public function installStep1()
    {
        $this->schemaManager()->alterTable('xf_user_field', function(Alter $table)
        {
            $table->changeColumn('display_group', 'enum')->values(['personal','contact','preferences','new_custom_field_group']);
        });
    }

But I am not sure how to override corresponding entries in Entity and Repository. From the XF2 doc, I learned how to add a new entity, but not sure how to change an existing one.

Can anyone please help?

1) In XF\Repository\UserField (required to show the new group in Admin Custom User Field)

PHP:
public function getDisplayGroups()
    {
        return [
            'personal' => \XF::phrase('personal_details'),
            'contact' => \XF::phrase('contact_details'),
            'preferences' => \XF::phrase('preferences'),
            'new_custom_field_group' => \XF::phrase('new_custom_field_group_phrase')
        ];
    }

2) In XF\Entity\UserField (I am not sure if it is necessary)

PHP:
public static function getStructure(Structure $structure)
    {
        self::setupDefaultStructure(
            $structure,
            'xf_user_field',
            'XF:UserField',
            [
                'groups' => ['personal', 'contact', 'preferences', 'new_custom_field_group'],
                'has_user_editable' => true,
                'has_user_editable_once' => true,
                'has_moderator_editable' => true
            ]
        );

        ............................

        return $structure;
    }
 
Last edited:
Just to add, I have edited those two PHP files manually and everything is working fine. But I guess editing PHP file manually cannot be the way to do it.
 
To extend any class within XF you would use "Class extensions".

They are talked about here:
https://xf2demo.xenforo.com/dev-docs/general-concepts/#extending-classes

Thanks, I missed the last paragraph where it talks about overriding the existing methods, I always extended classes to add new methods.

So it should be like this

Code:
namespace AbdFahim\XF\Repository;

class UserField extends XFCP_UserField
{
    public function getDisplayGroups()
    {
        return [
        'personal' => \XF::phrase('personal_details'),
        'contact' => \XF::phrase('contact_details'),
        'preferences' => \XF::phrase('preferences'),
        'new_custom_field_group' => \XF::phrase('new_custom_field_group_phrase')
        ];
    }
}

And, I guess even though there are multiple UserField.php files (e.g. XF\Repository, XF\Entity), the system will know via the class extension entry in ACP that which class I am targetting to, right?
 
Last edited:
Oh, I guess the system knows it via class extension, where I write the target class name, even if I don't add that use XF\Repository\UserField declaration.
Correct. We write a hint file that references the XFCP class name in that namespace to the class you’re extending.
 
Thanks a lot @Chris.

One more thing, for overriding static method getStructure() in XF\Entity\UserField, do I follow the same or something extra is required?
 
You can actually create a code event listener to modify an entity structure, and generally this is the preferred approach.

Look for the "entity_structure" event under Admin > Development > Code event listeners > Add
 
@abdfahim Can you share how you got the entity_structure event to work? I'm running into basically the same issue and that's the one thing I haven't worked out yet.
 
Top Bottom