1

In Drupal you can use the following in template.php to test for a node type:

function THEMENAME_preprocess_node(&$variables) {
  if ($variables['type'] == 'INSERT TYPE HERE') {
    // include code
  }
}

How do you test for an entity that is attached to a node?

1
  • use devel module
    – Sohail
    Jun 15, 2013 at 13:07

1 Answer 1

1

The fully loaded node object should be available as a variable. You then need to know the field name that it is attached as (ie, the machine name from the manage fields tab on the content type). Once you have that, you should be able to use field_get_items() to check for actual values on that field

function THEMENAME_preprocess_node(&$variables) {
  $node = $variables['node'];

  if (field_get_items('node', $node, 'field_foo') !== FALSE) {
    // do something
  }
}

Just note that this checks for an actual value in the field, not that the field is on the particular content type. For that, you need to use field_info_instances().

function THEMENAME_preprocess_node(&$variables) {
  $node = $variables['node'];
  $fields_info = field_info_instances('node', $node->type);

  if (isset($fields_info['field_foo']) {
    // do something
  }
}

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.