By jdln on
My content type has the following;
Node1
fieldA - text for field A in Node 1
fieldB - text for field B in Node 1
Node2
fieldA - (empty)
fieldB - text for field B in Node 2
I want to show fieldA, unless it's empty in which case i wanted to show fieldB. So I would have the following;
Node1
'text for field A in Node 1'
Node2
'text for field B in Node 2'
Is there some php code that would do this?
Thanks
Comments
Hey Jdin, you'll need to
Hey Jdin, you'll need to create a custom node template for the specific content type (node-type.tpl.php) first.
Then try something like this:
You might need to use the Devel module to accurately name your fields.
Thanks, ill try that out.
Thanks, ill try that out.
What would be even more convenient would be a views solution. Could I put the same code into views by rewriting the field?
Absolutely. You'll need to
Absolutely.
You'll need to create a custom template for the fields in the view.
Is it defiantly the row style
Is it defiantly the row style output I need to override?
If I add html to the row override tpl file I cant see it on the page (including searching the page source). I can see html in the right place if I override the views-view-field.tpl.php, but then code doesnt seem to have any effect
<?php print $fields['field_a']->content; ?>.Ive also tried printing this, along with other code I got from the Content Template module, nothing seems to work.
<?php print $fields['title']->content; ?>Ive searched the forums loads about this and all posts seem to suggest the same code, which doesn't work for me.
It's definitely the row style
It's definitely the row style output. See if this helps:
Im using a row style
Im using a row style override, I can see from rescanning the template files my override file is being used. However if i add html or even delete the entire contence of the file, it has no effect on my page.
If i override the field template (below), if I add html I can see it being applied in the right place. However as I outlined in my previous post, using php to print a field doesnt seem to work.
Thanks for sticking with this.
Looks like you are overriding
Looks like you are overriding a theme template for a single field in your view where you should be overriding for every field in your row (see line 4 in your code above - "This template is used to print a single field in a view"). The $Id of this template is for views-view-field.tpl.php. You want to grab the template for views-view-fields.tpl.php (plural)!
The more specific you name it, the better. (Example screenshot) The template I highlighted in red is the name of the custom template in my theme. This is where the code should go.
Ive found the right template.
Ive found the right template. I can add html text to it and I can see it showing up in the right place.
However I cant print fields. I have a filed called order_my but the neither of these codes work;
Ive also tried to print the node's name using code I got from the Content Template module. Pretty much whatever I try I just get a '1' being outputted;
<?php print $node->nid ?>Using the code from the Content Template has worked for me before when I was pasting it into a content type tpl file. The page that the view is on is a taxonomy term, and the nodes are tagged with that term. Is there a different way of calling fields when in this situation?
In case it matters the views template file is under the following (my filed is called image_upload).
Field Content: image_upload (field_image_upload) (ID: field_image_upload_fid)
Thanks
Ive double checked. The
Ive double checked. The following code works when added to page.tpl.php (so when on the nodes page). But it doenst work when being used in an override of views-view-field.tpl.php (when on a views override of a taxonomy page).
Thanks
Your ID should be
Your ID should be field_image_upload_fid.
If you're trying to output the images in your view you would print the following in your template:
Or, in this example, I could print this:
When templating views, always use the ID shown in the view's theming information, not the original ID available to the node. Does this help?
I think I understand the
I think I understand the concept but this doesn't print anything;
If I show the variables in my view i get the following;
This just prints the number from node_data_field_image_upload_field_image_upload_fid;
I think it just prints the number as this template is for a single field. Presumably there is some way I could
modify the code that would use the number to find and print the image it relates to.
Thanks
OK, Ive made some progress.
OK, Ive made some progress.
I should have mentioned im using a Views Slideshow. Views Slideshow works fine with field templates, but ive realized it ignores row templates.
Could I just use fieldA as the only field (in the Views UI), then in fieldA’s template print fieldB if fieldA is empty? Is it easy enough to print the CCK field fieldB in this way or does the data have to be loaded into the View in some special way?
Or, do I have to include both fields in my View, and then in the row template dont show one of them? Im assuming the row template should contain both fields so they could both be pulled from the array.
The 2nd method would require getting Views Slideshow to use the row templates, so the 1st method would probably be easier if its possible.
Thanks again
Ive decided on a different
Ive decided on a different approach. Instead of fields im using nodes in the view. This has allowed me to print both my fields with this;
The following code however does not show the field_image_upload_thumb, even when using the code above I can see that the field should show the image.
I think ive got it working.
I think ive got it working. The fact that the fields I was using were image cache caused problems. When I used a different filed it seems to work.
Thanks a lot for all your help
Great! So glad you figured it
Great! So glad you figured it out. Best of luck w/the rest of the project.
likewise problem, but my if statement isn't working
I want the field "field_vid_thumb_fid_1" to print if it is populated otherwise print 'field_video_embed'
The output of the array for array where it is populated is:
not populated:
My if-statement:
But the if-statement doesn't work - it only outputs 'field_vid_thumb_fid_1' and doesn't output the 'field_video_embed' at all.
I think there might be something I'm missing, which is why I added ['fid'] to the first line, but it doesn't help.
I hope you guys can help me.
Thank you so much!
Ok so figured it out with the help of the wonderful people on ir
Turns out the code needs to be
seeing as
either always or never output to true.
So thanks to bkat for figuring out the if statement should check the raw value of the cck field.
They were imagecache thumbnails so I guess they weren't arrays?
Again thanks for all the help in irc: micrypt and George_OConnor and of course to b.kat!
The Drupal community is awesome =)
Thanks
Just wanted to say thanks.
The instructions are clear, and following them solved a big problem for me.
There's similar discussion
There's similar discussion here, with some solutions:
http://drupal.org/node/206012#comment-5832346