1

If I have a node object and would like to render just a given CCK field, how would I do so? I don't mean on a node template page or in a nodeapi hook, just in general.

My custom CCK field's theme_mymodule_formatter_default function takes an $elements array - when this function is called on the node page, $elements is an array with keys including the following:

  • #formatter
  • #node
  • #field_name
  • #theme
  • 0
  • 1

...where 0 and 1 point to arrays of values in the multi-valued CCK field.

Now if I want to render this field, and all I have is my formatter function and a node object, how do I format it? If I call theme('mymodule_formatter', $node->field_my_cck_field[0]), what gets passed to the $elements array of my formatter function is an array with only the following keys

  • 0
  • 1

But rather than even having to call the theme() method, I'd like to call something like drupal_render() that will pick the default theme formatter function. I imagine there's a way to do something like this, I just don't know how.

4 Answers 4

2

In D6, you can render CCK fields with:

content_format

e.g:

print content_format('field_my_cck_field', $node->field_my_cck_field[0], 'default', $node);

See also content_view_field for an alternative option.

If you're in a node template and the field is set to display, you can also just go:

print $FIELD_NAME_rendered // where $FIELD_NAME is the field name
2
$field = content_fields('field_my_cck_field', $node->type);
$rendered_field = content_view_field($field, $node, FALSE, TRUE);
2
  • Hello. It's usually a good idea to post a bit more than a code in your answer. A bit of explanation, links to api docs and so on.
    – Mołot
    Commented Sep 10, 2013 at 12:02
  • This is what i was looking for. Rendering a cck field with the div wrappers that they normally come with.
    – Bibokid
    Commented Jul 30, 2014 at 8:28
1

Rendering CCK Field values in Drupal 6.x

If you have a copy of the node and you need to output the rendered value of a field or fields, then you need to

  1. Set the view mode to either 'full' or 'teaser'
  2. Push the node through node_build_content
  3. Output the 'view' key of the fields you want to render

Here's an example:

<?php
$nid = 12345;
$node = node_load($nid);

if ($teaser_view_is_what_you_want) {
    $build_mode = NODE_BUILD_PREVIEW;
    $teaser = TRUE;
    $page = FALSE;
else {
    $build_mode = NODE_BUILD_NORMAL;
    $teaser = FALSE;
    $page = TRUE;
}

$node->build_mode = $build_mode;
$node = node_build_content($node, $teaser, $page);

content_alter($node);

echo $node->field_myfield[0]['view'];
?>
0

The funcitons field_view_value() and field_view_field() may help you in this case?

Have a link on this article about these functions: http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

4
  • I should have mentioned it's Drupal 6 I'm working with. I have updated the thread title to reflect this.
    – user779159
    Commented Nov 11, 2011 at 14:16
  • The question says D6.
    – doublejosh
    Commented Mar 6, 2012 at 7:19
  • And he says that he changed the title to d6 after i posted the answer.
    – Lance
    Commented Mar 6, 2012 at 7:50
  • whopsie daisy :)
    – doublejosh
    Commented Apr 28, 2013 at 23:54

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.