2

I am running a Drupal 8.0.5, I have a content type called show which has fields title, an entity referenced called movie and a date time. I am trying to create a view of the latest shows of each movie. The initial view is like this which shows the movie and show time

movies and show times

I enabled aggregation. Aggregation functions i used are For movie - group results together and for datetime - maximum. The result is as follows

enter image description here

it just gives 2,016 as date time field where i was expecting Mon 03/21/2016 15:00:00. Are we not supposed to aggregate on date field? I thought since MAX usually works in database for timestamp, it is valid here also. Can anything be done to achieve this result?

1
  1. First of all, select "None" for "Thousands marker" to have a properly formatted timestamp value;
  2. In the "REWRITE RESULTS" section you can override the output of the field. Given I have the aggregated field "created" overriding the field would look as following:

    {{ created |timestamp_format('Y-m-d') }}

enter image description here

  • This would be the correct answer if timestamp_format would be a Twig filter but it's not. It's called date. So you want to write {{ create | date('Y-m-d') }} in there. – Smartsheet eng Nov 10 at 6:00
0

Yes, the issue should be addressed in the views module. You can use hook_views_pre_render or (hook_views_post_execute) to modify the values example:

YOUR_MODULE_views_pre_render(ViewExecutable $view) {
    if ($view->id() == 'YOUR_VIEW_ID'){
        $format = "d/m/Y H:i";
        foreach($view->result as $key => $result){
            $result->STRING_FIELD = date($format, result->TIMESTAMP_FIELD);
        }
    }
}

WARNING: the destination field must be a string, so you have to add a mock string field to the view (STRING_FIELD).

$result->TIMESTAMP_FIELD = date($format, result->TIMESTAMP_FIELD);

will not work

0

The easiest way to correct the output of this field (show the full date instead of just the year) seems to be creating a Twig template file for the field, then calling the value directly; you can use twig filters to modify the format as needed.

For example:

  • View: safety_reports
  • Display: page_2
  • Date field: field_safetyrpt_mailing_date
  • Aggregation type: Maximum
  • Template file name: views-view-field--safety-reports--page-2--field-safetyrpt-mailing-date.html.twig
  • Template file contents:

    {{ row.node__field_safetyrpt_mailing_date_field_safetyrpt_mailing_d }}

    or

    {{ row.node__field_safetyrpt_mailing_date_field_safetyrpt_mailing_d | date('F j, Y') }}

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.