1

I wrote a module that implements hook_entity_view_display_alter(). I modify the display settings of one of the fields depending on some configuration setting of my module.

/**
 * Implements hook_entity_view_display_alter().
 */
function ps_events_entity_view_display_alter(EntityViewDisplayInterface $display, array $context) {
  // Change the date formats of the date field on the full page display based
  // on configured settings.
  if ($context['entity_type'] === 'node' && $context['bundle'] === 'ps_events' && $context['view_mode'] === 'ps_full_content_no_lb') {
    $eventsSettings = \Drupal::config('ps_events.settings');
    $dateComponent = $display->getComponent('field_ps_events_date');
    if ($dateComponent) {
      $dateComponent['settings']['date_format'] = $eventsSettings->get('detail_page_date_format_day_and_time');
      $display->setComponent('field_ps_events_date', $dateComponent);
      $display->addCacheTags(['config:ps_events.settings']);
    }
  }
}

However, the cache tag I add doesn't seem to have any effect. When I inspect the cache_render table and look at the entry for this entity view display, my cache tag is not present. Changing the settings of my module does not clear the cache for this entity view display.

As a workaround, I also implement hook_entity_view_alter(), which operates on the complete render array of the entity view display. I add the cache tag here and it works.

This seems like either a bug in core, or maybe I misunderstand the purpose of cache tags on the EntityViewDisplay object?

  • 1
    Did you check the cache_config table if your entry config:ps_events.settings exist ? – berramou Jan 6 at 18:56
2

For that to work, core would have to use getCacheTags() from that config entity, which it doesn't do.

I'm not sure if that should be changed, core currently does not add the cache tags of that entity on purpose, instead it it invalidates the generic {$entity_type}_ cache tag on saving such an entity in \Drupal\Core\Entity\Entity\EntityViewDisplay::postSave(). Consdering that this changes only very infrequently once a site is live, this is more performant as there are fewer cache tags to check on each request.

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.