I am using csv_serialization contrib module to add csv encoding to the core Serialization output formats.

I have a view with a REST export display to allow the download of contact form 'message' entities, and as each contact form might have different fields I must return a (serialized) Entity rather than Fields (with the option to configure each field's display).

The resulting csv download lists created date as a unix timestamp and I need to format it as a human readable time and date, but as far as I can see the 'REST export' does not go through rendering so I cannot preprocess it in the same way I might with a different type of Views display.


Even with the rows being displayed as entities, you can have a hook_views_pre_render(&$views) invocation alter the entity field properties before they are used they are rendered out.

function MYMODULE_views_pre_render($view) {

  // Only alter my_view_machine_id view, rest_export display
  if ($view->id() == 'my_view_machine_id' && 
      $view->getDisplay()->definition['id'] == 'rest_export') {
    foreach ($view->result as $row) {
      if (isset($row->_entity->created)) {
        // Alter the entity created property to a readable ISO8601 date
        $row->_entity->created->setValue(date('c', $row->_entity->created->value));

There might be a better part of Views rendering pipeline (e.g. REST views export has some templating system) to place this in, but it still renders out the altered serialized property.


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.