Using Drupal 7.10 with a Zen sub-theme. I have a custom content type, say Product. The products have titles such as "Wrench", "Hammer", "TV". I want to designate certain products as being a recommended product by having a little star icon appear inline after the title.

So for example:

Hammer *
Piano *

Whether the star appears or not should be based on a boolean field I added to the product content type. I want the star to appear when viewing the node directly or when the node appears as part of a list in a view.

My thought was to put an if statement in the node template for my content type, e.g. node--product.tpl.php. However, what has me stuck is that in Zen it appears, if you are viewing a node as a page then the title is rendered in the page template, and if you're not viewing the node as a page then the title is rendered in the node template. I'm not sure how to reconcile this. Furthermore, I'm not sure if my field variable would even be available directly from the page template.


It appears I can successfully create a boolean preprocess variable in the function mytheme_preprocess_page whose value is based on field from the node:

function mytheme_preprocess_page(&$variables, $hook) {

  //$variables['sample_variable'] = t('Lorem ipsum.');

  if (isset($variables['node'])
      && $variables['node']->type == 'mycontent_type') {

       && $variables['node']->field_is_recommended[LANGUAGE_NONE][0]['value'] == 1) {
      $variables['is_recommended'] = TRUE;
    else {
      $variables['is_recommended'] = FALSE;


So now that I know this is possible, what's the best way for me to do this?

1 Answer 1


While this can be on in the page preprocess, and the node template, and probably several other places as well, I would discourage that. It is putting to much logic in the theme, that doesn't belong there. The main advantage of my solution is that the field can look right regardless of where you display it. On a page, in a block, in the header, or essentially anywhere that properly integrates with the Field API.

My solution is as follows

Add a text field, with the boolean Yes/No options

Implement two hooks, hook_field_formatter_info() and hook_field_formatter_view()

function product_field_formatter_info() {
  return array(
    'product_star' => array(
    'label' => t('Recommended product'),
    'field types' => array('text'),

function project_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if ($field['type'] == 'text' && $display['type'] == 'product_star') {
    return array(0 => array('#markup' => '<img src="star.png">'));

Clear the cache

Go to the fields display settings, and set it to "Recommended product"


  • Thanks I agree I don't like putting so much logic in the theme. This is definitely the sort of "drupal-way" type of stuff I want to learn. I'm learning drupal, and, well, frankly it's overly complex and non-intuitive. Although D7 is better than D5 when I last worked with Drupal. So then to get this to display next to the title, would I just do <?php print render($content['field_is_recommended']); ?> in the template?
    – User
    Jan 8, 2012 at 11:11
  • Also, why a text field rather than a boolean field?
    – User
    Jan 8, 2012 at 11:11
  • 1
    If you control the output in the fields display settings, there should be no need to output it in the template, although you could. Personally I exclusively control output with Panels, but that's for another question. :) As for Booleans, last time I checked, Database agnosticism meant we didn't support those :( If I'm wrong, the by all means go with a Boolean, just update the first hooks field type list. :)
    – Letharion
    Jan 8, 2012 at 11:53
  • Ah so even if I choose a "boolean" field type in the fields setup, it's still actually a string. I did notice that because you have to choose a string representation for "on" and "off" when setting up a boolean field.
    – User
    Jan 8, 2012 at 22:02
  • 1
    Technically, I believe they can go in any php-file that get's loaded by core, I would have put them in a site-specific module though. I usually have a "site-name_custom"-module.
    – Letharion
    Jan 15, 2012 at 19:11

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.