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

kccmcck’s picture

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:

  if ($node->field_a[0]['value'] != '') {
    print $node->field_a[0]['value'];
  } else {
    print $node->field_b[0]['value'];
  }

You might need to use the Devel module to accurately name your fields.

jdln’s picture

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?

kccmcck’s picture

Absolutely.

You'll need to create a custom template for the fields in the view.

  1. Under the view's basic settings, click Theme: Information. You'll want to override the default Row style output. Name your new template file the last available name in the Row style output options; it's the most specific. (Ex: views-view-fields--viewname--default.tpl.php)
  2. In your new template, you will use something like this:
      if ($fields['field_a']->content != '') {
        print $fields['field_a']->content;
      } else {
        print $fields['field_b']->content;
      }
    
  3. Make sure you rescan your template files to verify your new template is being called.
jdln’s picture

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.

kccmcck’s picture

It's definitely the row style output. See if this helps:

  1. Create a new template file inside your site's theme. Name it exactly as it appears in the row style output list (views-view-fields--viewname--default.tpl.php). (Replace 'viewname' with the name of your view.)
  2. Click on the row style output link. You'll see the default code and all the variables available to you. Copy the entire block of code, and paste it into your new template file, save it (and upload it to your server if you're not working locally).
  3. Go back to the theming information, and click the "Rescan template files" button. When you do that, you should now see your new template listed in bold in the row style output list. Click "ok" and save the view.
  4. Now, go back into your template file. Delete all the PHP below the comments. This is where you'll enter your new PHP and HTML.
  5. Add the code from my previous comment, making sure to name your fields accurately (replace 'field_a' & 'field_b' with their correct naming conventions (also found in the view's theming information).
  6. Save the template, clear the Drupal cache if necessary, and that should do it.
jdln’s picture

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.

<?php
// $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $
 /**
  * This template is used to print a single field in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $field: The field handler object that can process the input
  * - $row: The raw SQL result that can be used
  * - $output: The processed output that will normally be used.
  *
  * When fetching output from the $row, this construct should be used:
  * $data = $row->{$field->field_alias}
  *
  * The above will guarantee that you'll always get the correct data,
  * regardless of any changes in the aliasing that might happen if
  * the view is modified.
  */
?>
<?php print $output; ?>

Thanks for sticking with this.

kccmcck’s picture

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)!

<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
 * @file views-view-fields.tpl.php
 * Default simple view template to all the fields as a row.
 *
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->separator: an optional separator that may appear before a field.
 * - $row: The raw result object from the query, with all data it fetched.
 *
 * @ingroup views_templates
 */
?>
<?php // Here's the custom code - I removed the default & entered my own.
  if ($fields['field_a']->content != '') {
    print $fields['field_a']->content;
  } else {
    print $fields['field_b']->content;
  }
?>

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.

jdln’s picture

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;

<?php print print $fields['order_my']->content; ?>
<?php print print $fields['field_order_my']->content; ?>

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

jdln’s picture

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).

<h1> <?php print $node->nid ?></h1>
<h1><?php print check_plain($node->title) ?></h1>

Thanks

kccmcck’s picture

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:

  print $fields['field_image_upload_fid']->content;

Or, in this example, I could print this:

  print $fields['field_current_screenshot_fid']->content . $fields['field_current_site_title_value']->content . $fields['field_current_link_value']->content;

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?

jdln’s picture

I think I understand the concept but this doesn't print anything;

<?php
  print $fields['field_image_upload_fid']->content;
?>

If I show the variables in my view i get the following;

stdClass Object
(
    [nid] => 59
    [term_data_name] => Art
    [term_data_vid] => 1
    [term_data_tid] => 3
    [node_data_field_image_upload_field_image_upload_fid] => 64
    [node_data_field_image_upload_field_image_upload_list] => 1
    [node_data_field_image_upload_field_image_upload_data] => a:2:{s:3:"alt";s:0:"";s:5:"title";s:0:"";}
    [node_type] => image1
    [node_vid] => 59
    [node_data_field_image_upload_field_order_my_value] => 0
    [node_data_field_order_my_field_order_my_value] => 0
)

This just prints the number from node_data_field_image_upload_field_image_upload_fid;

<?php print $row->{$field->field_alias}; ?>

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

jdln’s picture

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

jdln’s picture

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;

       <!-- manual thumbnail image -->
       <?php print $node->field_image_upload_thumb[0]['view'] ?>
       
       <!-- automatic thumbnail image -->
       <?php print $node->field_image_upload[0]['view'] ?>		

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.

		<?php
 		 if ($node->field_image_upload_thumb[0]['value'] != '') {
		    print $node->field_image_upload_thumb[0]['view'];
		  } else {
  		  print $node->field_image_upload[0]['view'];
 		 }
		?>		
jdln’s picture

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.

<?php
  if ($node->field_image_upload_thumb[0]['fid'] != '') {
    print $node->field_image_upload_thumb[0]['view'];
  } else {
    print $node->field_image_upload[0]['view'];
  }
?>

Thanks a lot for all your help

kccmcck’s picture

Great! So glad you figured it out. Best of luck w/the rest of the project.

jordana’s picture

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:

stdClass::__set_state(array(
   'nid' => '35',
   'node_data_field_vid_thumb_field_vid_thumb_fid' => '44',
   'node_data_field_vid_thumb_field_vid_thumb_list' => '1',
   'node_data_field_vid_thumb_field_vid_thumb_data' => NULL,

not populated:

stdClass::__set_state(array(
   'nid' => '33',
   'node_data_field_vid_thumb_field_vid_thumb_fid' => NULL,
   'node_data_field_vid_thumb_field_vid_thumb_list' => NULL,
   'node_data_field_vid_thumb_field_vid_thumb_data' => NULL,
   'node_type' => 'media',

My if-statement:

	<?php
		if (is_null($fields['field_vid_thumb_fid_1']->content['fid']) ) {
		      print ($fields['field_video_embed']->content); 
		}
		   else {
			   print ($fields['field_vid_thumb_fid_1']->content);
		   }
	?>
(Array, 5 elements)
0 (String, 21 characters ) field_vid_thumb_fid_1
1 (String, 17 characters ) field_video_embed
2 (String, 5 characters ) title
3 (String, 7 characters ) created
4 (String, 4 characters ) name

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!

jordana’s picture

Turns out the code needs to be

<?php
		if (isset($fields['field_vid_thumb_fid_1']->raw)){
		     print ($fields['field_vid_thumb_fid_1']->content);
		}
		   else {
			   print ($fields['field_video_embed']->content); 
		   }
		?>

seeing as

 if (is_null($fields['field_vid_thumb_fid_1']->content['fid']) ) {

 and 

 if (is_null($fields['field_vid_thumb_fid_1']->content) ) {

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 =)

Taesto’s picture

Just wanted to say thanks.

The instructions are clear, and following them solved a big problem for me.

Cablestein’s picture

There's similar discussion here, with some solutions:
http://drupal.org/node/206012#comment-5832346