XF 2.1 Extend User.php Entity getStructure

Sysnative

Well-known member
Hi all,

I'm looking to overwrite a specific value set in the static getStructure($structure) function in the User Entity class.

I'd like to override one of the specific columns in the getStructure method and have a question on syntax:

PHP:
class User extends XFCP_User
{

    public static function getStructure(Structure $structure)
    {
        $results = parent::getStructure($structure);
        $results->columns['custom_title'] = ['type' => self::STR, 'maxLength' => 100, 'default' => '',
        'censor' => true
    ];
        return $results;
      
    }
}

Will the above be the correct way of extending this, or should I be using the $structure->set function at this stage? I'm looking specifically to override the custom_title maxLength from 50 to be 100.

Edit: possibly being dumb - updated the above code to fix an array typo.

Thanks,
Will
 
Last edited:
Think I have this working with this code:

PHP:
class User extends XFCP_User
{

    public static function getStructure($structure)
    {
        $results = parent::getStructure($structure);
        $results->columns['custom_title'] = ['type' => self::STR, 'maxLength' => 100, 'default' => '',
        'censor' => true
        ];
        return $results;
    }
}
 
The optimal way to do this is as follows:
PHP:
class User extends XFCP_User
{

    public static function getStructure($structure)
    {
        $results = parent::getStructure($structure);
        $results->columns['custom_title']['maxLength'] = 100;
        return $results;
    }
}
The difference here is that your existing code is essentially overwriting the entire definition with your own. With my example above, only the specific maxLength value is being changed.
 
Hmmm... when I move this from my scotchbox setup to a live dev server I'm getting a PHP warning:

Code:
An exception occurred: [ErrorException] [E_WARNING] Declaration of Sysnative\UserTitleCharLength\XF\Entity\User::getStructure($structure) should be compatible with XF\Entity\User::getStructure(XF\Mvc\Entity\Structure $structure) in src/addons/Sysnative/UserTitleCharLength/XF/Entity/User.php on line 0

This happens when I comment out all of my code, leaving only the extension declaration. Should I include all the full declaration of structure before calling the parent function? Alternatively is there a different function/class to extend that calls the getStructure method that would be a better place to make the modification?

The code is working fine on my scotchbox setup and the add-on works as intended - our server setup has strict warnings on though, which seem to be blocking this.
 
I believe it should be: public static function getStructure(Structure $structure)

You may also need to use: use XF\Mvc\Entity\Structure; in your class.
 
Last edited:
I believe it should be: public static function getStructure(Structure $structure)

You may also need to use: use XF\Mvc\Entity\Structure; in your class.

You're right - that fixed it.

It was generating the same error when I tried that variation before, but turns out I'd put use XF\Mvc\Entity\Structure; before the namespace declaration accidentally... :rolleyes:

Looks like everything is working now. Thanks for your help! :)
 
Top Bottom