Search code examples
phpdrupaldrupal-7

Display text for users with certain roles


I have a piece of text in a custom page--front.tpl.php file. I would like to wrap this in a php if statement so that it is displayed only to two of my site roles. I have the below but it only displays for the "client" role whereas I would also like to display it for the"consultants" role.

<?php if (in_array('client', $GLOBALS['user']->roles)):?>

Client profile


Solution

  • in_array function should accept multiple search parameters if they are passed as an array. So it should be something like:

    <?php if (in_array(array('client','consultants'), $GLOBALS['user']->roles)):?>
    

    But if that's not working (and it should) you can always use or statement:

    <?php if (in_array('client', $GLOBALS['user']->roles) || in_array('consultants', $GLOBALS['user']->roles)):?>
    

    Update: it seems that in_array() can't accept an array for first (needle) parameter. Check out this thread on stack overflow:

    Checking to see if one array's elements are in another array in PHP

    So array_intersect() function should be what you are looking for.