2

I am using a node-content_type.tpl file to control the content display of a certain content-type. I am rendering the fields like so:

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

I want to display some empty text if the field is empty so it doesn't show up blank. Is there a way to set empty text for all of the field of a content type so I don't have to check every field individually? Can I do this in a preprocess function in my theme's template.php file?

0

I guess that these examples could be useful:

example 1:

$exampleArray1=array("a"=>"one","b"=>"two","c"=>"");

function walkingfunction(&$value,$key)
{
    if($value == ''){
    $value = "empty text";
    }
}
array_walk($exampleArray1,"walkingfunction");

print_r($exampleArray1);

Example 2:

$exampleArray2=array("a"=>"one","b"=>"two","c"=>"");

foreach($exampleArray2 as $key => &$value) {
    if($value === '') {
        $value = "empty text";
    }
}

print_r($exampleArray2);

for the two examples, the value of c now is empty text. & is used to get element as a reference to change it.

this is an example with a CCK field:

foreach ($field_test as $key => &$value) { 
    if($value['view'] === '') {
        $value['view'] = "value is empty";
    }
}
  • that works...but I think it's picking up elements that aren't cck fields like user_picture , terms, and links because those are all null values. Is there any way to only pick up cck fields? – user2014 Sep 29 '11 at 1:28
  • hello @landing, this was tested with a cck field: <?php foreach ($field_test as $key => &$value) { if($value['view'] === '') {$value['view'] = "value is empty";}} ?> – cigotete Sep 29 '11 at 2:12
  • I guess that if your target are several CCK fields, you can asign all respective value elements that you need inside an array to change each respective empty values. – cigotete Sep 29 '11 at 2:56
  • The last snippet doesn't work for my cck fields. I'm adding them as a variable through a preprocess_node function in my template.php, so the `['view]' object isn't in the same place as your last example. Your second snippet worked well enough. – user2014 Sep 29 '11 at 17:56
0

You could use an if statement, to check for value, and if not print something else:

<?php
  if (!isset( $node->field_name[0]['value'] )) {
    print t('My empty text');
  } else {
    print $node->field_name[0]['value']; 
  }
?>

But I think that if you print the whole field you don't need to do this. Use something like:

<?php print $node->field_name_rendered; ?>
  • I want to apply the your first method to all the fields a once. I have 60 plus fields, doing this for each field isn't practical. – user2014 Sep 28 '11 at 20:38
  • You should really use $node->field_name_rendered;, if you want to still use the first method but for 60 fields use a For loop. – ipwa Sep 28 '11 at 23:09
  • @landing : add default values for all the fields that you want some text to show up for. – zarsiwala Sep 28 '11 at 23:37
  • @ipwa: Is there a reason that $node->field_name_rendered is preferred over the other method? Also, I guess my question really is how to pick out each field in the For loop...can I identify any field with a particular variable? – user2014 Sep 29 '11 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy