1

I am looking for a way to hide a content field based on the nodes date field.

Example:

Event Name: Some Name Here

Event Date: 12/12/2012

Event Location: Somewhere

Buy Tickets: ticket.com

If the date has passed I would like the Buy Tickets field to not show ... any help would be appreciated.

2 Answers 2

2

The solution is very simple. In template.php file implement MY_THEME_preprocess_node(). Example:

function MY_THEME_preprocess_node(&$variables) {
  //  dpm($variables);
  if($variables['type'] == 'MY_CONTENT_TYPE') {
    $date_field_value = $variables['content']['field_DATE_FIEL_NAME'][0]['value']; // find the correct value of your field

    $converted_date = strtotime($date_field_value);

    if(time() > $converted_date) {
      hide($variables['content']['field_DATE_FIEL_NAME']);
    }
  }
}

you can also use hook_node_view:

function MY_MODULE_node_view($node, $view_mode, $langcode) {
  if($node->type == 'MY_CONTENT_TYPE') {
    $date_field_value = $node->content['field_DATE_FIEL_NAME'][0]['value'];

    $converted_date = strtotime($date_field_value);

    if(time() > $converted_date) {
      hide($node->content['field_DATE_FIEL_NAME']);
    }
  }
}
1

A good starting point is Field formatter conditions module. Easy to implement what you are looking for but will work well where the date is fixed. However since the dates for each event are bound to be different you are most likely not going to find it feasible for the task since the formatter is not implemented per node.

In that case I would go for a custom module (you can correct me on this one) that implements hook_node_view_alter()

I am answering this in a bit of a hurry. Will be back with a code sample but for now have a look at the hook on api.drupal.org and at another question answered on Stackexchange to get an idea.

Hoping I am of help.

1
  • Thanks for the recommendation Bernard. That module is awesome, but doesn't let me show / hide based on date. It will let me based on a certain string ... but not sure how to tell it Commented Dec 18, 2013 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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