jalendport/craft-preparse

Date Time field gets parsed in user locale, not site locale

Opened this issue · 1 comments

Wonderful plugin, and really helpful! I used it to find the start day of the first matrix block, and the end day of the last matrix block, used when comparing them in the twig templates, and to output them for performance reasons (better than looping through all matrix blocks in the template).

I noticed the locale section in the readme, but the problem I have is that I couldn’t get it to output the German date even though the site is (single-locale) German. I then changed my user’s language settings to German and it worked. So it looks like the field is parsed on save in the locale chosen by the saving user, not the site.

I have two fields, each is put in a separate tab in the course entry type inside the courses section.

firstDay parsed field:

{% spaceless %}
    {% set course = element %}
    {{ course.dates | length ? course.dates.one.startDay|date("j. M Y") }}
{% endspaceless %}

lastDay parsed field:

{% spaceless %}
    {% set course = element %}
    {% set lastDate = course.dates[course.dates|length - 1] %}
    {# If the last date is a single day, use that day #}
    {% if lastDate.type == "singleDay" %}
        {% set lastDay = lastDate.startDay %}
    {# If the last date is multiple days, use the end day #}
    {% elseif lastDate.type == "multipleDays" %}
        {% set lastDay = lastDate.endDay %}
    {% endif %}
    {{ lastDay|date("j. M Y") }}
{% endspaceless %}

I get 1. May 2019, which should be 1. Mai 2019, same for Dec instead of Dez, etc.

Once I switch my user over to German, re-save the course section, and reload the page, the locale is parsed correctly and I get the German dates. The same code was previously used inside the template and worked correctly.

I experimented a bit and found out that I can save the reparse field without the date filter, which also makes ordering possible by comparing the fields in each entry (dumb me).

EDIT:

This seems to be only good for outputting the date, not for comparing it, as it clearly gets saved as a string. So the question is: How can I compare these dates in my twig templates? Specifically, how can this code work with the plugin?

{% set courses = craft.entries.section("courses").relatedTo(category).orderBy("firstDay") %}
{% for course in courses %}
…
{% endfor %}