Hi,

I've been playing with Drupal for a while and I'd be interested in rendering various CCK fields using their widgets in my custom blocks and ctools panes.
For instance, I'd like to render CCK FileField files ina block and I was expecting a theme function to do it for me but that didn't work.
I tried:

      $out = theme('upload_attachments',$node->field_myfiles[0]);
      $out = theme('upload_attachments',$node->field_myfiles);

eventually I did it manually:

      $out = "<div>" . theme('filefield_icon',$node->field_cv_myfiles[0]) . l($node->field_myfiles[0]['filename'],$node->field_myfiles[0]['filepath']) . "</div>";

How does the ['view'] property get rendered?
I've been looking for reference documentation but I can only find theming information.
I'd also be very interested in rendering a whole CCK group or fieldset by code instead of doing the fieldset myself and pulling all the contained fields, checking the permissions for each one.

Any direction would be appreciated.

Thanks!

Comments

WorldFallz’s picture

Maybe checkout how the cck_blocks module does it?

josepvalls’s picture

Thanks! That was a very good hint.
I had came across drupal_render() but didn't know how to send the parameters to.
I'll continue researching, but that was a good start. cck_blocks is a little module, easy to understand.
I'll keep digging as I want to render content for users that don't have access to those fields, but I think it will work out eventually.
just for the record:

$nid = 3417;
$node = node_load($nid);
$node->build_mode = 'Full node';
if (node_hook($node, 'view')) {
  $node = node_invoke($node, 'view', $teaser, $page);
}
else {
  $node = node_prepare($node, $teaser);
}
node_invoke_nodeapi($node, 'view', $teaser, $page);

Just for the record, I don't understand why they don't use instead

$nid = 3417;
$node = node_load($nid);
$node = node_build_content($node);

Anyway, then you can do:

dpm($node);
$cck_field_data = $node->content['field_myfield'];
dpm(drupal_render($cck_field_data));
$cck_field_data = $node->content['group_mygroup'];
dpm(drupal_render($cck_field_data));
infojunkie’s picture

In order to force CCK fields to be rendered, the simplified code should explicitly set build_mode to 'full node':

<?php
$nid = 3417;
$node = node_load($nid);
$node->build_mode = 'full node';
$node = node_build_content($node);
?>

To hide the label when rendering the field, append the following:

<?php
$node->content['field_myfield']['field']['#label_display'] = 'hidden';
?>
infojunkie’s picture

When you want to display CCK field values without the surrounding DIVs:

<?php
$node = node_load($nid);
$node->build_mode = 'full node';
$node = node_build_content($node);
drupal_render($node->content);
content_alter($node);
?>

You can then access your individual formatted field values as such:

<?php
echo $node->field_myfield[0]['view'];
?>
jindustry’s picture

I was a little confused at first looking for an html render of a field that allowed multiple values. While $node->field_myfield[0]['view'] allows you access to the html for one of the values in the field, you can access the group of values rendered in $node->content['field_myfield']['#children'].

arthur5005’s picture

Thanks for this post. Knowing how to render nodes manually is golden.

spike22’s picture

Thx infojunkie!!!
U saved my day by sharing this post.

lsiden’s picture

I tried doing exactly that in my preprocess_node() in template.php but get an empty string:

$field_bus_category = $node->content['field_business_category'];
$vars['business_category'] = drupal_render($field_bus_category);

If I do the same in a hook_preprocess, I wind up with infinite recursion.

christopherhanson’s picture

thanks been looking for this! using panels and page manager from chaos tools and it stops me from using rendered children of cck fields, this made it work again! thanks!