2

In some of the content type I have a field date with collects start date and end date. The end date is not required and is empty as default value.

When I show date in teaser or node or view it will show start and end date but I want a condition

if (empty($end_date)) {
 print "current";
} else {
print $end_date;
}

of course I can set it in the node.tpl.php, for the teaser and the node, and with alternative text if empty in the views with field setting.

Now would like to know if there is any way to do it with the API. Something I can do in the template.php and not in every single node--CONTENT-TYPE.tpl.php and in every single view.

I'm looking for something like

function my_module_date_api ($something) {
if (empty($end_date)) {
     print "current";
    } else {
    print $end_date;
    }
}

Thanks in advance

2

Have a look at Custom Formatters module.

"The Custom Formatters module allows users to easily create custom CCK/Field Formatters without the need to write a custom module."

6
  • Westie, I've tested it but I would prefer to use the old way through the tpl.php file. Do you know which API should I call? – Tyler Durden Jul 13 '12 at 14:13
  • 1
    Well you can do this is two ways, either a preprocessor hook (like template_preprocess_field) in your template.php before the data comes into the tpl.php or in the tpl.php itself. – WestieUK Jul 13 '12 at 14:25
  • sorry, that was half comment. I will try this function. Thanks – Tyler Durden Jul 13 '12 at 15:31
  • Hi Westie, I've put this piece of code function bartik_preprocess_field(&$variables) { if($variables['element']['#field_name'] == field_date) { } } now this error comes up Use of undefined constant field_date - assumed 'field_date' in bartik_preprocess_field() – Tyler Durden Jul 13 '12 at 16:27
  • 1
    try 'field_date' otherwise php is looking for a constant rather than a value – WestieUK Jul 15 '12 at 16:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.