function availability_calendar_booking_formlet_field_formatter_view in Availability Calendars 7.3
Same name and namespace in other branches
- 7.5 booking_formlet/availability_calendar_booking_formlet.module \availability_calendar_booking_formlet_field_formatter_view()
- 7.4 booking_formlet/availability_calendar_booking_formlet.module \availability_calendar_booking_formlet_field_formatter_view()
Implements hook_field_formatter_view(). @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...
File
- booking_formlet/
availability_calendar_booking_formlet.module, line 408 - Availability Calendar booking formlet module. This submdule of the Availability Calendar module defines a field that shows a small booking form that interacts with a calendar field. The form only has an arraival and departure date field and a submit…
Code
function availability_calendar_booking_formlet_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, &$items, $display) {
$settings = $display['settings'] + $instance['settings'] + $field['settings'];
// Get values for the begin and end date based on the field settings and
// the context.
$from = availability_calendar_booking_formlet_extract_date($settings, 'preset_begin_date');
$to = $from !== NULL ? availability_calendar_booking_formlet_extract_date($settings, 'preset_end_date', $from) : NULL;
// We need this function here only when from is set.
if ($from !== NULL && !function_exists('availability_calendar_is_available')) {
module_load_include('inc', 'availability_calendar', 'availability_calendar');
}
// Find all calendars in this entity that are enabled. If there is no enabled
// calendar, we do not show the booking form.
//
// We also look for the first calendar that is "available" during the from-to
// period (or just on the from date if no to date is set). This calendar will
// be used as default selection, together with the from and to date.
$cids = array();
$calendar = NULL;
$calendar_settings = NULL;
$field_instance_info = field_info_instances($entity_type, $instance['bundle']);
foreach ($field_instance_info as $field_name => $field_instance) {
$field_info = field_info_field_by_id($field_instance['field_id']);
if ($field_info['type'] === 'availability_calendar') {
$calendar_field_items = field_get_items($entity_type, $entity, $field_name, $langcode);
if ($calendar_field_items !== FALSE) {
foreach ($calendar_field_items as $delta => $calendar_field_item) {
if (isset($calendar_field_item['enabled']) && $calendar_field_item['enabled']) {
$cids[] = $calendar_field_item['cid'];
if ($from !== NULL && $calendar === NULL) {
if (availability_calendar_is_available($calendar_field_item['cid'], $from, $to !== NULL ? $to : $from, $field_info['settings']['default_state'])) {
$calendar = $calendar_field_item;
$calendar_settings = $field_info['settings'];
}
}
}
}
}
}
}
$element = array();
if (count($cids) > 0) {
// $items will be empty because this is a display only field: fake 1 item.
$delta = 0;
$items[$delta] = array();
// If we didn't find an available calendar we have to set the from and to
// dates to null.
if ($calendar === NULL) {
$from = NULL;
$to = NULL;
}
$element[$delta] = drupal_get_form('availability_calendar_booking_formlet_form', $entity_type, $entity, $settings, $cids, $calendar, $calendar_settings, $from, $to);
}
return $element;
}