How? Getting a select's user attribute value
Permalink
I have a user 'select' attribute, e.g. Apple, Pear, Banana
userinfo->getAttribute returns an object which I'm guessing is a Select object rather than the selected value which I was expecting, so having spent too long searching examples and looking at attribute API information I'm still no closer to getting the actual VALUE of what's selected in that user attribute. How do you get it?
I can put 'display' into the getAttribute call if I want and strip the html out or the returned text but that's just wrong and I don't want to do that kludgery.
userinfo->getAttribute returns an object which I'm guessing is a Select object rather than the selected value which I was expecting, so having spent too long searching examples and looking at attribute API information I'm still no closer to getting the actual VALUE of what's selected in that user attribute. How do you get it?
I can put 'display' into the getAttribute call if I want and strip the html out or the returned text but that's just wrong and I don't want to do that kludgery.
Isn't $ui just a UserInfo object there? The UserInfo->getAttribute(<handlename>) was the very first thing I tried which came back with an object not a value which I then had to decipher.
Try that with a select attribute instead of a string (which I assume your example uses?).
FYI my users were coming back from a result list created by filtering users on usergroup though they should still be UserInfo objects, right?
Try that with a select attribute instead of a string (which I assume your example uses?).
FYI my users were coming back from a result list created by filtering users on usergroup though they should still be UserInfo objects, right?
It's the combination of those 3 lines to get the correct output.
I know I had to figure this out too, because of the ui object.
I know I had to figure this out too, because of the ui object.
Will try it that way right now and see!
Nope, 'fraid not, you still get an object rather than the attrib value in the case of a select attrib:
And XDEBUG shows the same object type returned in $type - see scrshot attached.
Think I'll have to stick the the somewhat Heath Robinson method I uncovered for selects.
foreach($provider_list as $provider) { $ui = UserInfo::getByID($provider->getUserID()); $type = $ui->getAttribute('userprovidertype'); //$type = $provider->getAttribute('userprovidertype')->getSelectedOptions()->current()->getSelectAttributeOptionValue(); $providers[$type][] = $provider; }
And XDEBUG shows the same object type returned in $type - see scrshot attached.
Think I'll have to stick the the somewhat Heath Robinson method I uncovered for selects.
C5.7
This is part of what I use based on the memberlist of the current loggedin users' group (output as table). This 'client_name' is coming from a user attribute type select
This is part of what I use based on the memberlist of the current loggedin users' group (output as table). This 'client_name' is coming from a user attribute type select
$group = Group::getByName('Clients'); $members = $group->getGroupMembers(); foreach($members as $member) { if (trim(h($member->getAttribute('client_name'))) == $clientName && $clientName!=''){ echo('<tr>'); echo('<td>' . trim(h($member->getAttribute('client_name'))) . '</td>'); echo('<td>' . $member->getUserName() . '</td>'); echo('<td>' . $member->getUserEmail() . '</td>'); echo('</tr>'); } }
I might have finally found how to do it. $provider is a UserInfo object, userprovidertype is the attribute handle for a single-choice select attribute. I don't even have any clue if this is the right way to do it as there's no docs that I found for selects.
It's taken 3 hours to discover this including reverse engineering core code to get clues for where to look which no-one should have to be doing. That makes C5 a very expensive platform to develop in where some simple, complete examples for all attribute types instead of one or two examples which can't be applied to e.g. selects, would have saved the end client 2+1/2 hours on his bill today (or rather got 2+1/2 hours further towards completion).
$type = $provider->getAttribute('userprovidertype')->getSelectedOptions()->current()->getSelectAttributeOptionValue();
It's taken 3 hours to discover this including reverse engineering core code to get clues for where to look which no-one should have to be doing. That makes C5 a very expensive platform to develop in where some simple, complete examples for all attribute types instead of one or two examples which can't be applied to e.g. selects, would have saved the end client 2+1/2 hours on his bill today (or rather got 2+1/2 hours further towards completion).
I agree. This is very difficult given the paucity of documentation. Have you tried the methods in
https://documentation.concrete5.org/developers/attributes/attribute-...
I've used:
https://documentation.concrete5.org/developers/attributes/attribute-...
I've used:
$userInfo = UserInfo::getByID($user->getUserID()); $valueObject = $userInfo->getAttributeValueObject('units'); if ($valueObject != false) { $units = $valueObject->getPlainTextValue(); ...
I'll check that out also, thanks!
I have a select attribute where you can choose your company name from a list:
And where I need it: