What is the correct way to access CCK fields within a theme file?

Joined: 01/20/2009
User offline. Last seen 1 year 33 weeks ago.

Hi,

I've created a few custom user profile fields using the Content Profile module.

In my theme file I can view the new fields using the following code:

$var = $content_profile->get_variables('user_profile');
print_r($var);

Now, I could access each field like this:

print $var['field_first_name'][0]['value'];

But is this the "correct" way of getting the field value? Is there a standard/best practice way to do this?

Thanks,

Steve

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
That's one way to do it, but

That's one way to do it, but in D6 one of the preferred ways is to use a preprocess node and define new variables.

In your template.php file,

function MYTHEME_preprocess_page(&$variables)
{
  // add the taxonomy term description to the variables
  $variables['mynewvar'] = $['node']->field_cckcontenttype_field[0]['view'];

  If (logic){
    $variables['mylogicvar'] = "do your logic in here rather than in your tpl.php file";
  }
}

Then in your tpl.php you can just print out those new variables you defined:

print $mynewvar;
print $mylogicvar;

Ideally you don't want logic in your tpl files.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
Oh, with cck fields, use

Oh, with cck fields, use "view" instead of "value". The "view" has been processed where the "value" is a raw input.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

G&G Podcast Host
Matt Farina's picture
Joined: 06/01/2006
User offline. Last seen 21 weeks 6 days ago.
$field_name_rendered

Where CCK injects fields it also injects a rendered variable into the tpl file.

For example, take a CCK field with the name 'field_test' on a node type in the node.tpl.php file there is a $field_test_rendered variable you can just print.

Matt Farina
Geeks and God Former Co-Host
www.mattfarina.com

Joined: 01/20/2009
User offline. Last seen 1 year 33 weeks ago.
Thanks Matt, I see how this

Thanks Matt,

I see how this might be useful for a node template to show the rendered field. However in my "user_profile.tpl.php" file these type of fields do not seem to be there (eg: $field_first_name_rendered)

The weird thing is that even $content doesn't even render anything in this file.

This is sure one steep learning curve!

Steve

Joined: 01/20/2009
User offline. Last seen 1 year 33 weeks ago.
Thanks, I don't see a "view"

Thanks BishopBooyah,

I don't see a "view" value, but I do see a "safe" value if I call content_view($node) first. Is this the equivalent in D6?

For a newbie this is all very confusing.

Steve

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
By the time you get to the

By the time you get to the user-profile.tpl.php everything's been rendered into html and stuffed into the "content" variable.

If you're using content profile, create a node-profile.tpl.php and you should be able to access the $field_contenttype_name variables.

In your template.php,

function mymodule_profile_preprocess_node(&$vars){
  // Create any specialized variables you might need, or do any logic
  $var['myspecialvariable'] = "booyah" . $var['node']->field_profiletype_name[0]['view'];
}

Then create a node-profile.tpl.php file,

<?php

// format your profile the way you want to here,
// but logic should go in your preprocess_node fucntion
print myspecialvariable;

// ?>
you don't need the closing php tag

Then you can do anything extra you might need inside user-profile.tpl.php

// use devel's dsm function to see what's available for you
// your node-profile.tpl.php content should be inside these variables as html content
dsm($account);
dsm($user);

So drupal will first process node-profile.tpl.php then shuffle the output of that into the user-profile.tpl.php (end of the food chain and displays output of all the previous node-*.tpl.php files).

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 01/20/2009
User offline. Last seen 1 year 33 weeks ago.
OK -- I'm starting to see how

OK -- I'm starting to see how it all fits together.

I'm still not sure though how I would create a template for a page showing just one user profile and how this would differ from a template used to show a list of user profile nodes.

Sorry if my questions seem so basic but this is very new to me.

Thanks again,

Steve

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
I don't understand your last.

I don't understand your last.

Basically, node-profile.tpl.php would be used for all content profile nodes. user-profile.tpl.php includes everything from the node-profile.tpl.php but it also includes output from other tpl.php files upstream from it. Basically every profile would flow through node-profile.tpl.php and out through user-profile.tpl.php.

If you want a page to show a list of all user profile's, then you can use views to generate something useful. You can also use views bulk operations if you want to do specialized manipulations of your profiles.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 01/20/2009
User offline. Last seen 1 year 33 weeks ago.
Hmmm... so a content-type

Hmmm... so a content-type node template file controls the look and feel of all its nodes regardless of where it's used?

I was under the impression that I could have 2 node template files for the user profile, one to use in a list of profiles, and the other to use when viewing a single user profile.

I guess I don't see why you'd need a user-profile.tpl.php in the first place then.

How did you (Geeks&God) go about doing a custom template for a user profile page?

Feeling out of my depth...

Steve

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
I guess I don't understand

I guess I don't understand your first sentence.

Basically a node-insertypehere.tpl.php file will control the look of any insertypehere nodes. In your case, node-profile.tpl.php will control the look of all content profile nodes. But if you use a blog content type or any other content type, node-profile.tpl.php would be ignored.

Drupal has several layers to the themeing process.

1. Core themes
2. Module themes
3. Theme engine stuff
4. Your Theme

So anything you do in your theme (#4) will override previous themes. But you can override just about anything.

If you're using a cck you can theme individual fields:

1. content-field-individualprofilefield.tpl.php

Then downstream from that is your content type itself:

2. node-profile.tpl.php

Then downstream from that is the user template which spits everything out on screen.

3. user-profile.tpl.php.

There are other templates you could tap into between 1 and 3. It really depends on WHAT you want to theme and why. In your case you want to theme the content profile so that would be #2. If you want to add stuff to the user profile (outside of the content profile stuff) like history, or "member since" stuff, then you can edit the user-profile.tpl.php.

Basically drupal is all about flexibility. That's why if you're borderline OC like me it can drive you nuts because it seems like there's always a better way to do something.

I themed profiles for isca-apologetics.org using node-profile.tpl.php and node-socialnetworking.tpl.php then spit everything out through user-profile.tpl.php.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 10/18/2008
User offline. Last seen 1 year 27 weeks ago.
Here's a dojo lesson

Here's a dojo lesson regarding all this stuff: http://drupaldojo.com/lesson/28

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Manish Kutaula
Manish Kutaula's picture
wow.. i some how landed here

wow.. i some how landed here ... and find what the thing which is troubling me is right here.
I was working on a custom theme donno who designed it.. and it contains lots lots of modifications in theme and i was puzled whats going on.

Can U tell me some resources from where to start.. basically on how actually dupal works at the backend.