0

I have a block which renders fields of a node. The output of each field is controlled in the view mode settings of the node entity. Now, how can I add caching which is depended on the view mode settings?

1 Answer 1

1

Found it out :)

Using entity_get_display to actually load the display view settings of the current node and then just merge the tags:

<?php

public function getCacheTags() {
  if ($node = $this->getNode()) {
    $cacheTags = Cache::mergeTags(parent::getCacheTags(), array('node:' . $node->id()));
    $display = $this->getEntityViewDisplay(); // function which uses entity_get_display
    $cacheTags = Cache::mergeTags($display->getCacheTags(), $cacheTags);
  }
  else {
    $cacheTags = parent::getCacheTags();
  }

  return $cacheTags;
}
4
  • 1
    Did you consider using the ctools view entity block, that accepts a node as context and renders it in a given view mode.
    – Berdir
    Commented Jul 22, 2016 at 18:24
  • @Berdir I dont want all the visible view mode fields in that specific block - if that makes any sense
    – Alex
    Commented Jul 26, 2016 at 7:43
  • You can always make a new view mode that only contains the fields that you want.
    – Berdir
    Commented Jul 26, 2016 at 7:45
  • But that sounds like an overhead of view modes, doesnt it?
    – Alex
    Commented Jul 26, 2016 at 7:50

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.