How to return the value of a field?

mjlF95 - July 3, 2009 - 16:44

Is there a way to return the value of a cck text field via a function? I'm playing around with a module that builds a table with a listing of nodes and each value is the result of a function that returns stuff like the title, created time, or author. It seems like there are functions in Drupal API to return all these values but I don't know how to do the same thing for the value of a CCK field. Is this possible? (Working in Drupal 5 BTW) Thanks!

Well it sounds like you could

nevets - July 3, 2009 - 17:12

Well it sounds like you could use the views module and save your self some work.

If you have a node id (nid) you can load the node with something like $node = node_load($nid); at which point all the node fields are available in $node;

Here is what I tried

mjlF95 - July 3, 2009 - 17:44

Thank you! I messed around with this and I keep getting an error that says "Cannot use object of type stdClass as array in...". I'm working in the content management filter module specifically.

To build the table listing of nodes it starts like this:

while ($node = db_fetch_object($result)) {
    $nodes[$node->nid] = '';
    $form['title'][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '.
      theme('mark', node_mark($node->nid, $node->changed)));
    $form['kind'][$node->nid] = array('#value' => _cmf_get_img('node', t('node')));

I've added a conditional that will add a new value "organization" and display it only for nodes of a certain type. If I put a regular string in there it will show up ok. Here is what I tried to get it to show the value of a specific field:

    if ($node->type == 'data_entry_1') {
    $matts_node = node_load($node->nid);
    $form['organization'][$node->nid] = array('#value' => $matts_node['field_contacts_organization'][0]['value']);
    }

I tried some variations on this but I keep getting "Cannot use object of type stdClass as array in...". Any ideas?

This $matts_node['field_conta

nevets - July 3, 2009 - 18:12

This

$matts_node['field_contacts_organization'][0]['value']

should be
$matts_node->field_contacts_organization[0]['value']

That worked!

mjlF95 - July 3, 2009 - 18:51

Thank you so much! I wouldn't have come up with that syntax without your help. Do you know any place that has documentation or examples that relate to that?

EDIT: FYI, you helped me out before on this issue http://drupal.org/node/361138 and I've see your posts on the forums before. Thanks for all your work on the Drupal forms helping me and others like me.

 
 

Drupal is a registered trademark of Dries Buytaert.