1

I have a content type Event that has a startdate (field_eventdate) and an optional enddate (field_eventend). Although it's optional, the enddate is often filled in by the users with the same date as the startdate. So I want to hide the enddate if it's equal to the startdate or display it on the same line as the startdate when it's different.

e.g.

2020-02-02 10:00
Here comes more text for a single day event

and

2020-02-02 10:00 - 2020-02-03 16:00
Here is more text for a multiday event

So far I managed to delete the enddate from the render array when it's equal:

function mytheme_preprocess_node__event(&$variables) {
  $startdate = $variables['content']['field_eventdate'][0]['#markup'];
  $enddate = $variables['content']['field_eventend'][0]['#markup'];

  if ($startdate === $enddate) {
    unset($variables['content']['field_eventend']);
  }
  else {
    // add an &em-dash; so the dates are nicely spaced
    $variables['content']['field_eventend'][0]['#markup'] = " — " . $enddate;
    // add float-left to the start date so they end up on the same line
    // TODO: what goes here?
  }
}

But I cannot find out how to notice in a twig template that I have a startdate and an enddate and therefore add 'float' css classes to put them in line.

I have tried to add a special attribute to the 'field_eventdate' field, and add a 'float-left' class in the twig template for the field, but the custom attribute gets lost somewhere.

How can I solve this problem?

5
  • Does this have to be done on preprocessing level ? Perhaps you can use drupal.org/project/fico ? Have you tried ?
    – GiorgosK
    Commented Jan 27, 2020 at 5:56
  • @GiorgosK Thanks for the suggestion. It sounds like an interesting module, but is has a dependency in Display Suite, which I don't intend to use.
    – hepabolu
    Commented Jan 27, 2020 at 6:54
  • I looked into it some more and from my first attempts I found: I need to build a plugin (which is beyond my skill set) that does what my preprocess function already does and I still have no solution for the inline problem. Note: the DS dependency hasn't thrown an error so far with v1.0.
    – hepabolu
    Commented Jan 27, 2020 at 7:05
  • Using what you have already done, you can check in the twig template if the field exists and if it does you can add the classes you want to use. {% if content.field_eventend %} --print the field with wanted classes-- {% endif %}
    – Mike Chris
    Commented Jan 27, 2020 at 13:21
  • Thanks @MikeChris. Which twig template? I created a template field--field-eventdate.html.twig but I cannot find if field_eventend exists. I prefer to fall back to the default templates as much as possible, so I don't want to create a node--event.html.twig if possible. Can I set a custom attribute on the field_eventdate in the preprocess function? If so, how?
    – hepabolu
    Commented Jan 27, 2020 at 20:07

1 Answer 1

1

I finally solved the problem by adding a template for the content type. This allowed me to simplify the preprocess function:

function mytheme_preprocess_node__event(&$variables) {
  $startdate = $variables['content']['field_eventdate'][0]['#markup'];
  $enddate = $variables['content']['field_eventend'][0]['#markup'];

  if ($startdate === $enddate) {
    unset($variables['content']['field_eventend']);
  }
}

I then copied the node.html.twig template to node--event.html.twig. I only changed the main {{ content }} part into:

    {{ content.field_tags }}
    {{ content.field_eventlocation }}
    <div class="event_dates clearfix">
      {{ content.field_eventdate }}
      {% if content.field_eventend %}
        &nbsp;&ndash; {{ content.field_eventend }}
      {% endif %}
    </div>
    {{ content|without('field_tags', 'field_eventlocation', 'field_eventdate', 'field_eventend') }}

This replaced my attempt to create and modify field--field-eventdate.html.twig template.

Maybe this is not the best option, but at least it works.

Your Answer

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

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