0

I have a Drupal 7 site. I have this taxonomy field that I get printed out using tokens and Display Suite Module.

So, if I create a "code field" at /admin/structure/ds/fields, I can type: [node:field-suscriptor-estado1:tid] in order to get the term id of that particular node. In fact that outputs correctly the term id. It outputs a number. field-suscriptor-estado1 is the field of type term reference.

If I add var_dump($tidNum); I get int(0).

In order to show some differences between different terms, I try to compare the term ID to the one that I've got stored in database, and if they do match, it shows the result in one color. If they don't, the color is different.

As I know that the only terms that I will ever get there are 867 and 870 ("abierto" and "cerrado") I've done a quick if statement in order to achieve that:

<?php
  $tid = "[node:field-suscriptor-estado1:tid]"; //I assign the term id to a variable
  echo $tid; // I tested to see if I get it printed out using php, and it does print it: 870
  $tidNum = (int)$tid; // I transform the string variable to a number

  //now, if the number that the variable $tidNum has is 869 print the field with the class suscriptor-estado-abierto, else, print it with the class suscriptor-estado-cerrado.

  if ($tidNum == 869) {
    echo "<div class='suscriptor-estado-abierto'>[node:field_suscriptor_estado1]</div>";
    } else {
    echo "<div class='suscriptor-estado-cerrado'>[node:field_suscriptor_estado1]</div>";
    }
?>

The only differences are the css classes that I assign to the field. That way, if it is "cerrado" (closed) it's in red and if it is "abierto" (open) the color is green.

My question is, why if the number that I do get is 870, it keeps printing it out as if the did match? (with suscriptor-estado-abierto class).

I've tried different ways: ($tidNum == 869), ($tidNum === 869), and ($tidNum != 869). This last one changing the color rules, naturally.

The result is the same. I've tried also with a new if, asking for $tidNum == 870. The result is the same.

Why it isn't working?

If I try this, for the sake of testing, it outputs as it should:

  $tid = "870";
  echo $tid;
  $tidNum = (int)$tid;
  if ($tidNum == 869) {
    echo "abierto";
    } else {
    echo "cerrado"; // it prints out cerrado
}
2

This happens because you can't use tokens in the snippet itself.

When you print the token, PHP outputs the token as-is, but Drupal catches this output with Output buffering, and tokens are processed later.

In the field, see if you get the field item or taxonomy term as a PHP variable. Then, use that variable to extract the term ID. You need to refer to the information provided by your module. If you have Devel module, you can inspect those variables using dpm function.

You are probably looking for something like this: $node->field_suscriptor_estado1[LANGUAGE_NONE][0]['tid']

0
  1. try to add token_replace function:

    $tid = token_replace("[node:field-suscriptor-estado1:tid]");

or 2. another way to get field values is:

$values = field_get_items('node', $node, 'field_suscriptor_estado1');
$tid = $values[0]['tid'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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