1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why does {$user.customFields.FieldID} not fetch the value?

Discussion in 'XenForo Development Discussions' started by TheBigK, Mar 13, 2013.

  1. TheBigK

    TheBigK Well-Known Member

    Okay, I wish to display a custom user field (real name) in in place of the user id on XenPorta. I created a custom user field with field ID "realname" and in phrases where it says -

    by {user} at {date}

    I put -

    by {$user.customFields.x} at {date}

    I'm expecting the out put would be

    By (real name entered by the user) at (date) as output.

    What am I doing wrong?
     
  2. ManOnDaMoon

    ManOnDaMoon Well-Known Member

    For phrases vars, replacements are done where the phrase is called, not directly into the phrase.

    You should leave the {user} replacement in the phrase, find where it is called, and call it this way:
    Code:
    {xen:phrase whateverthephrasename, 'user={$user.customFields.x}', 'date={$whateverthedatevar}'}
     
    Lawrence and Jeremy like this.
  3. TheBigK

    TheBigK Well-Known Member

    Okay, can someone tell me how to make this work in Xenporta? I'm bit lost!
     
  4. TheBigK

    TheBigK Well-Known Member

    Here's where it's being called -

    Code:
    <div class="messageContent baseHtml">
                        <div class="postedBy">
                            <span class="posted iconKey"><div class="sticky"></div>{xen:phrase by_x_at_y,
                                'user=<a href="{xen:link members, $news}" class="username">{$news.username}</a>',
                                'date=<a href="{xen:link threads, $news}">{xen:time $news.post_date, 'absolute'}</a>'}</span>
                            <span class="views">({xen:number $news.view_count} {xen:phrase views} / {xen:number $news.first_post_likes} {xen:phrase likes})</span>
                            <span class="comments iconKey"><div class="new"></div><a href="{xen:link threads/unread, $news}">{xen:number $news.reply_count} {xen:phrase comments}</a></span>
                        </div>
     
                        <div class="newsText">{xen:raw $news.messageHtml}</div>
                        <div class="clearFix"></div>
                    </div>
    
    I'm unable to get the real name of the author display o_O
     
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    You need to change $news.username, however depending on how XenPorta constructs the $news variable in the controller, custom fields may not be present in the data.
     
  6. ManOnDaMoon

    ManOnDaMoon Well-Known Member

    As King stated, customFields are not fetched in XenPorta's RecentNews block.

    You should first add these lines in the file library/EWRporta/Block/RecentNews.php

    Find, on line 40:
    Code:
    		foreach ($news AS &$post)
    		{
    
    Replace with (add the red code):
    Code:
    		foreach ($news AS &$post)
    		{
    			$fieldModel = $this->getModelFromCache('XenForo_Model_UserField');
    			$customFields = $fieldModel->prepareUserFields($fieldModel->getUserFields(
    				array(),
    				array('valueUserId' => $post['user_id'])
    			));
    			$post['customFields'] = $customFields;
    
    Then in the template, replace with the red code:
    Code:
    <div class="messageContent baseHtml">
                        <div class="postedBy">
                            <span class="posted iconKey"><div class="sticky"></div>{xen:phrase by_x_at_y,
                                'user=<a href="{xen:link members, $news}" class="username">{$news.customFields.x.field_value}</a>',
                                'date=<a href="{xen:link threads, $news}">{xen:time $news.post_date, 'absolute'}</a>'}</span>
                            <span class="views">({xen:number $news.view_count} {xen:phrase views} / {xen:number $news.first_post_likes} {xen:phrase likes})</span>
                            <span class="comments iconKey"><div class="new"></div><a href="{xen:link threads/unread, $news}">{xen:number $news.reply_count} {xen:phrase comments}</a></span>
                        </div>
     
                        <div class="newsText">{xen:raw $news.messageHtml}</div>
                        <div class="clearFix"></div>
                    </div>
    
    Note that you should test whether the field has a value before inserting it in place of the username. I recommend using
    Code:
    {xen:if $news.customFields.x.hasValue, $news.customFields.x.field_value, $news.username}
    instead of just
    Code:
    {$news.customFields.x.field_value}
     
  7. TheBigK

    TheBigK Well-Known Member

    Super! That really works. I just had to replace 'twitter' with 'realname' (which is my custom filed) and it worked. Tried this on test site and will make it work on my live site soon :) . Thanks a TON!
     
  8. CaptainMorgan

    CaptainMorgan Active Member

    I can't get this to work for me...

    I have a page with user groups that list members in each group. Of the information I'm trying to retrieve is a custom field. I'm trying:

    $user.customFields.taste.techno

    Where taste is the fieldId of the custom field, and techno is an option from a drop down box. I've been hacking away at it for a while now and having no luck.

    The best I seem to be able to do is retrieve the fields from the User Profile model, unserialize it, and var_dump the fields. Matching them to a user's selection shouldn't be necessary - shouldn't I be able to use the above line of code? ($user.customFields.taste.techno).

    Any ideas?
    Thanks
     

Share This Page