By tflmike on
I'm working on theming a specific content type (using node-content_type.tpl.php) and I noticed that nodes seem to take all the entered fields and dumps them all into a big string called $content. That's good to know but... I want to put some of my CCK fields into a separate DIV for theming.
Does anyone know how I can:
1. take the CCK field variables and wrap a div around it and
2. how I can unset the CCK field vars from the $content array so they don't show up twice?
Thanks!
Comments
Contemplate module
One good tool for this is to get the Contemplate module, or if you prefer, the Devel module ought to be able to get you what you want out of the $content variable.
I don't suggest using the Contemplate module to override or change the output for your fields "within" the Contemplate module... however, you can use the module to easily find out what PHP variables you can use and surrounding code (and paste into your tpl.php files) to print out the individual fields.
Just go to admin/content/templates, click on "create template" next to the name of the Content Type you want to acquire fields from, click the Body link to expand it, check the "Affect body output" checkbox. If you see the field you need now, go ahead and copy/paste the code into your tpl.php file. If not, expand the Body variables link, and click inside to add additional code to the field above. When you've got the code you need, Exit from the Contemplate screen without saving any changes. It is always better to save your code, whenever possible, in actual files rather than the database.
(Note I noticed that on one of my local Drupal test sites I had FCKeditor installed, and by default it caused Contemplate's textareas to become wysiwyg editor areas. If you have a wysiwyg editor, ensure it isn't affecting Contemplate's forms).
Once you get used to the code that formats these fields you may even be able to just skip Contemplate completely and just add the simple PHP and your field names as needed.
When I do this myself, I usually drop the $content variable altogether from the node-content_type.tpl.php file, and reconstruct just the fields I want using what I extracted from Contemplate module. If you want to also include the Body field's content itself, this is the code to do it:
<?php print $node->pages[0] ?>(whereas<?php print $node->body ?>would re-print all the data from the $content variable which isn't what you want).Hope this helps.
-- David
absolutecross.com
Interesting results...
David-
Thanks for your reply! I was checking out the Contemplate screen and I noticed something odd... In the 'Body' field, it only shows the CCK fields! It doesn't show me the div's and vars for the rest of the body! Hmmmm does this mean I have the Contemplate module set up wrong?
I also tried the
<?php print $node->pages[0] ?>and although it works fine, I noticed that it's outputs simple text... or in other words, I lose any formatting I set up with my FCKeditor. I was also wondering how that code would work if I set up a pager. Would<?php print $node->pages[0] ?>only show the first page?I was using something similar
<?php print $node->content['body']['#value'] ?>which keeps the FCKeditor formatting, but it breaks my inline image token (from Inline module). Sheesh... one thing after another! ;-)I guess that's why I was thinking about trying to use
<?php unset $content->field_int_header['0']['view']; ?>to remove the fields from the $content variable so when I use<?php print $content; ?>it would show me the body copy with my inline images intact... but it doesn't work.Any additional thoughts? Thanks very much!
Yes, it only displays the
Yes, it only displays the CCK fields by default... though other things are within the "Body Variables" area.
I just tested to be sure, and using
<?php print $node->pages[0] ?>outputs the full HTML of Drupal's Body field, limited only by the settings of my Filtered HTML Input Format (e.g. I added the<b>and<i>tags to Filtered HTML, since Drupal doesn't specify these in Filtered HTML by default, but they are what FCKEditor appears to use). Not sure why yours isn't displaying with HTML.I tried
<?php print $node->content['body']['#value'] ?>as well, and tested it with Paging... you're right, paging works for this but not the other, so I'd go with this one. I don't use Inline module, so I'm not sure what's going on with that. I tested and reproduced what you said: inline image tags are not rendered in this case. However I tried with Image Assist, which uses similar tags, and it "did" work. Both Inline and Image Assist have the same module maintainer by the way. So my suggestion would be to check the Inline issue queue for other people with this same problem, or post a new issue if it hasn't come up yet.-- David
absolutecross.com
Cool... Thanks for checking
Cool... Thanks for checking it out. I'll give the issue queue a shot.
Thanks David!
You can simply add this at
You can simply add this at the end of your "ode-content_type.tpl.php" file temporarily to see the available fields.
No delete & cancel button when trying to delete a page
Hi guys,
So, I'm using
print $node->content['body']['#value']and it works great for my purposes.However, when I click on the edit tab and then scroll to the bottom and click on the Delete button to delete a page, I get the confirmation message "Are you sure you want to delete _page_title_". But, there is no Delete and Cancel button to confirm the delete.
It shows up when I use
print $content.Any help is appreciated! Thanks!
-Tim
Potential Solution
I know this thread is bit old. But I encountered the same problem when developing a new theme. So I thought it will be useful for someone out there looking for the same
Requirement
- I have a new CCK field in "Page" content type called "field_page_footer"
- During the theming, I need to extract this field separately and display in a separate DIV tag
- When I print the $content, this CCK field should not display
Solution
- Once the CCK filed is added to the content type (in this case "Page"), goto the "Display Fields" in the edit menu of the content type.
- In that select from the drop down for "Full node" column (or even teaser) for the "field_page_footer" column and save it.
- In the template print the $content variable (
print $content) and you can see it will exclude the "field_page_footer"- Now separately access the filed using
print $node->field_page_footer[0]['value']All the other contents remain intact
Hope this helps !!!
not to mention not one thread
not to mention not one thread ive found about this mentioned check_plain() - PRINTING THE VALUE OUT RIGHT OPENS UP A SECURITY WHOLE
you should also look into dpm($node) (dpm - from devel module), and check out $node->content['field/group']['#children'] which has html code to displat that field or group directly.. and its escaped
Hi there So I'm
Hi there
So I'm confused....my $content variable is quite big and complicated. when I print_r($node) its kind of a big output of data.
Not to say searching through it for specific array variables is a problem. But:
for node-mycontent.tpl.php
Let's say I just want to flip the order of two print variables in $content
does that mean i have to now, disable output of $content and manually specify every single print command for every variable on the page.
That seams like a lot of work for a simple change: anyone see what i'm missing?
A couple of choices. If it's
A couple of choices.
If it's ok to flip them on input, you can just re-arrange the fields under "Manage Fields" for the content type.
If the order needs to be different for input and output you might check out the Display Suite, the Node Display part will give you a drag and drop interface to change the order of the fields on output separate from input.
thanks very much for the
thanks very much for the pointer to that module. And for changing the weight/order of fields: great! But what about, say inserting a few lines of code?
For example, I have some CCK fieldgroups assigned to tabs. I spent some time trying to get the node fields (body, title etc) assigned to fieldgroups - but essentially gave up.
All I wanted to do was add a few lines of PHP code to a given a tab (i.e.
<?php print $title; ?>)But reading through the tutorials, comments etc it seems the only way to do this is to hand code the tabs html and manually print each variable.
What happens if I add a field later....do I need to edit every .tpl file which displays that variable?
Like this:
Of course this is an example. What I'm trying to ask is, how do I get to into the meat and potatoes of a given node $content variable while still staying a flexible?
You probably would find the
You probably would find the display suite useful. It allows you to order the standard fields (body, title) also. You can also make field groups for display independent of those use on the input form. And you can set the field groups to display as tabs.