Conditionally display field based on comparison to other field 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(&amp;$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 &amp;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?
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(&amp;$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 }}
    &lt;div class="event_dates clearfix"&gt;
      {{ content.field_eventdate }}
      {% if content.field_eventend %}
        &amp;nbsp;&amp;ndash; {{ content.field_eventend }}
      {% endif %}
    &lt;/div&gt;
    {{ 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.