Drupal 8 Quick Tip: Entity View Hooks

By default, Drupal 8 comes optimized for performance.  If you've use the standard install profile, you might find that it's difficult to develop or debug changes to the way an entity is displayed.  I spent an hour or so last night trying to figure out why my code in hook_node_view as behaving strangely.  Code would run once, and then not again, and if I made changes they wouldn't show up.  Very strange!

I eventually realized that things worked immediately after rebuilding the cache, which set me on a hunt for more info.  Drupal 7 didn't have an entity cache that worked like this, and I didn't see anything in the Performance section of config.

Eventually I did find a drupal.org discussion about working environments and settings files that seemed on point, and buried in the page was this:

$settings['cache']['bins']['render'] = 'cache.backend.null';

Rendered objects get their own cache. Adding this to your settings file will disable that cache, and you should then find that whenever you reload a page your changes to the various view and view_alter hooks are reflected every time.

Just don't forget to remove that line for production or showcase environments!

image credit: Starkiteckt via Interfacelift