I'm trying to retrieve the value of a CCK node's field (type: textfield) within my template.php file.
I have created a CCK node called banner and added a field: 'banner url' which in the database looks as if it has the name 'field_banner_url'.

I have used the views theme wizard to create a function inside template.php which will call a custom 'views-my-choice-of-name.tpl.php' to display my CCK nodes.

From browsing the web and drupals forums - I wrote this piece of code as an attempt to retrieve the value of the custom field:

    foreach ($nodes as $i => $node) {
     ..
   $vars['my_variable_name'] .= $node->field_banner_url[0]['value'];
    ...
 }

..and I get nothing. What am I doing wrong?

TIA - for any help. :)

Comments

shwetanksingh’s picture

Use $field_banner_url[0]['view'] to print the value of banner_url, instead of $node->field_banner_url[0]['value'].

chrisfab’s picture

Thank you for the pointer.

Unfortunately this doesn't seem to solve the issue. I am once again left with a blank variable.
I've pasted in my complete function (created by theme wizard, slightly modified by me, and inserted into 'template.php') to shed some more light on whats going on.

Any suggestions for me?
Thanks.


function phptemplate_views_view_list_banner_forborn($view, $nodes, $type) {
  $fields = _views_get_fields();

  $taken = array();

  // Group our nodes
  $set = array();
  foreach ($nodes as $node) {
    $set[$node->node_title][] = $node;
  }

  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $field_names[$id] = $field_name;
  }

  // Set up some variables that won't change.
  $base_vars = array(
    'view' => $view,
    'view_type' => $type,
  );

  $output = '';
  foreach ($set as $label => $nodes) {
    $items = array();
    foreach ($nodes as $i => $node) {
      $vars = $base_vars;
      $vars['node'] = $node;
      $vars['count'] = $i;

// -------------------- my line of code 
     $vars['field_banner_url_value'] = $field_banner_url[0]['view']
// -----------------------------

      $vars['stripe'] = $i % 2 ? 'even' : 'odd';
      foreach ($view->field as $id => $field) {
        $name = $field_names[$id];
        $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
        if (isset($field['label'])) {
          $vars[$name . '_label'] = $field['label'];
        }
      }
      $items[] = _phptemplate_callback('views-list-banner_forborn', $vars);
    }
    if ($items) {
      $output .= theme('item_list', $items, $label);
    }
  }
  return $output;
}
mmerlange’s picture

Hi,

I have the same question; did yo ufind the answer?

Thank you,
Michael

yuriy.babenko’s picture

Use:

echo '<pre>';
print_r($node);
echo '</pre>';

to see all of the node's data.
---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

jmlavarenne’s picture

I have a similar issue.

I can see all the fields when I use print_r($node).

My theme function is like this :


function THEMENAME_views_view_teasers_VIEWNAME($view, $nodes, $type, $teasers = true, $links = true) {

  $output .= "<div>";
  foreach ($nodes as $n) {
    $node = node_load($n->nid);
    $output .= $node->title;
    
    $output .= '<div>';

    // this field  *is not* added to the output
    $output .= $node->field_a_adresse[0][view];

    // this field *is* added to the output
    $output .= $node->title;
    // taxonomy fields also get printed if added to $output in this way

    $output .= node_view($node, $teasers, false, $links);
    $output .= '</div>';
    
   // this prints the content of the field
   print_r($node->field_a_adresse[0][view]);
  }
  
  $output .= "</div>";
  return $output;
}

Interestingly, if I write

$output .= $node->field_a_adresse[0][value]

with value instead of view, the value gets added to $output.

If I remember well, 'view' is the sanitized value, so using 'value' might be better only if check_plain() is also used.

jazzdrive3’s picture

Having a similar problem.

Using the following code in a theme view function in template.php:

foreach ($nodes as $node) {
    $row = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
        if ($node->field_featured[0]['view'] == 'Yes') {
	  $cell['class'] = "featured-item view-field ". views_css_safe('view-field-'. $field['queryname']);
	} else {
          $cell['class'] = "featured-item-not view-field ". views_css_safe('view-field-'. $field['queryname']);
	}
        
        $row[] = $cell;
      }
    }
    $rows[] = $row;
  }

The key portion of code I added was the following:

        if ($node->field_featured[0]['view'] == 'Yes') {
	  $cell['class'] = "featured-item view-field ". views_css_safe('view-field-'. $field['queryname']);
	} else {
          $cell['class'] = "featured-item-not view-field ". views_css_safe('view-field-'. $field['queryname']);
	}

And the ELSE clause gets triggered everytime, even if the field_featured does equal "Yes". Doesn't make any sense. The only thing I can think of is that it's not getting accessed or passed in correctly.

I really need help with this? Anyone figure something out?

Thanks!

giovans’s picture

Just after loading the $node object, launch:

content_view($node);

in my case, this appear to populate the "view" field of the cck custom fields.

Engioy,
Giovans

adaddinsane’s picture

The correct way to get field values in Drupal 7 is to use the field_get_items() function, like this:

$items = field_get_items('node', $node, 'field_myfield');

Which returns an array with all the data items in that field.

foreach ($items as $delta => $item) {
  //... do something with the item. For example:
  $node = node_load($item['nid']);
}

What's contained in an $item depends on what the field actually contains - could be 'nid' => X if it's a node reference, or 'tid' => X if it's a taxonomy term reference, might be just 'value' => X. Or in the case of files, an array with lots of different keys.

On my D7 blog I have a function that can help extract this information auto-magically, here.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

NickWebman’s picture

adaddinsane,

I used the following...

function MYTHEME_preprocess_html(&$variables, $hook) {
	
	$output = field_get_items('node', $node, 'field_MYFIELD');
	
$variables['MYVARIABLE'] = $output;

And it isn't spitting out anything - and I'm getting an "undefined variable" error.

Did I get something wrong?

Thanks,
Nick

adaddinsane’s picture

$node is not defined, if that's all the code. (I've been on holiday hence the delay in replying.)

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

adaddinsane’s picture

There's now a module for extracting field data from entities here.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list

jfrazier’s picture

I have a problem where I need to populate a Google directions widget with a predefined destination, getting the destination from the location module's address fields, or Location CCK fields optionally. I'm using D7, and the Google directions embed code will be added to a block. I looked at your module for help but I'm not sure how to implement it. Any help appreciated.

adaddinsane’s picture

I think you'll find that locations are an entity in themselves - but I'm not sure.

Anyway they are not added to nodes (or anything else) using the Field API so my module is not the correct solution. If they are entities in themselves my module still isn't the solution because the various bits of the location are properties rather than fields.

Steve Turnbull, D8 developer

Like Science Fiction, Fantasy or Steampunk stories? Check out https://taupress.com/book-list