0

I am attempting to take all of the start dates of my coworkers and calculate a total # of years of experience. However I am getting the following error

Twig\Error\SyntaxError: Unexpected token "punctuation" of value ":" ("end of statement block" expected). in Twig\TokenStream->expect() (line 44

{% if title %}
  <h3>{{ title }}</h3>
{% endif %}

{% set total_seconds = 0 %} {# Initialize a variable to store the total seconds #}

{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}

  {# Assuming 'row.content' contains a date field named 'field_start_date' #}
  {% set date_str = row.content['#view'].style_plugin.getField(row_index, 'field_start_date') %}

  {% if date_str %}
    {% set today = date('now') %}
    {% set date_object = date(date_str) %}

    {# Calculate the difference in seconds using Drupal's date formatter service with createAttribute for options #}
    {% set options = create_attribute() %}
    {% set options = options.setAttribute('granularity', 1) %} 
    {% set diff_seconds = Drupal::service('date.formatter')->formatDiff(date_object.getTimestamp(), today.getTimestamp(), 'en', options) %}

    {% set total_seconds = total_seconds + diff_seconds %} {# Add the calculated seconds to the total #}

    <div{{ row.attributes.addClass(row_classes) }}>
      {{- row.content -}}
    </div>
  {% else %}
    <div{{ row.attributes.addClass(row_classes) }}>
      {{- row.content -}}
    </div>
  {% endif %}

{% endfor %}

{% if total_seconds > 0 %}
  {% set total_years = (total_seconds / (365 * 24 * 60 * 60))|round(2) %} {# Convert seconds to years and round #}
  <p>Total combined years since all dates: {{ total_years }}</p> {# Display the final result #}
{% endif %}

commenting out this line {#% set diff_seconds = Drupal::service('date.formatter')->formatDiff(date_object.getTimestamp(), today.getTimestamp(), 'en', options) %#} removes any error, but doesn't return the single value that I am expecting.

1 Answer 1

0

Got past the error by rewriting the offending line. Still not giving the results I want but that is another issue.

    {% if title %}
  <h3>{{ title }}</h3>
{% endif %}


{% set total_seconds = 0 %} {# Initialize a variable to store the total seconds #}

{% for row in rows %}
  {# ... (Code to set row_classes remains the same) ... #}

  {# Assuming 'row.content' contains a date field named 'field_start_date' #}
  {% set date_str = row.content['#view'].style_plugin.getField(row_index, 'field_start_date') %}

  {% if date_str %}
    {% set today = date('now') %}

    {# Calculate the difference in seconds using Twig's date and diff functions #}
    {% set diff_interval = date(date_str).diff(today) %}

    {% if diff_interval %}
      {% set diff_seconds = (diff_interval.y * 365 * 24 * 60 * 60) + 
                             (diff_interval.m * 30 * 24 * 60 * 60) +  
                             (diff_interval.d * 24 * 60 * 60) + 
                             (diff_interval.h * 60 * 60) + 
                             (diff_interval.i * 60) + 
                             diff_interval.s %}

      {% set total_seconds = total_seconds + diff_seconds %} {# Accumulate the total tenure in seconds #}
    {% endif %}
  {% endif %}

  {# ... (Rendering the row content remains the same) ... #}

{% endfor %}

{# Calculate and display the total combined tenure #}
{% if total_seconds > 0 %}
  {% set total_years = (total_seconds / (365 * 24 * 60 * 60))|round(2, 'floor') %} {# Convert to years and round down #}
  <p>Total combined tenure of all employees: {{ total_years }} years</p> 
{% else %}
  <p>No employee start dates found.</p> 
{% endif %}

Sign up to request clarification or add additional context in comments.

2 Comments

You should extend twig and a custom Twig\TwigFunction and move the logic to there
Please mention the issue you are facing and what's your expectation from the code?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.