Request for better time formats

Created on 8 October 2015, about 9 years ago
Updated 12 May 2023, over 1 year ago

I found the time format that comes with this just wasn't cutting it for me, so implemented a new field format, which allows valid text entry for say a DESCRIPTION: field with no HTML tags and \n for line breaks, as well as two date/time formats, which check for all day events and adjust the format accordingly.

This can just drop into views_ical.module or into a separate module by replacing the views_ical_ hook:

/**
 * Implements hook_field_formatter_info().
 */
function views_ical_field_formatter_info() {
  return array(
    'ics_plain_text' => array(
      'label' => t('ICS-ready text'),
      'field types' => array('text', 'text_long', 'text_with_summary'),
    ),
    'ics_image_src' => array(
      'label' => t('ICS image path'),
      'field types' => array('image'),
    ),
    'ics_date_local' => array(
      'label' => t('ICS-ready local date'),
      'field types' => array('date', 'datestamp', 'datetime'),
      'settings' => array(
        'fromto' => 'value',
      ),
    ),
    'ics_date_utc' => array(
      'label' => t('ICS-ready UTC date'),
      'field types' => array('date', 'datestamp', 'datetime'),
      'settings' => array(
        'fromto' => 'value',
      ),
    )
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 * Borrows the date.api version
 */
function views_ical_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  if(isset($form['setings']['fromto'])){
    $date_form = date_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
    $form = array('fromto'=>$date_form['fromto']);
    $form['info']['#markup']=print_r($field,true);
    unset($form['fromto']['#options']['both']);
    return $form;
  }
}

/**
 * Implements hook_field_formatter_settings_summary().
 * Borrows the date.api version
 */
function views_ical_field_formatter_settings_summary($field, $instance, $view_mode) {
  if(isset($form['setings']['fromto'])){
    return date_field_formatter_settings_summary($field, $instance, $view_mode);
  }
}

/**
 * Implements hook_field_formatter_view().
 */
function views_ical_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  switch ($display['type']) {
    case 'ics_plain_text':
      foreach ($items as $delta => $item) {
        $clean = str_replace(array('</p>','<br>','<br/>'),'\n',$item['value']);
        $clean = str_replace(array("\r","\n"),'',$clean);
        $clean = strip_tags($clean);
        if(substr($clean,-2)=='\n')$clean=substr($clean,0,strlen($clean)-2);
        $element[$delta] = array('#markup' => $clean);
      }
      break;

    case 'ics_image_src':
      foreach ($items as $delta => $item) {
        $output = file_create_url($item['uri']);
        $element[$delta] = array('#markup' => $output);
      }
      break;

    case 'ics_date_local':
      foreach ($items as $delta => $item) {
        $time = $item[$settings['fromto']];
        $stamp = strtotime($time);
        $stamp = strtotime($time) + date('Z',$stamp);
        $date = date('Ymd',$stamp);
        $hours = date('His',$stamp);
        if($hours=='000000'){
          $output = $date;
        } else {
          $output = $date.'T'.$hours;
        }
        $element[$delta] = array('#markup' => $output);
      }
      break;

    case 'ics_date_utc':
      foreach ($items as $delta => $item) {
        $time = $item[$settings['fromto']];
        $stamp = strtotime($time);
        $date = date('Ymd',$stamp);
        $hours = date('His',$stamp);
        $utc_hours = date('His',$stamp + date('Z',$stamp));
        if($utc_hours=='000000'){
          $output = date('Ymd',$stamp + date('Z',$stamp));
        } else {
          $output = $date.'T'.$hours.'Z';
        }
        $element[$delta] = array('#markup' => $output);
      }
      break;

  }
  return $element;
}
✨ Feature request
Status

Closed: outdated

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom alpirrie

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.

    The updated Wizard plugin in the D8+ version uses the eluceo/ical library to render the calendar. I would encourage everyone to use that.

    The D7 version is no longer supported.

Production build 0.71.5 2024