2

I have view with one date field. Now I want to preprocess this date field(lets say I want to change the date format).

Twig file override : views-view-field--field_date.html.twig

Prepocess code I have tried but kint is showing me only DEPTH TOO GREAT value.

function my_sub_theme_preprocess_views_view_field__field_date(&$variables) {
    kint($variables['view']);
}

Please let me know how to preprocess this field. Is it possible to change date format in twig itself?

2 Answers 2

4

The output of views field templates is flat markup. You can't change a render array like you can in field templates when rendering entities in view modes.

So if you want to change the output you have to start from scratch with the values that are in the database. In views field templates you can use the getValue() method from the field variable. As parameter you have to provide row:

{{ field.value(row) }}

The result is a single value or an array for multivalue fields.

Example

views-view-field--created.html.twig

The raw value from created is a unix timestamp

Timestamp: {{ field.value(row) }}

You can use the timestamp for example to reformat the date with the date filter from twig:

Date: {{ field.value(row)|date("m/d/Y") }}
2

Yes. A date format can be changed from TWIG template itself. Look for more documentation on Date in Twig here - http://twig.sensiolabs.org/doc/2.x/filters/date.html

About DEPTH TOO GREAT of Twig. It's coming due to Kint having setting of less maxLevel than what is there in the variable you are trying to dump.

You can change DEPTH in same function itself by writing something like -

$_kintSettings['maxLevels'] = 10;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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