Anyone knows how to put cck field(s) into Page.tpl.php? For node template it is easy - just call <?php print $node->field_mycckfield[0]['view'] ?>
But how to do it in page's template? I guess it should somehow refer to a node then pull the cck info from it.

Comments

cog.rusty’s picture

According to http://drupal.org/node/11812 the $node variable is available in page.tpl.php in Drupal 5 if you are in a full node page.

Otherwise you could probably load a specific node object using its 'nid' into a variable of yours, using http://api.drupal.org/api/function/node_load/5

nevets’s picture

page.tpl.php has $node as a variable which is set when viewing a single node. So one could check to see if $node is set, probably want to check that it is the correct type and then print the field if set. I do wonder though why you would want to print cck fields directly in page.tpl.php.

encho’s picture

Well, I would like to display cck related to a node, but not as a part of a node, but in a separate area. I have tried using a block, but it did not work for me. So anyone having an idea how to call cck field outside node, please help.

nevets’s picture

You can access the information inside page.tpl.php, the code just need to check that $node->type is of the type you want, then any fields should be available as they normally would. You can do the same thing in a block, you just need to get $node. So you would have code something like

<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
  if ( $node->type == 'your_type' ) {
    // Do something with $node
  }
}
?>
encho’s picture

Thanks very much for your effort. Will try it asap.

encho’s picture

It does not seem to work, output looks empty :-(
Any ideas?

nevets’s picture

It would help if you posted the code you have so far.

encho’s picture

Just calling cck field like I would in node template. I am beginner in this, so might need something else:

<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
  $node = node_load(arg(1));
  if ( $node->type == 'your_type' ) {
    // Do something with $node
    print $node->field_mycckfield[0]['value'];
  }
}
?>
nevets’s picture

Need to change 'your_type' to the actual type that you are interested in. For example if you had called your type 'cars', you would replace 'your_type' with 'cars'.

Something useful to do if you do not see what you expect is to add extra tempory print statements that simple print a string (something like "you are here") to make sure the code is being run.

encho’s picture

I did replace it with 'book' as it was book node type (I just added couple of fields)... Maybe should be custom node, as book would be builtin?

nevets’s picture

After

  $node = node_load(arg(1));

add

drupal_set_message("Type is $node->type");

Then visit a node you think the block should display for. You should see the type print in the message area. If the type is the one in your code add

drupal_set_message("<pre>" . print_r($node, TRUE) . '</pre>');

after the above change. This will show all the fields that are available for printing.

(If the type is not the one you have in the exisiting code, update the code with the type printed and try again)

encho’s picture

Did it. First it says:
* Type is book

Then some code:

      stdClass Object
      (
          [nid] => 99
          [vid] => 99
          [type] => book
          [status] => 1
          [created] => 1185366755
          [changed] => 1185366755
          [comment] => 2
          [promote] => 1
          [sticky] => 0
          [revision_timestamp] => 1185366755
          [title] => My Title
          [body] => Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
          [teaser] => Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.
          [log] => 
          [format] => 1
          [uid] => 1
          [name] => admin
          [picture] => 
          [data] => a:0:{}
          [path] => mypath/mytitle
          [parent] => 14
          [weight] => 0
          [last_comment_timestamp] => 1185366755
          [last_comment_name] => 
          [comment_count] => 0
          [taxonomy] => Array
              (
              )

          [field_mycckfield] => Array
              (
                  [0] => Array
                      (
                          [value] => bat
                      )

              )

      )

But when I do something like:

  if ( $node->type == 'book' ) {
    // Do something with $node
    print $node->field_mycckfield[0]['value'];
    print $title;
    print "you are here";
  }

You guess, it displays just title and "you are here" :(

cog.rusty’s picture

I happened to stumble on the following discussion when karenS (the maintainer of CCK) advices against printing fields in the page template, and suggests printing them only in the node template. But I see that this is not what you want.

http://drupal.org/node/104774

I don't really know why your field is not printed, but they were saying that "the form is wiped out".

nevets’s picture

Following the link cog.rusty provided try replacing

print $node->field_mycckfield[0]['value'];

with

print content_format(field_mycckfield', $node->field_mycckfield[0]);

If that does not work try adding

drupal_set_message("<pre>T1:" . print_r($node->field_mycckfield, TRUE) . '</pre>');
drupal_set_message("<pre>T2:" . print_r($node->field_mycckfield[0], TRUE) . '</pre>');
drupal_set_message("<pre>T3:[" . print_r($node->field_mycckfield[0]['value'], TRUE) . ']</pre>');

and see what shows.

encho’s picture

print content_format(field_mycckfield', $node->field_mycckfield[0]); worked! Already quit trying, but was back to this project and needed it. It works, thanks for all your help.

dgrebey’s picture

Would like to expand on this a bit. Instead of just printing one value in the array, I'd like to print the whole array.

print content_format('field_keyfeatures', $node->field_keyfeatures[0]) // Prints one value

i.e. Instead of just printing the first key feature (or the second, or whatever the case may be), I'd like to print all key features.

dgrebey’s picture

Heh, loop through the array with a foreach loop.

     foreach ($node->field_keyfeatures as $feature) {
          print content_format('field_keyfeatures', $feature);
     }
amariotti’s picture

Someone should create a handbook page with the results of this discussion. It solved my issue that I was having with the printer-friendly page module template. I had some CCK fields that were hidden in the node view and needed them to be displayed in the print.tpl.php file. Using the info from this page I got it working perfectly. Thanks guys!