4

I am using Views Data Export to create a custom RSS feed that looks like My Title 2012-05-12 22:33:00

Start date is a Date field provided by the Date module.

In order to achieve the date formatting on the second field I have to use the Plain formatter from the Date module to avoid theming tags. Unfortunately plain formatting, gives the raw date as stored in the DB ignoring timezone settings.

I have created a custom field formatter module to tackle this:

/**
* Implements hook_field_formatter_info().
*/
function rss_date_formatter_field_formatter_info() {
    return array(
        'rss' => array(
          'label' => t('RSS'),
          'description' => t('Displays a date as rss.'),
          'field types' => array('date', 'datestamp', 'datetime'),
    ),  );
}


/**
 * Implements hook_field_formatter_view().
 */
function rss_date_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
    $element = array();
    $settings = $display['settings'];
    $formatter = $display['type'];
    foreach ($items as $delta => $item) {
        $element[$delta] = array('#markup' => $item['value']);
    }
    return $element;
}

Problem is that no matter what I cannot set $element to return the localtime. $item['value'] contains a string of the date representation as it is stored in the DB (thus UTC). Converting it to a date and then back seems to output nothing. Also if I hardcode a string there, it seems the string is altered but I cannot find from which function this happens (eg. entering 'test' in the #markup returns '1est', like converting the first letter to a valid month (1-12).

Any clues on this? I never thought outputting a date in localtime without theming would be so hard.

  • 1
    Does the formatter work for a node page view? – jonhattan Feb 6 '13 at 19:38
1

You should:

  1. Create a new date format: /admin/config/regional/date-time/formats
  2. Create a new date type that uses the format you just created: admin/config/regional/date-time
  3. Use that date type in view.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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