1

I need a way to generate a simple list of all the items in a particular field. The field name is field_portfolio_description, and I'm looking to simply loop through the results and output each item into an unordered list. For example:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

Is there a way to do this programmatically without using a module like Views? I would also like to display the data in page.tpl.php not node.tpl.php. I tried checking out some Drupal functions like field_info_field but they weren't much help.

Update:

I've been working with the following function in my template.php file:

function blocks_preprocess_node(&$vars) {
  $node = $vars['node'];
  if ($node->type == 'portfolio') {
    $vars['description'] = $node->field_portfolio_description[LANGUAGE_NONE][0]['value'];
  }
}

However a quick var_dump($description) where I'm trying to access the data brings up null. Am I on the right track?

edit: fixed typo

0

2 Answers 2

1

You're pretty much on the right track. Try something like:

function blocks_preprocess_node(&$vars) {
  $node = $vars['node'];
  if ($node->type == 'portfolio') {
    $portfolios = field_get_items('node', $node, 'field_portfolio_description');
    $items = array();
    foreach ($portfolios as $folio) {
      $items['data'] = $folio['value'];
    }
    $vars['description'] = theme('item_list', array('items' => $items));
  }
}

Adding stuff to nodes like this is maybe better in hook_node_view() in a custom Module. Because you're altering the node's content before its shown (and its adding derived content to the node, not just changing some markup at the theme layer).

Code is untested and just Help. (but should mostly work)

Resources

4
  • Spooky - I just wrote an answer with exactly the same code (even down to the foreach element being called $folio) and this popped up :)
    – Clive
    Dec 5, 2012 at 23:58
  • Thank's for the input tenken (and Clive), I appreciate you both taking the time. The code looks good to be but unfortunately it didn't seem to work, var_dump($description); where I'm trying to output the data still brings up NULL. Perhaps I'm missing something obvious. I'll keep playing around with this, but if anyone else has ideas I'm all ears.
    – Brad C
    Dec 6, 2012 at 0:03
  • @BradC Are you still trying to print the description in page.tpl.php? If so you need to put the code in blocks_preprocess_page() instead, and change the first line to $node = menu_get_object(); Then clear the caches and try again
    – Clive
    Dec 6, 2012 at 0:23
  • @clive yes I'm trying to print the values in the page template. I tried your suggestions and I'm still getting NULL on $description.
    – Brad C
    Dec 6, 2012 at 0:38
0

For reference, other answers for this question were posted here.

Alternatively, instead of editing your templates or the preprocess template.php file, you can use the tiny module Text Formatter which allows you to set the display of your fields to list items.

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.