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.

surefyre
 
c5dragon replied on at Permalink Reply
c5dragon
C5.7
I have a select attribute where you can choose your company name from a list:
$u = new User();
$ui = UserInfo::getByID($u->getUserID());
$clientName = trim(h($ui->getAttribute('client_name')));

And where I need it:
<?php echo $clientName; ?>
surefyre replied on at Permalink Reply
surefyre
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?
c5dragon replied on at Permalink Reply
c5dragon
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.
surefyre replied on at Permalink Reply
surefyre
Will try it that way right now and see!
surefyre replied on at Permalink Reply 1 Attachment
surefyre
Nope, 'fraid not, you still get an object rather than the attrib value in the case of a select attrib:

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.
c5dragon replied on at Permalink Reply 1 Attachment
c5dragon
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
$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>');
 }
}
surefyre replied on at Permalink Reply
surefyre
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.

$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).
Gondwana replied on at Permalink Reply
Gondwana
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:
$userInfo = UserInfo::getByID($user->getUserID());
    $valueObject = $userInfo->getAttributeValueObject('units');
    if ($valueObject != false) {
        $units = $valueObject->getPlainTextValue();
         ...
surefyre replied on at Permalink Reply
surefyre
I'll check that out also, thanks!