Pulling in custom user fields

Gossamer

Active member
Hello! I've been going through some tutorials and decided to try and tackle my first little modification. Basically, I created a page using a php callback. I've setup a template that displays all users in a specific usergroup. My final goal is to basically group them by a custom field.

Right now, I'm struggling with pulling in the custom field. I'm trying to display a certain custom user field using {$user.customFields.fandom}, but that data isn't being pulled in with the $users object.

This is the code of my php file:
PHP:
<?php

class Goss_CanonList_CanonList
{
    public static function respond(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract $response)
    {
        // fetch recent registrations
        $userModel = $controller->getModelFromCache('Xenforo_Model_User');
        $response->params['users'] = $userModel->getUsers(array(
            'user_group_id' => 5,
            'user_state' => 'valid'
            ), array('limit'=>5));
       
        $response->templateName = 'goss_canonlist';
    }
}
Any advice for a newbie? :)
 
Thanks! I edited that, but it hasn't changed the output at all. I've added this to my template: {xen:helper dump, $users}

And that shows these fields for each user:
["user_id"] => int(3)
["username"] => string(5) "Alice"
["email"] => string(20) "alice@alice23423.com"
["gender"] => string(0) ""
["custom_title"] => string(0) ""
["language_id"] => int(1)
["style_id"] => int(0)
["timezone"] => string(13) "Europe/London"
["visible"] => int(1)
["user_group_id"] => int(5)
["secondary_group_ids"] => string(0) ""
["display_style_group_id"] => int(5)
["permission_combination_id"] => int(9)
["message_count"] => int(0)
["conversations_unread"] => int(0)
["register_date"] => int(1372303871)
["last_activity"] => int(1372303871)
["trophy_points"] => int(0)
["alerts_unread"] => int(0)
["avatar_date"] => int(0)
["avatar_width"] => int(0)
["avatar_height"] => int(0)
["gravatar"] => string(0) ""
["user_state"] => string(5) "valid"
["is_moderator"] => int(0)
["is_admin"] => int(0)
["is_banned"] => int(0)
["like_count"] => int(0)
["warning_points"] => int(0)
["subaccount_parent_user_id"] => int(1)
["subaccount_parent_state"] => string(13) "email_confirm"
["subaccount_user_ids"] => string(0) ""

So, the custom user fields don't appear to be pulling in at all.
 
The custom user fields cache is stored in the xf_user_profile table, so firstly we need to join that table in our getUser query, which can be done by changing your code to the following:
PHP:
$response->params['users'] = $userModel->getUsers(array(
            'user_group_id' => 5,
            'user_state' => 'valid'
            ), array(
            'limit' => 5,
            'join' => XenForo_Model_User::FETCH_USER_PROFILE
            ));

To make your custom fields appear as an array, rather than a serialized string, it would be advisable to pass the users array through the prepareUsers method. So, add something like:
PHP:
$response->params['users'] = $userModel->prepareUsers($response->params['users']);
 
  • Like
Reactions: Bob
Thanks! I edited that, but it hasn't changed the output at all. I've added this to my template: {xen:helper dump, $users}

And that shows these fields for each user:


So, the custom user fields don't appear to be pulling in at all.
Look like you missing query. All custom fields was save on `xf_user_profile`
You need LEFT JOIN (INNER JOIN) `xf_user_profile` to get all custom fields :)
 
Thanks for the response! When I tried the prepareUsers method, it gave me an undefined method error.

I took a look at the User models and only saw prepareUser, so I tried that one instead and it gave me this error:
Undefined index: user_id
  1. XenForo_Application::handlePhpError() in XenForo/Model/User.php at line 1174
  2. XenForo_Model_User->prepareUser() in Goss/CanonList/CanonList.php at line 17
  3. Goss_CanonList_CanonList::respond()
  4. call_user_func_array() in XenForo/ControllerPublic/Page.php at line 46
  5. XenForo_ControllerPublic_Page->actionIndex() in XenForo/FrontController.php at line 313
  6. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
  7. XenForo_FrontController->run() in C:/xampp/htdocs/Xenforo_DEV/index.php at line 13
 
Looks like you're getting multiple users. If that's the case, you have to loop through and join on each user. To test this before setting up the loop change
PHP:
$response->params['users'] = $userModel->prepareUser($response->params['users']);
to
PHP:
$response->params['users'][0] = $userModel->prepareUser($response->params['users'][0]);
 
Last edited:
Thanks for the response! When I tried the prepareUsers method, it gave me an undefined method error.

I took a look at the User models and only saw prepareUser, so I tried that one instead and it gave me this error:
Apologies, I saw the prepareUser method too and just assumed there would be a prepareUsers method. Apparently not.

Try this instead:
PHP:
foreach ($response->params['users'] as &$user) {
$user = $userModel->prepareUser($user);
}
 
Back
Top Bottom