Display Custom Attribute on Profile page

Permalink
How do I display a custom USER attributes on the profile page. Example: I create an "age" attribute and now I want to display it in the users profile.

I know that I can just check the box to show up in the profile page, but it is hard to style it that way because it shows up in the array of the custom attributes.

Is there a way to just call it out manually? Such as <?php echo $profile...> by calling the handle of the attribute?

Any help would be appreciated. Thanks

 
Mainio replied on at Permalink Reply
Mainio
global $u; // current user
$info = UserInfo::getByID($u->getUserID());
echo $info->getAttribute('age');


I think you only referred the "age" as an example, but please be sure never to create an attribute for age, you or your users would have to update that every year. Create birthday instead.

Antti / Mainio
fastcrash replied on at Permalink Reply
fastcrash
it's pretty hard, better leave it as is.

and it will more hard when you checked 'this field required', there is extra div that why you cannot put yor input field beside label. it must be under label. your only control is from form class, float left for label, float right for input.
thats what i experience :)

i think with mainio sugestion your atribut will not dynamic, everytime you add atribut you will add this to your source code, and the query will run one by one, well maybe i'm wrong.
Mainio replied on at Permalink Reply
Mainio
Yeah, sure, I was answering to his question: "Is there a way to just call it out manually?".

Other possibility would be to customize the whole profile view, which I think should be the best option in most cases. Just answered to his question in this one.

Antti / Mainio
fastcrash replied on at Permalink Reply
fastcrash
oh i see, i miss that meaning, sory :)
ralanyo replied on at Permalink Reply
Thanks for the replies.
Yes "age" was just an example.

Just to clarify. So what you are saying is; Mainio's code example would not be dynamic and that would defeat the purpose.

Mainio, you mentioned customizing the profile view. If I understand what you are saying correctly, I think I have done this. But see this code from view file.

<h1>Hello <?php echo $profile->getUserName()?></h1>
        <?php 
        $uaks = UserAttributeKey::getPublicProfileList();
        foreach($uaks as $ua) { ?>
            <div>
                <label><?php echo $ua->getKeyName()?></label>
                <?php echo $profile->getAttribute($ua, 'displaySanitized', 'display'); ?>
            </div>
        <?php  } ?>


Its easy to to style the line that displays the username
<h1>Hello <?php echo $profile->getUserName()?></h1>


My goal using the example I have already used "age". Would be to have it say something like... Hello Username, you are "age" years old. <--just example. I am not really planning to use age.

But as you see it doesn't separate each attribute as it does the username line of code, so placing the age attribute in a certain location seems quite difficult
Mainio replied on at Permalink Reply
Mainio
Yes, that's actually true.

If you want to use the c5 internal structure, I think what you should do here is to create attribute sets where you can place your attributes and display them properly for each attribute set.

So then your view could look like this:
<h1>Username</h1>
<h2>Basin Info</h2>
<div>Birthday: 1975-10-10</div>
<div>Real name: John Doe</div>
<h2>Additional info</h2>
<div>Country: Bahamas</div>
<div>Nickname: Johnny</div>


However, there isn't any proper documentation on how to add those attribute sets, you will have to take a look into the core on how to add them.

So, if you want to go the "easy path", just get the attributes manually as I demonstrated.

Also, I think if you JUST NEED THE AGE, it's really easier to do it manually here. For example, if you had the "date" type attribute for age, there still wouldn't be a proper way to display the age for that date.

So, if you want to do it manually: just use the code above. This is 2-min job.

If you want to use the "dynamic data structure", there's two things you need to do:
- Create a new attribute type for "age" (select date, display age)
- Create attribute sets for the user to display the data in correct places
- Customize the profile view to use those sets and display data for each set in correct places

This one is more like 30-60min job if you already know this stuff.


Antti / Mainio
Mainio replied on at Permalink Reply
Mainio
One possibility would also to be to hardcode some of those attribute keys into the template and style those attributes accordingly:

<h1>Hello <?php echo $profile->getUserName()?></h1>
        <?php 
        $uaks = UserAttributeKey::getPublicProfileList();
        foreach($uaks as $ua) { ?>
            <?php if ($ua->getAttributeKeyHandle() === 'age') : ?>
            <div>
                <p>This is AGE</p>
                <label><?php echo $ua->getKeyName()?></label>
                <?php echo $profile->getAttribute($ua, 'displaySanitized', 'display'); ?>
            </div>
            <?php else : ?>
            <div>
                <label><?php echo $ua->getKeyName()?></label>
                <?php echo $profile->getAttribute($ua, 'displaySanitized', 'display'); ?>
            </div>
tolga replied on at Permalink Reply
tolga
An easier solution would be assigning all custom attributes to their attribute key named variables like this:

foreach($uaks as $ua)
{ 
  ${$ua->getAttributeKeyHandle()} = $profile->getAttribute($ua, 'displaySanitized', 'display');
}


Now we can use each profile attribute with its name directly:

<h1><?php echo $first_name;?> <?php echo $last_name;?>, <?php echo $city;?></h1>
ralanyo replied on at Permalink Reply
Got it. That helps a lot. I think this will work. Thanks again