XF 2.1 Communication/Notices

Graham Smith

Active member
Have just configured a notice in xenForo 2.1.

We have a number of custom user fields such as one called "forename"

However, when I use {$xf.visitor.Profile.custom_fields.forename} within a Notice, it doesn't work.

Am I trying to do something that xenForo can't?
 
According to Jake's post from 2011 normal container parameters aren't passed in. There's only specific tokens available.


In src -> XF -> NoticeList.php you can see the code for it, which corresponds to the description in the ACP.

Only {user_id}, {name} and {title} is available I believe without making an addon.

PHP:
    protected function getTokens()
    {
        return [
            '{user_id}' => strval($this->user->user_id),
            '{name}' => $this->user->user_id ? htmlspecialchars($this->user->username) : \XF::phrase('guest')
        ];
    }

    public function addNotice($key, $type, $message, array $override = [])
    {
        $notice = $override + $this->noticeBase;

        $tokens = $this->tokens;
        $tokens['{title}'] = $notice['title'];

        $notice['message'] = strtr($message, $tokens);

        $this->notices[$type][$key] = $notice;
    }
 
Thanks buddy

So I'll give up my idea of adding the first name on those notices where I need to show user's avatar to grab attention.

I'm using "First-name Surname" as username and I also added "First name" and "Surname" as separated custom field. It would have been cool if we could use one of those, just like for email marketing services.
 
Yeah, it would be very useful to grab custom profile field info in notices. I've needed it in the past and ended up just not using the notice system to pull it in and putting the 'notice' straight into the template with appropriate display conditions so I could access the template variables.
 
Okay, I figured out a way to do it.

If you edit the getTokens function from above to add a token for your firstname custom user field it works.

In my case the Field ID of the first name of the user is first_name

PHP:
    protected function getTokens()
    {
        return [
            '{user_id}' => strval($this->user->user_id),
            '{name}' => $this->user->user_id ? htmlspecialchars($this->user->username) : \XF::phrase('guest'),
            '{firstname}' => $this->user->Profile->custom_fields->first_name
        ];
    }

Obviously it's not advisable to directly edit the files, and the change would be lost in an upgrade. But you might be able to turn this into an addon.

1624991130107.webp
 
Obviously it's not advisable to directly edit the files, and the change would be lost in an upgrade.
In fact i prefer not to touch the main files, just for this reason: do you think it could be possible to do it by a Template modification instead? 🤔

you might be able to turn this into an addon.
Well... I don't anything about programming 😅

Hopefully someone else would be so kind to spend some time for it 😇
 
Top Bottom