I have found many articles about showing sections of the user profile edit page based on $user->roles but none that deal with other custom fields.

For example,

global $user;
if (in_array('administrator', $user->roles)) {

else {
  drupal_add_css(drupal_get_path('theme', 'my_theme') . '/css/normal-user.css');
  //Disable admin-only fields
  $fields = array('field_employee_types');
  foreach ($fields as $field) {
    $form[$field]['#disabled'] = TRUE;

BUT what I am needing to do is check a custom user profile field (which happens to be a taxonomy term array) and if the user has a certain term, then do something such as show fields. Can someone lead me in the right direction? I am doing all of this in a custom module to alter profile form.


So I somewhat got the info to print on the user profile page

[field_affiliation] => Array
        [und] => Array
                [0] => Array
                        [tid] => 28
                        [taxonomy_term] => stdClass Object

1 Answer 1


(upon writing back and forth, the solution to this problem is listed underneath Edit 2)

not sure where exactly you are stuck. are you asking how to check which taxonomy term they have assigned in a field, or how to show/hide fields?

if you already have access to the taxonomy term id and you need to further check the content of the taxonomy term, you can assign that id to a variable like$tid, and then do something like this:

$myfield_term = entity_load('taxonomy_term',array($tid))[$tid];

I find myself doing this a bunch of times, because of the way I use taxonomy terms. this gives you an object with all the content of the taxonomy term. if you wanna fetch information out of it, keep in mind that php uses a different approach for objects than for arrays. so if you need the title value, instead of doing $myfield_term['title'], you need to do $myfield_term->title


can you please show us what the structure of the array looks like?
this can be done in various ways. drupal has its own technique of printing arrays (I believe it's caled devel or something), but I still prefer the good old native php way via print_r.

how I'd do it:

  • assign the array to a value, e.g. $user_apartments

  • then in the next line add print_r($user_apartments);

  • switch back to your browser, right click > view page source (may vary depending on your browser, this would be Chrome)

somewhere at the beginning or further down the code (depending on the file that executed the print_r command) should be a printed list of an array, that may look something like this:

[0] => 4
[2] => 11
[3] => 28
[4] => ...

but it may as well be more complicated than this, if for example it not only lists the term IDs but all the content of the terms as well.

  • now just grab it and past a part of it here please (if it is a huge list, no need to post the repetitive part)

the way the print_r command works is it takes the object that you feed into it and displays the content in a more human-readable manner, giving you an insight of its structure, whether something is an object, an array, what else it contains etc.

so depending on what this array looks like, we will need to use a different approach on checking the array for your specific ID, this is why I am asking for an insight of the array we want to check.


so upon looking at the structure of the array, this is how you check for your term ID: the array that we need to look at is ...['field_affiliation']['und']. you can (optionally) assign it to a variable, e.g. $field_affiliation, and then do something like this:

$hasDepartment28 = false;
foreach( $field_affiliation as $thisTerm ) {
    if( $thisTerm['tid'] == 28 ) {
        $hasDepartment28 = true;

then, where- and whenever needed, you can simply check by doing

if( $hasDepartment28 ) {
    // do..

and here is how it works:

  • $hasDepartment28: we declare a boolean, and we say by default the department with the term id 28 is not listed
  • using foreach, we loop through every entry that is listed in the taxonomy term array we named $field_affiliation and every time we assign current entry to the variable name $term
  • now, as we could see in the structure of the array, what we want to look for is the 'tid' key of every entry
  • we compare it with the ID you mentioned in the comment below, and if we have a match, we set $hasDepartment28 to TRUE
  • and via break; we stop the current foreach. since we found what we were looking for, there is no need to loop through any other entry in the array that may come after it
  • if we found no entry at all where the tid equals 28, the boolean we set earlier will still equal FALSE

let me know if it worked!

keep in mind all the variable names are arbitrary and you should use whatever describes the variable best.

one last thing I'd add is if you were delete and re-create that term entry, you'd have to update this code and check for a different Term ID. perhaps a more readable and future-proof approach would be to check inside the foreach loop for something like:

if( $thisTerm['taxonomy_term']->name == 'Apartment A' ) { ... }

but this in return only works if the [taxonomy_term] gets loaded into your array for every entry. chances are only the first entry has all the term content, but based on the array you submitted I cannot tell you if this is the case. if you wanna stick to this approach though, this is exactly where drupal's entity_load function becomes super useful!

  • Well, the custom profile field can have multiple selections (values), so I need to check if a certain term is in the custom field, and if so, disable certain fields. So, if value is in the custom field, then do something.. else.. do something else. Mar 22, 2014 at 15:47
  • @Stephen_lex ah, so it's a multi-values taxonomy term reference. do you want to check for a specific term id? if so, in PHP there is a function called in_array. so for example if the term id you are checking for is 34, you can do if (in_array(34, $myfield_terms)) { #do.. }, whereas here $myfields_terms must be an array that lists all term IDs as values (not as keys!). can you show us how your taxonomy term array is structured?
    – EchoCrow
    Mar 22, 2014 at 16:28
  • Yeah, you are right, that is what I am trying to do. The term id in my case is 28. The structure is simply a list of Departments, and users can be associated with 0 to many departments. I am a .net programmer by trade, so I am in the very early stages of learning php and drupal for a small project, so I am kind of at the beginning of the learning phase - I appreciate your patience. I was experimenting with the in_array earlier, but couldn't ever get it to find the value so I am obviously declaring something wrong. I had no problem doing the $user->role function Mar 22, 2014 at 16:42
  • in that case, welcome to drupal (: I too am currently working on my first big PHP and drupal project! – so yeah as I mentioned, in order for the in_array function to achieve the desired goal, the array that we check has to be of a certain structure. depending on how and where you grab the list of term ids, the array structure of the array can vary. I've updated my post above with some instructions on how to get an insight of the array, so we can decide what method of checking for a value is appropriate.
    – EchoCrow
    Mar 22, 2014 at 17:26
  • I am doing this inside a module to modify the edit user form that I don't think will do a print_r. It is simply altering the form. Is there a way to do this inside my custom user profile display page template? Mar 22, 2014 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.