If you’re looking to load up a list of term references that are attached to a node in your Drupal setup, here is some useful PHP code that will hopefully help you on your journey to getting what you’re looking for. The code highlighted here was actually written during my day job for a slightly more expanded case than this, but I was able to boil it down to its essence to share here with those who may be seeking to do a similar task.

For a bit of background on the original use case, I unfortunately needed to add this to a View via a Global: PHP field in order to swap out the actual name of a term in the system with a secondary name field that is assigned to each term. While not an ideal scenario, this is what I was handed and deal with until I’m able to assign the proper name as the term title itself…

Getting Started

For ease of both understanding and copy pasta, I’ll jump through this step-by-step as noted above, then provide the full code in summary in a single block for easy pickings. Things highlighted in red in the code blocks below are what you should replace with your own field machine names as set up in your node content types or taxonomies.

This can be broken down into a few steps:

  1. Load up the node using NID (to get access to its fields)
  2. Count the number of items assigned to a ‘Term Reference’ node field
  3. Load every the name of every term from the ‘Term Reference’ field into an array
  4. Sort the array alphabetically and output the contents with a simple separator (in this case: ” | “)

Load the node using NID:

Set variable $nid and then use it in Drupal’s node_load function to get access to the node’s fields.
(As noted above, in this particular case I was adding this as logic to a View PHP field. I was able to load the NID from $data->nid by adding the Content: NID field and checking the box to exclude it from view.)

$nid = $data->nid;
$nodeload = node_load($nid); 

Prepare array & count the number of items in Term Reference field:

You’ll want to prep an array for use in collecting the term names later. Counting the number of items in the field allows us to loop through using range within the foreach function in the next step.
(Please note: the -1 here is simply because, for reasons unknown, Drupal outputs an extra blank value while doing the count.)

  
$termlist = array();
$dcount = count($nodeload->field_taxonomy[LANGUAGE_NONE]) -1;

Load each the term name into the array:

  1. We start with a foreach running through using the range function and the variable we set for a term count above — in this case $dcount — and assign $dnum for each individual item. Everything starts at 0 instead of 1, so we loop through starting from 0 through $dcount.
  2. Next, we set variable $dtid to the ‘tid’ (term ID) for each term by utilizing the $dnum that we’re doing business as.
  3. By using the $dtid we just grabbed, we can now load our term so we can access its fields. This is done by setting the $dtermload variable and using Drupal’s taxonomy_term_load function.
  4. Lastly is the if else for deciding whether the term name should be listed or not. In my actual code, I did a bit more specific checks and logic here, but I thought it might be helpful to show a basic isset case to check first that the term name is set. If the term name is set, output the name to the $dname variable, which is then added to our $termlist array we set in the previous step; if not, it’s set to NULL. For each term, it will load the term name into the array by utlizing $termlist[].
    
foreach(range('0', $dcount) as $dnum) {
    $dtid = $nodeload->field_taxonomy[LANGUAGE_NONE][$dnum]['tid'];
    $dtermload = taxonomy_term_load($dtid);

    if(isset($dtermload->field_name[LANGUAGE_NONE][0]['value'])) {
        $dname = $dtermload->field_name[LANGUAGE_NONE][0]['value'];
        $termlist[] = $dname;
    } else {
        $dname = NULL;
    }
}

Sort the array alphabetically/ascending; output with separators

To finish, we just do a bit of clean up work. First we sort the array alphabetically for easy parsing on the front-end, and secondly add a separator — a pipe — to differentiate between terms, though you could also use a comma or anything else you wish here. (I used a pipe simply because some of my term names had commas in them already.) I also wrapped the separator in a span for ease of styling with CSS to differentiate them from the term names.

 
asort($termlist);
echo implode(' | ', $termlist);

Our Final Code

$nid = $data->nid;
$nodeload = node_load($nid); 

$termlist = array();
$dcount = count($nodeload->field_taxonomy[LANGUAGE_NONE]) -1;

foreach(range('0', $dcount) as $dnum) {
    $dtid = $nodeload->field_taxonomy[LANGUAGE_NONE][$dnum]['tid'];
    $dtermload = taxonomy_term_load($dtid);

    if(isset($dtermload->field_name[LANGUAGE_NONE][0]['value'])) {
        $dname = $dtermload->field_name[LANGUAGE_NONE][0]['value'];
        $termlist[] = $dname;
    } else {
        $dname = NULL;
    }
}

asort($termlist);
echo implode(' | ', $termlist);

Leave a Reply

Your email address will not be published. Required fields are marked *

Continue Reading