XF 2.0 Best way to modify the User Title (in all locations of the forum) ?

Scandal

Well-known member
Hello all! :)
I need to modify the user title of some accounts, in all locations of the forum (main user profiles, user profiles popup, thread view - postbits etc).
I don't want to save or modify something in the database. Just to return specific titles in case my_field == true (see below) and user_state == 'disabled'. Just show them on the end user / visitor.

I have apply a new User Entity field via my own addon (let's say it my_field). This is on the entity structure event listener:
PHP:
$structure->columns['my_field'] = ['type' => Entity::BOOL, 'default' => 0];

I tried both following ways but with not 100% succeed.
A. Via a getter on User Entity. I applied this getter on event listener:
PHP:
$structure->getters['custom_title'] = '';
Then a class extension (XFCP_User Entity) with this method:
PHP:
    public function getCustomTitle()
    {
        if ($this->my_field == 1 && $this->user_state == 'disabled') // or true instead of "1"
        {
            return 'A';
        }
       
        if ($this->user_state == 'disabled')
        {
            return 'B';
        }
    }
B. Via a class extension of \XF\Template\Templater.php with method:
PHP:
    public function fnUserTitle($templater, &$escape, $user, $withBanner = false, $attributes = [])
    {
        $main_reply = parent::fnUserTitle($templater, $escape, $user, $withBanner, $attributes);
       
        if ($user->my_field == true && $user->user_state == 'disabled')
        {
            $userTitle = 'A';  
        }
        else if ($user->user_state == 'disabled')
        {
            $userTitle = 'B';  
        }
       
        if ($user->my_field == true || $user->user_state == 'disabled')
        {
            $class = $this->processAttributeToRaw($attributes, 'class', ' %s', true);

            if (!empty($attributes['tag']))
            {
                $tag = htmlspecialchars($attributes['tag']);
            }
            else
            {
                $tag = 'span';
            }

            unset($attributes['tag']);

            $unhandledAttrs = $this->processUnhandledAttributes($attributes);

           
            return "<{$tag} class=\"userTitle{$class}\" dir=\"auto\"{$unhandledAttrs}>{$userTitle}</{$tag}>";              
        }      
        return $main_reply;
    }

For some reason, in some cases the above is not working as expected. It seems that there are cases that the code is not running.

I want to bypass any other addon / class extension and apply the correct titles ('A' or 'B'). It is for a small amount of accounts.

Any idea? :)
 
From the top of my head I think it's supposed to be
Code:
$structure->getters['custom_title'] = true;

I don't think I've ever tried or looked up what happens with other values, specifically ones that evalute to false on a bool comparison. So give that a try.
 
From the top of my head I think it's supposed to be
Code:
$structure->getters['custom_title'] = true;

I don't think I've ever tried or looked up what happens with other values, specifically ones that evalute to false on a bool comparison. So give that a try.
hmm maybe the issue is that by this way we modify the "custom title", not the main "user title"?
The strange on the whole thing is that on my dev board that's working as expected both above ways. On my client's board that's not working always.
I added= '' instead of = true cause the custom title is string. ok I will try this but if you have also another idea let me know :)

Another question: maybe on method
public function getCustomTitle()
... I have to add also a:
return $this->custom_title;
as an end line below the other conditions?
 
Back
Top Bottom