I'm creating a calendar module in Drupal. Due to the clients' needs, I need to be able to hide the end date using a boolean variable on the node, saved in a CCK field.
My problem is that I am able to hide it, by hooking into the theme_date_display_range() theming function, but not from within my module. As far as I can see, this is only possible from within the theme. This will mean, that I won't be able to hide the end date without using a certain theme enabling this.
If I then say, I'll use a certain theme and live with that, I'm still not able to see the context in which the mytheme_date_display_range() is called, and I thereby have no way of knowing if the current node wants to show or hide the end date. I could pass it along as a variable, but would there be a better way to do this?
Could I maybe overwrite the date's theming function to use my module instead, and how would I do this, if that was the best/correct way?
Okay, so I think I've found a good solution without using the theming functions at all. I took a closer look at theme_date_display_combination(), which themes the dates. If the end date is not set, it will only display the start date. I hook into hook_entity_prepare_view()
and check if both the date and hide endtime fields are present. If so I unset the endtime based on the boolean value.
/**
* Implements hook_entity_prepare_view().
*/
function kw_calendar_full_entity_prepare_view($entities, $type, $langcode) {
foreach ($entities as &$entity) {
if (isset($entity->field_event_date) && isset($entity->field_hide_endtime) && $entity->field_hide_endtime[LANGUAGE_NONE][0]['value'] == 0) {
unset($entity->field_event_date[LANGUAGE_NONE][0]['value2']);
}
}
}
Hope this will help somebody one day...