0

I found the code below to print a watcher link in a block, but it is for Drupal 6.

<?php
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    $node = node_build_content($node, false, true);
    print $node->content['watcher']['#value'];
  }
?>

Does anyone know how to get the same thing done for Drupal 7?

0

This depends on if you are trying to get the value of the field or the rendered output of the field.

If you just want the value of the field you can do this in the same way in Drupal 7. Instead of

 $node->content['field_name']['#value'];

you need to specify a language and the a number for the field value you want (even if the field doesn't accept multiple values).

 $node->field_name[$node->language][0]['value'];

If you have the entity api module installed you can use the entity_metadata_wrapper function which is a little cleaner: http://drupal.org/node/1021556

If you want the rendered output, use node_view:

 if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_view(node_load(arg(1));
    print drupal_render($node['field_name']);
  }

The rendered output will give you the properly themed output, something closer to:

"<div class="field field-name-field-field-name field-type-list-text field-label-hidden"><div class="field-items"><div class="field-item even">field value</div></div></div>"
  • okay :) .. bear in mind that i have very little experience in programming. I can understand a bit how things work. So I have my D6 code $node->content['watcher']etc. how do i combine your code with the D6 code? :) – blogook Feb 17 '12 at 21:16
  • switch that with '$node->watcher[$node->language][0]['value'];' if 'watcher' is your field name. – acouch Feb 17 '12 at 21:36
  • I really do appreciate your elaborate reply, many thanks for this. I tried to switch to print $node->watcher[$node->language][0]['value']; but it is not working. I am guessing the fieldname is different in watcher for D7. I tried checking with devel hoping to find the fieldname of watcher for D7 but it is giving me so much information :( ... – blogook Feb 17 '12 at 22:36
  • Install the devel module: dgo.to/devel and write "dpm($node)" and you will get a nice clickable list of the node object. Then you can see what the field name is. – acouch Feb 17 '12 at 22:49
  • I tried using devel but i cannot find the node field. When i use 'watcher' as field name i get a message in my dblog "Notice: Undefined property: stdClass::$watcher" .. thanks so far, i will get back to this at a later time – blogook Feb 18 '12 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.