Example #1
0
/**
 * Overrides theme_date_display_combination().
 */
function favrskovtheme_date_display_combination($variables)
{
    static $repeating_ids = array();
    $entity_type = $variables['entity_type'];
    $entity = $variables['entity'];
    $field = $variables['field'];
    $instance = $variables['instance'];
    $langcode = $variables['langcode'];
    $item = $variables['item'];
    $delta = $variables['delta'];
    $display = $variables['display'];
    $field_name = $field['field_name'];
    $formatter = $display['type'];
    $options = $display['settings'];
    $dates = $variables['dates'];
    $attributes = $variables['attributes'];
    $rdf_mapping = $variables['rdf_mapping'];
    $add_rdf = $variables['add_rdf'];
    $precision = date_granularity_precision($field['settings']['granularity']);
    $output = '';
    // If date_id is set for this field and delta doesn't match, don't display it.
    if (!empty($entity->date_id)) {
        foreach ((array) $entity->date_id as $key => $id) {
            list($module, $nid, $field_name, $item_delta, $other) = explode('.', $id . '.');
            if ($field_name == $field['field_name'] && isset($delta) && $item_delta != $delta) {
                return $output;
            }
        }
    }
    // Check the formatter settings to see if the repeat rule should be displayed.
    // Show it only with the first multiple value date.
    list($id) = entity_extract_ids($entity_type, $entity);
    if (!in_array($id, $repeating_ids) && module_exists('date_repeat_field') && !empty($item['rrule']) && $options['show_repeat_rule'] == 'show') {
        $repeat_vars = array('field' => $field, 'item' => $item, 'entity_type' => $entity_type, 'entity' => $entity);
        $output .= theme('date_repeat_display', $repeat_vars);
        $repeating_ids[] = $id;
    }
    // If this is a full node or a pseudo node created by grouping multiple
    // values, see exactly which values are supposed to be visible.
    if (isset($entity->{$field_name})) {
        $entity = date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
        // Did the current value get removed by formatter settings?
        if (empty($entity->{$field_name}[$langcode][$delta])) {
            return $output;
        }
        // Adjust the $element values to match the changes.
        $element['#entity'] = $entity;
    }
    switch ($options['fromto']) {
        case 'value':
            $date1 = $dates['value']['formatted'];
            $date2 = $date1;
            break;
        case 'value2':
            $date2 = $dates['value2']['formatted'];
            $date1 = $date2;
            break;
        default:
            $date1 = $dates['value']['formatted'];
            $date2 = $dates['value2']['formatted'];
            break;
    }
    // Pull the timezone, if any, out of the formatted result and tack it back on
    // at the end, if it is in the current formatted date.
    $timezone = $dates['value']['formatted_timezone'];
    if ($timezone) {
        $timezone = ' ' . $timezone;
    }
    $date1 = str_replace($timezone, '', $date1);
    $date2 = str_replace($timezone, '', $date2);
    $time1 = preg_replace('`^([\\(\\[])`', '', $dates['value']['formatted_time']);
    $time1 = preg_replace('([\\)\\]]$)', '', $time1);
    $time2 = preg_replace('`^([\\(\\[])`', '', $dates['value2']['formatted_time']);
    $time2 = preg_replace('([\\)\\]]$)', '', $time2);
    // A date with a granularity of 'hour' has a time string that is an integer
    // value. We can't use that to replace time strings in formatted dates.
    $has_time_string = date_has_time($field['settings']['granularity']);
    if ($precision == 'hour') {
        $has_time_string = FALSE;
    }
    // No date values, display nothing.
    if (empty($date1) && empty($date2)) {
        $output .= '';
    } elseif ($date1 == $date2 || empty($date2)) {
        $output .= theme('date_display_single', array('date' => $date1, 'timezone' => $timezone, 'attributes' => $attributes, 'rdf_mapping' => $rdf_mapping, 'add_rdf' => $add_rdf, 'dates' => $dates));
    } else {
        $output .= theme('date_display_range', array('date1' => $date1, 'date2' => $date2, 'timezone' => $timezone, 'attributes' => $attributes, 'rdf_mapping' => $rdf_mapping, 'add_rdf' => $add_rdf, 'dates' => $dates));
    }
    return $output;
}
/**
 * Construct an appropriate DATETIME format string for the granularity of an item.
 */
function date_granularity_format($granularity)
{
    if (is_array($granularity)) {
        $granularity = date_granularity_precision($granularity);
    }
    $format = 'Y-m-d H:i:s';
    switch ($granularity) {
        case 'year':
            return substr($format, 0, 1);
        case 'month':
            return substr($format, 0, 3);
        case 'day':
            return substr($format, 0, 5);
        case 'hour':
            return substr($format, 0, 7);
        case 'minute':
            return substr($format, 0, 9);
        default:
            return $format;
    }
}