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

Outputting custom user fields?

Discussion in 'XenForo Questions and Support' started by maxwolfie, Jun 4, 2012.

  1. maxwolfie

    maxwolfie Active Member

    I was not sure where to post, basically I guess you could call this related to a modification that I am using called "XenStaff" however it is no longer supported and the forum is closed. What I am asking, however is not really specific to this modification, but rather just a general query as to how to do something.

    What I am looking to do is output custom fields that the user has filled out, and note them next to their names on the following page:

    http://www.hackslashrepeat.com/guildmembers/

    For example, for Greg (me), I have a number of Custom User Fields set up that I've filled out, I want those fields to display their names and outputs on the Guild Members page above. The information is basically my gaming name, account name or Battle Tag (in the case of Diablo 3). If you go to any of my threads, such as this one, you will see these custom fields under my avatar. These are the ones that I want to have outputted to the Guild Members page, next to my name. People can then go to the Guild Members page and add everyone to their in-game friends lists, rather than manually trawling the forums to try and find everyone in the guild.

    Please see the attached image to see what I mean.

    The code below is the index for the Guild Members (XenStaff_index) template/page:

    Code:
    <xen:title>{$xenOptions.staffpageTitle}</xen:title>
    <xen:h1>{$xenOptions.staffpageTitle}</xen:h1>
     
    <xen:description>{$xenOptions.staffpageDescription}</xen:description>
     
    <xen:if is="{$xenOptions.staffnoticeActive}">
     
        <xen:if is="{$xenOptions.displaystaffNotice}">
            <p class="importantMessage">
            {$xenOptions.displaystaffNotice}
            </p>
        </xen:if>
     
    </xen:if>
     
    <xen:require css="XenStaff_index.css" />
    <xen:require css="member_list.css" />
     
    <xen:foreach loop="$staffgroups" value="$staffgroup">
     
    <div id="{$staffgroup.title}" class="section">
        <h2 class="staffHeading">{$staffgroup.title}</h2>
     
            <ol class="section memberList">
     
            <xen:foreach loop="$staffgroupsuserids" value="$staffgroupsuserid">
     
                <xen:if is="{$staffgroupsuserid.user_group_id} == {$staffgroup.group_id}">
             
                    <xen:foreach loop="$staffuserinfo" value="$user">
     
                        <xen:if is="{$staffgroupsuserid.user_id} == {$user.user_id}">
     
                    <xen:include template="member_list_item">
                        <xen:set var="$noOverlay">1</xen:set>
                    </xen:include>
     
                        </xen:if>
     
                    </xen:foreach>
     
                </xen:if>
     
            </xen:foreach>
     
        </ol>
    </div>
     
    </xen:foreach>
     
    <xen:sidebar>
     
              <xen:if is="{$staffonline}">
     
            <div class="section staffOnline avatarList">
                <div class="secondaryContent">
                    <h3>{$xenOptions.staffonlineHeading}</h3>
                    <ul>
     
                        <xen:foreach loop="$staffonline" value="$user">
                            <li>
                                <xen:avatar user="$user" size="s" img="true" />
                                <a href="{xen:link members, $user}" class="username">{xen:helper richUserName, $user}</a>
                                <div class="muted">{xen:helper userTitle, $user}</div>
                            </li>
                        </xen:foreach>
     
                    </ul>
                </div>
            </div>
     
              </xen:if>
     
    </xen:sidebar>
    And I'm guessing that these lines from message_user_info (below) can be used in the above, but I'm just not sure how.

    Code:
                <xen:if is="@messageShowCustomFields AND {$user.customFields}">
                <xen:hook name="message_user_info_custom_fields" params="{xen:array 'user={$user}', 'isQuickReply={$isQuickReply}'}">
       
                    <xen:foreach loop="$userFieldsInfo" key="$fieldId" value="$fieldInfo">
                        <xen:if is="{$fieldInfo.viewable_message}">
                            <xen:if hascontent="true">
                                <dl class="pairsInline userField_{$fieldId}">
                                    <dt>{xen:helper userFieldTitle, $fieldId}:</dt>
                                    <dd><xen:contentcheck>{xen:helper userFieldValue, $fieldInfo, $user, {$user.customFields.{$fieldId}}}</xen:contentcheck></dd>
                                </dl>
                            </xen:if>
                        </xen:if>
                    </xen:foreach>
           
                </xen:hook>
                </xen:if>
    Then there's member_list_item which is called in XenStaff_index:

    Code:
    <xen:require css="xenforo_member_list_item.css" />
     
    <li class="primaryContent memberListItem{xen:if $extended, ' extended'}"{xen:if $id, ' id="{$id}"'}>
     
        <xen:avatar user="$user" size="s" class="{xen:if $noOverlay, 'NoOverlay'}" />
     
        <xen:if is="{$extraTemplate}"><div class="extra">{xen:raw $extraTemplate}</div></xen:if>
     
        <div class="member">
     
            <xen:if is="{$user.user_id}">
     
                <h3 class="username"><xen:username user="$user" rich="true" class="StatusTooltip{xen:if $noOverlay, ' NoOverlay'}" title="{xen:string censor, $user.status}" /></h3>
         
                <div class="userInfo">
                    <div class="userBlurb dimmed">{xen:helper userBlurb, $user}</div>
                    <dl class="userStats pairsInline">
                        <dt title="{xen:phrase total_messages_posted_by_x, 'name={$user.username}'}">{xen:phrase messages}:</dt> <dd>{xen:number $user.message_count}</dd>
                        <dt title="{xen:phrase number_of_times_something_posted_by_x_has_been_liked, 'name={$user.username}'}">{xen:phrase likes_received}:</dt> <dd>{xen:number $user.like_count}</dd>
                        <dt>{xen:phrase trophy_points}:</dt> <dd title="{xen:phrase trophy_points}">{xen:number $user.trophy_points}</dd>
                    </dl>
                </div>
         
            <xen:else />
                <h3 class="username guest dimmed">{xen:phrase guest}</h3>
            </xen:if>
     
            <xen:if hascontent="true">
                <div class="contentInfo"><xen:contentcheck>{xen:raw $contentTemplate}</xen:contentcheck></div>
            </xen:if>
     
        </div>
     
    </li>
    Any suggestions?
     

    Attached Files:

  2. Chris D

    Chris D XenForo Developer Staff Member

    Hi Greg,

    You can output the content of custom user fields using this:

    {$user.customFields.fieldID}

    Just replace fieldID with the ID of your custom user field.
     
  3. maxwolfie

    maxwolfie Active Member

    Thanks, I seem to be able to get the "title", but not the value listed after it..

    I.e. I can get Battle.net BattleTag: (but here the users value is not showing).

    Can you post the exact code needed for member_list_info please?
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Reminded of this:

    http://xenforo.com/community/thread...message_user_info-template.20792/#post-314978

    This is for the member list and not XenStaff, but in looking at the code I think the same problem applies.

    Untested but...

    library/XenStaff/ControllerPublic/Index.php

    Add the red code:

    Code:
    <?php
    
    	class XenStaff_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
    	{
    
    		public function actionIndex()
    		{
    
    		$options = XenForo_Application::get('options');
    
    		if (!$options->displaystaffActive)
    		{
    			return $this->responseError(new XenForo_Phrase('access_url_staff_off'));
    		}
    
    
    		$staffGroupsModel = $this->getModelFromCache('XenStaff_Model_StaffGroups');
    		$staffGroupsUserIdsModel = $this->getModelFromCache('XenStaff_Model_StaffGroupsUserIds');
    		$staffUserInfoModel = $this->getModelFromCache('XenForo_Model_User');
    		$staffGroupsUniqueIdsModel = $this->getModelFromCache('XenStaff_Model_StaffGroupsUniqueIds');
    
    		$staffGroups = $staffGroupsModel->getSelectedUserGroups();
    		$staffGroupsUserIds = $staffGroupsUserIdsModel->getListofUsersPerStaffGroup();
    		$staffIds = $staffGroupsUniqueIdsModel->getListofUniqueIds($staffGroupsUserIds);
    		$staffUserInfo = $staffUserInfoModel->getUsersByIds($staffIds, $staffOptions = array('join' => XenForo_Model_User::FETCH_USER_FULL));
    
    		$staffUserInfo = $staffUserInfoModel->prepareUserCards($staffUserInfo);
    
    		$visitor = XenForo_Visitor::getInstance();
    
    
    		if ((!$options->displaystafftoAll) AND !(in_array($visitor['user_id'], $staffIds)))
    		{
    			return $this->responseError(new XenForo_Phrase('access_url_staff_only'));
    		}
    
    
    		$onlineUsers = $this->getModelFromCache('XenForo_Model_Session')->getSessionActivityQuickList(
    			$visitor->toArray(),
    			array('cutOff' => array('>', $this->getModelFromCache('XenForo_Model_Session')->getOnlineStatusTimeout())),
    			($visitor['user_id'] ? $visitor->toArray() : null)
    		);
    
    	        $staffOnline = array();
    
    		if (!empty($onlineUsers))
                    {
    		        foreach ($onlineUsers['records'] AS $staffId)
    		        {
    
    		                if (in_array($staffId['user_id'], $staffIds))
                                    {
                                           $staffOnline[] = $staffId;
                                    }
    
    		        }
    
    		}
    
    		$viewParams = array(
    			'staffgroups' => $staffGroups,
    			'staffgroupsuserids' => $staffGroupsUserIds,
    			'staffuserinfo' => $staffUserInfo,
    			'staffonline' => $staffOnline
    		);
    
    			return $this->responseView('XenStaff_ViewPublic_Index', 'XenStaff_index', $viewParams);
    
    		}
    
    	}
    
    That should allow you to use this code inside of the member_list_item template:

    Code:
    {$user.customFields.field_id}
    
    Note that editing this template will affect both the member list and the XenStaff page since they both use the same template.
     
    maxwolfie likes this.
  5. maxwolfie

    maxwolfie Active Member

    Hi Jake, I tried everything in that thread before making this new one. I just couldn't get it to output the user's entered field data... Only the title.
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Did you try the code change I posted above? That is specific to XenStaff.
     
    maxwolfie likes this.
  7. maxwolfie

    maxwolfie Active Member

    Oh, sorry no I didn't - I'm on my iPhone so I missed it. I'll give it a go. With that code in index.php, what other code do i need exactly to output title:user data, I.e. BattleTag: (users BattleTag) in XenStaff_Index - can you copy and paste the existing code and add it in red?

    Is there an easy way to hide the title:eek:utput if the field has not been filled in as well?
     
  8. maxwolfie

    maxwolfie Active Member

    Ideally just the staff page but if the members list must also change then I guess there's not much we can do.

    Either way if you can write the code into my existing template (not sure which one?) in red, including the "hide if no data code"?
     
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The entire contents of that file is posted. You can add just the red code or copy the entire contents.

    Use this in the template:

    Code:
    <xen:if is="{$user.customFields.field_id}">
    	TITLE HERE: {$user.customFields.field_id}
    </xen:if>
    
     
    MsJacquiiC and maxwolfie like this.
  10. James

    James Well-Known Member

    I thought there was a helper for custom user fields?
     
  11. maxwolfie

    maxwolfie Active Member

    Great, thanks. Since your first code was specific to the staff section, does that mean that I put that last code into XenStaff_Index or member_info_group (and then I'm assuming it will only work for the staff section anyways because the first code was specific to it?)
     
  12. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    This code:

    Code:
    <xen:if is="{$user.customFields.field_id}">
    	TITLE HERE: {$user.customFields.field_id}
    </xen:if>
    
    Goes in this template:

    Admin CP -> Appearance -> Templates -> member_list_item
     
    maxwolfie likes this.
  13. maxwolfie

    maxwolfie Active Member

    Jake, you've done it again! :)
     
  14. bubbl3

    bubbl3 Active Member

    Is it possible to add custom user fields in page_container?

    I'm trying to do this:
    Code:
    <link rel="stylesheet" type="text/css" href="{$user.customFields.user_css}" />
    But it seems to not be able to retrieve the value.

    EDIT: nevermind, solved using $visitor instead of $user.
     
    Chris D likes this.

Share This Page