I need to compare two fields using template_preprocess_field().

I've got a field date and a boolean field. Field date collect start and end date but if it's one day event, I need to show "start date - one day"

If you don't collect the end date, it will be the same date of the start date. Of course I can compare start date and end date and if they are the same I will show "one day" but if the event is still running (so I won't collect the end date), I should show "on air"

So I'm trying this

function bartik_preprocess_field(&$variables) {

  //here I call the date field
  if($variables['element']['#field_name'] == 'field_duration') { 
    $starttotime = time($variables['element']['#items'][0]['value']); // convert it in time
    $starttodate = date('d-m-y', $starttotime); // formatting date

    // check if start date and end date are similar
    if ($variables['element']['#items'][0]['value'] == $variables['element']['#items'][0]['value2']) {
      if (HERE THE FIELD ONE DAY == 0) {
        $variables['items'][0]['#markup'] = $starttodate." - One day";
      } else {
        $variables['items'][0]['#markup'] = $starttodate." - On air";}
  • Hey there, I don't quite understand your question. What's this 2nd field you're trying to check (the boolean, I'm assuming)? I can see your question is somehow related to it, but am not quite getting what the problem is.... – Boriana Ditcheva Jul 30 '12 at 19:51
  • I've got one field date and one single checkbox. The single checkbox is to say "one day". The field date can collect start and end. I need to override the end date if the checkbox "one day" is checked. – Tyler Durden Jul 30 '12 at 22:59

hook_preprocess_field() is invoked while rendering a single field, but (basing on the documentation for field.tpl.php) $variables['element']['#object'] is the entity object to which the field is attached. Through that, you can get the value of the other field you are looking for.

| improve this answer | |
  • May you do an example please? – Tyler Durden Jul 30 '12 at 23:00

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.