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

Pulling in custom user fields

Discussion in 'XenForo Development Discussions' started by Gossamer, Jun 28, 2013.

  1. Gossamer

    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:

    class Goss_CanonList_CanonList
        public static function 
    respond(XenForo_ControllerPublic_Abstract $controllerXenForo_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? :)
  2. Gossamer

    Gossamer Active Member

    Any ideas? Sorry if this is a silly question. I'm still learning my way around. :)
  3. James

    James Well-Known Member

    On quick glance your user model should be XenForo not Xenforo. Capitalisation matters!
  4. Gossamer

    Gossamer Active Member

    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.
  5. Jon W

    Jon W Well-Known Member

    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:
    $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:
    $response->params['users'] = $userModel->prepareUsers($response->params['users']);
    Bob likes this.
  6. Nobita.Kun

    Nobita.Kun Well-Known Member

    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 :)
  7. Gossamer

    Gossamer Active Member

    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:
  8. Daniel Hood

    Daniel Hood Well-Known Member

    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
    $response->params['users'] = $userModel->prepareUser($response->params['users']);
    $response->params['users'][0] = $userModel->prepareUser($response->params['users'][0]);
    Last edited: Jul 4, 2013
  9. Jon W

    Jon W Well-Known Member

    Apologies, I saw the prepareUser method too and just assumed there would be a prepareUsers method. Apparently not.

    Try this instead:
    foreach ($response->params['users'] as &$user) {
    $user $userModel->prepareUser($user);
    dwdmadmac likes this.

Share This Page