0

I am trying to capture the data that is selected when a user selects categories within a categories field type (handle: groups) within the Control Panel profile tab.

I was looking thru the Fields, Categories and Content services documentation in the craft docs section and I am still unsure.

I noticed when I did a var_dump of $user:

$user = UserModel::populateModel(UserRecord::model()->findById($id));

It displays the field 'groups' within the _attributeConfigs, but does not show up in private '_attributes' (Craft\BaseModel)

The other fields do show up, for example, occupation shows up within both the _attributeConfigs (its a string) and shows up in private '_attributes' (Craft\BaseModel) with a value of 'Teacher'.

I am unsure how to get the data the user selected when selecting the categories while updating their profile within the control panel.

3
+50

Usually, its very easy to pull data from a relational fieldtype (Categories, Entries, Assets etc). E.g. if your Categories field handle is memberGroups, you can simply do this:

$categories = $user->memberGroups;

However, your Categories field handle is groups, which conflicts with the built-in UserModel attribute groups, used to pull any user groups the user is a member of – this means that calling $user->groups will give you an array of UserGroupModel instances, ignoring your Categories field.

Also, the $user->getContent()->getAttribute('groups'); method doesn't appear to work for relational fields.

The easy answer: I'd suggest you simply change the field handle for your Categories field to something else – for example memberGroups. Don't worry, you won't lose any data if you do. Then you can just do $categories = $user->memberGroups;.

If you can't or wont change the field handle, here's a workaround:

$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->relatedTo = array(
    'sourceElement' => $user,
    'field' => 'groups'
);
$categories = $criteria->find();
  • Hi Mats - Sorry for confusing you, the "groups" is the handle of a custom category field type I assigned to the users field within the CP users settings fields page (/admin/settings/users/fields). Your code returns the user groups that were created at /admin/settings/users, if the user w/ that id is assigned any of those groups. I am looking for the custom field type: "groups" – Brad Apr 6 '16 at 15:20
  • If I do: $user->getContent()->occupation; it shows the occupation (is custom field type plain text) field data entered. But If I do $user->getContent()->groups; its blank. The groups field shows up within Craft\ContentModel Object ( [_requiredFields:Craft\ContentModel:private] => [_attributeConfigs:Craft\ContentModel:private] => Array – Brad Apr 6 '16 at 15:37
  • It prints out the group in that array: [groups] => Array ( [model] => [column] => text [type] => mixed [label] => Groups ) – Brad Apr 6 '16 at 15:38
  • 1
    Aaah, right... You're absolutely correct, $user->groups will output any groups (UserGroupModels) the user is in. If your field had a different handle, e.g. groupCategories, my original answer would work (i.e. $user->groupCategories). It's actually a bit weird that Craft allows groups as a field handle; pretty much every other built-in attribute for any of the element models is a reserved word and can't be used for field handles. Maybe this is an oversight on P&T's behalf, you might want to shoot them an email at support@craftcms.com. – Mats Mikkel Rummelhoff Apr 6 '16 at 17:12
  • 1
    @Brad Just edited my answer, including a workaround if you don't want to change the field handle. Hope it works out! – Mats Mikkel Rummelhoff Apr 6 '16 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.