By chrisfab on
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
Use $field_banner_url[0]['view']
Use $field_banner_url[0]['view'] to print the value of banner_url, instead of $node->field_banner_url[0]['value'].
doesn't work
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.
same issue: found how?
Hi,
I have the same question; did yo ufind the answer?
Thank you,
Michael
Use: <?phpecho
Use:
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
I have a similar issue. I
I have a similar issue.
I can see all the fields when I use print_r($node).
My theme function is like this :
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.
Having a similar
Having a similar problem.
Using the following code in a theme view function in template.php:
The key portion of code I added was the following:
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!
I think content_view is the answer
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
The correct way to get field
The correct way to get field values in Drupal 7 is to use the field_get_items() function, like this:
Which returns an array with all the data items in that field.
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
Did I Implement Incorrectly?
adaddinsane,
I used the following...
And it isn't spitting out anything - and I'm getting an "undefined variable" error.
Did I get something wrong?
Thanks,
Nick
$node is not defined, if
$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
There's now a module for
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
I checked out your blog and module...
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.
I think you'll find that
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