-1

I am using date range field in views and would like to display dates in the format 5-May +3 days using twig. Te values that can be used as per drupal are:
{{ field_mycontent_dates__value }} == Raw value {{ field_mycontent_dates__end_value }} == Raw end_value

How can I make the calculation and display it using rewrite output option in views?

0

I think this is what you are looking for? There is good info here to pass the cred... https://stackoverflow.com/questions/15657687/twig-date-difference

Top answer:

{# endDate and startDate are strings or DateTime objects #}
{% set difference = date(endDate).diff(date(startDate)) %}
{% set leftDays = difference.days %}
{% if leftDays == 1 %}
  1 day
{% else %}
  {{ leftDays }} days
{% endif %}

Another tidbit from same question:

{{ entity.ending_date|time_diff(entity.starting_date) }}

If not let us know. Plus depending on how you have configured Drupal to render out the value of the {{ field_mycontent_dates__value }} field, it may be as simple as something like:

{% set time = value_1 - value_2 %} then {{ time | date('F j i') }}

Ultimately, what you're looking for I believe are "twig filters".

2
  • 2
    Wouldn’t it just be easier to preprocess this data and send it into the template?
    – Kevin
    Nov 30 '17 at 0:17
  • Unfortunately, none of these answers work. I will give it a try using @Kevin suggestion. Nov 30 '17 at 17:54
0

I ended up creating a pre-processor as @Kevin suggested:

cargaview.info.yml

       name: cargaview
    type: module
    description: modifies carga node fields
    core: 8.x
    package: Flete
    dependencies:
      - field
enter code here

src/Plugin/Field/FieldFormatter/CargaViewFormatter.php

<?php

namespace Drupal\cargaview\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Datetime\DrupalDateTime;

/**
 * Plugin implementation of the 'carga_view' formatter.
 *
 * @FieldFormatter(
 *   id = "carga_view",
 *   label = @Translation("Carga View"),
 *   field_types = {
 *     "daterange"
 *   }
 * )
 */
class CargaViewFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'separator' => '+',
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    $separator = $this->getSetting('separator');

    foreach ($items as $delta => $item) {
      if (!empty($item->start_date) && !empty($item->end_date)) {
        /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
        $start_date = $item->start_date;
        /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
        $end_date = $item->end_date;

        if ($start_date->getTimestamp() !== $end_date->getTimestamp()) {

          $date_diff = $end_date->getTimestamp() - $start_date->getTimestamp();
          $day_diff = floor($date_diff / (60 * 60 * 24));

          $elements[$delta] = [
            '#type' => 'markup',
            '#markup' => date('d-M-Y', $start_date->getTimestamp()) . ' ' . $separator . ' ' . $day_diff . 'd',
          ];
        }
        else {
          $elements[$delta] = [
            '#type' => 'markup',
            '#markup' => date('d-M-Y', $start_date->getTimestamp()),
          ];

          if (!empty($item->_attributes)) {
            $elements[$delta]['#attributes'] += $item->_attributes;
            // Unset field item attributes since they have been included in the
            // formatter output and should not be rendered in the field template.
            unset($item->_attributes);
          }
        }
      }
    }

    return $elements;
  }
}

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.