Posted by liliplanet on March 4, 2010 at 9:40am
Hi Everyone!
In a custom template I have the following code that 'if the user is a certain membership, in this case Platinum' cck fields appear of that specific member.
<?php
$account = user_load(array('uid' => $node->uid));
if (in_array('Platinum Member', $account->roles)) :
?>content (cck fields)
<?php
endif;
?>which works fabulously :)
I would like to add another role (Premium Member) to this code as follows:
<?php
$account = user_load(array('uid' => $node->uid));
if (in_array('Platinum Member', 'Premium Member', $account->roles)) :
?>But unfortunately when I add the second role, it does not work ..
Please would you be so kind to show me the correct code to make that work?
Look most forward to any reply, and thank you.
Lilian

Comments
Like this
<?php$account = user_load(array('uid' => $node->uid));
if (in_array(array('Platinum Member', 'Premium Member'), $account->roles)) :
?>
Quentin
Thanx Quentin .. it does not
Thanx Quentin .. it does not seem to work, actually when I add both roles then the info for Platinum Members does not show anymore either .. so with only 1 role, all is good.
As mentioned in my first post, have tried that, somehow it will not read multiple roles, wonder why ?
Any suggestions most appreciated :)
Try this
<?php$account = user_load(array('uid' => $node->uid));
if (in_array('Platinum Member', $account->roles) || in_array('Premium Member', $account->roles)) :
?>
Quentin
If platinum OR premium return TRUE
Your best place to look for why your in_array function is not working, would be the PHP manual entry: http://php.net/manual/en/function.in-array.php
Max's answer above is intuitively right but won't work, because the in_array function is now looking for an array within the $account->roles array that matches his array perfectly.
What you want is in_array to return true if either of your strings are found in the $account->roles array's values. i.e. "If platinum OR premium return TRUE".
When this is done with a great number of "needles" to search for, you may want to add them to an array for manageability, but then you would have to loop through the array and search for strings individually. For two values the following should work.
Maybe just try something like:
<?php$account = user_load(array('uid' => $node->uid));
if (in_array('Platinum Member', $account->roles) || in_array('Premium Member', $account->roles)) :
?>
EDIT: Damn, 2 minutes late lol
That is just so stunning :)
That is just so stunning :) !!
Thank you so much, most appreciated and have a great day ..
Oops spoke to quickly and
Oops spoke to quickly and should have known that it was not that simple ...
Please what I would like is if the user is a Platinum or Premium member (as we have before):
<?php$account = user_load(array('uid' => $node->uid));
if (in_array('Platinum Member', $account->roles) || in_array('Premium Member', $account->roles)) :
?>
<?phpecho $node->
field_event_contact[0]['view'];
?>
<?php} else {
?>
now here is the catch .. if they are not above roles, show this text
<?php}
?>
so presuming there is something to add before
<?php} else {
?>
Thank you so much for any reply ...
Hey Lillian Maybe something
Hey Lillian
Maybe something like this...
<?php$account = user_load(array('uid' => $node->uid));
if (in_array('Platinum Member', $account->roles) || in_array('Premium Member', $account->roles)) {
echo $node->field_event_contact[0]['view'];
} else {
echo "<div> Some normal HTML here </div>";
}
?>
I used the echo there just to avoid having a funny looking message - but you could replace with HTML between < ? php ? > tags as well. I also changed the ":" from line 1 to a "{" because I hate the HTML like syntax - I find it hard to read - although Drupal standards do call for the ":" method in tpl.php files. Standards-schmandards - I go for code-legibility (just kidding)
Im not sure that answers your question thought - am I understanding?
Bryan
--
Bryan Gruneberg
Chief ninja in charge at Perceptum Thought Squad
Google Talk bryan@perceptum.za.net
Skype perceptum-ts
Code?
just wondering why you messing with the code when you can simple create the role in the user management > roles ... and then proceed with granting permissions .. and I missing something here.
As far I can tell you are editing in drupal core, which I constantly get told is a very bad idea because it might conflict with future updates.
I am a n00b though ; )
Thank you arkad for your
Thank you arkad for your reply. Yip, I'm still struggling.
I'm not in Drupal core, it's on a custom.tpl.php ..
Plus it's not a user permission, but users can view something if the person that added the content has a certain role. It's the other way around :)
@arkad - I heard once that
@arkad - I heard once that every-time core is hacked a lolcat gets punished...
Anyhow - @ Lillian. What I usually do when doing this kind of theming is use a preprocess theme function to generate some display variable. So inside the preprocess function ...
<?php
function preprocess_some_custom_theme_call(&$variables) {
// notice the &$ here ... this is pass by reference so the variable can be changed without returning a value from the function
$account = user_load(array('uid' => $variables['node']->uid)); // This is how the theme engine passes to preprocess functions
if (in_array('Platinum Member', $account->roles) || in_array('Premium Member', $account->roles)) {
$variables['event_contact'] = 'The contact details!';
} else {
$variables['event_contact'] = 'Agg shame - no one to contact for you role-less dude';
}
?>
THEN ... in your theme file you can just ...
<?php
// Now in custom.tpl.php
echo $event_contact;
?>
notice how $variables['event_contact'] in the preprocess function turns into $event_contact in the tpl.php file. Yes... its magick!
The main reason to do this is - according to most design patterns - you want to delink your application logic from your display layer. Drupal does a good job of this by the theme engine - but its power can be easily used in the "wrong" way. Truth be told - I do what you are doing a lot of the time as well - but its good to know when we are being bad - so that we can repent when the time comes... ;)
So anyhow - that was just a short rant FYI.
Best
Bryan
--
Bryan Gruneberg
Chief ninja in charge at Perceptum Thought Squad
Google Talk bryan@perceptum.za.net
Skype perceptum-ts
Brian, thank you so much,
Brian, thank you so much, that is absolutely stunning! I used:
<?php$account = user_load(array('uid' => $node->uid));
if (in_array('Platinum Member', $account->roles) || in_array('Premium Member', $account->roles)) {
echo $node->field_event_contact[0]['view'];
} else {
echo "<div> Some normal HTML here </div>";
}
?>
instead of the pre-process function as do not want 'event_contact' to be the same throughout several templates.
More than beautiful :) Have a fabulous day!