0

For testing purposes I've installed CCK module on vanila drupal, created new node type and attached CCK nodereference to it.

After creating new node, I've notice in node.tpl.php the following structure:

    [field_my_node_reference] => Array
    (
        [0] => Array
            (
                [nid] => 
                [safe] => Array
                    (
                    )

                [view] => 
            )

    )

As you can probably notice, there is a key $node['field_my_node_reference][0]['nid'] that is null.

In order for me to fully understand CCK - why CCK gives empty keys when nothing is selected?

Any answer is appreciated !

2
  • You found this in node.tpl.php? What theme are you using? Adding a cck field does not modify node.tpl.php. Is it possible that you are using the devel module to view the node object? – Craig Bertrand Apr 4 '12 at 14:04
  • I've print_r($node) in node.tpl.php – confiq Apr 4 '12 at 20:13
2

You are looking at a node in CCK's post build_content() phase. When you add a node reference field to a node, CCK adds the 'nid' portion to the database, and when you build or display the content, it creates the 'safe' and 'view' portions. If you were doing this manually, you could do something like:

$foo=node_load(27);

and if $foo had a nodereference, $foo would contain

[field_my_node_reference] => Array
(
    [0] => Array
        (
           [nid] =>
        )
)

eg, you could print 'my nid is ' . $foo->field_my_node_reference[0]['nid'];'

and it would print out my nid is [blank] if unset, or my nid is 593 if set. Now, when CCK comes in and gets all fancy, it creates the safe and view pieces, and these are set up by those widgets you select in the display fields portion of CCK before getting to node.tpl. (You can do this yourself by calling $foo=build_content($foo); in a module and looking at $foo again.) Anyways, CCK works all of that up into the $content variable it aggregates things together into for your node.tpl as well as in the $node variable it has in there if you yourself want to get fancier, eg, print 'this is a link to my node: ' . $node->field_my_node_reference[0]['view'] would normally print out 'this is a link to my node:' followed by the title of the node linked to it. All changeable via those display options/widgets.

But as to why it has empty keys, they are just part of the data structure created to ensure everything up and down the workflow works together as well as it does, eg, the safe and view portions are blanks where a link would probably go if the field was set, so that makes sense. And a NULL is in nid since it hasn't been set.

But...just plop in some content and you will see everything fill up all nice :)

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.