hook_ENTITY_TYPE_view() only fires once on cache rebuild? When implementing hook_ENTITY_TYPE_view() for nodes, I notice the following behaviour:


Cache rebuild
Navigate to node/1 // Message 'Node viewed!' displays
Navigate to node/2 // Message 'Node viewed!' displays
Navigate to node/1 // No message
Navigate to node/2 // No message
Cache rebuild
Navigate to node/1 // Message 'Node viewed!' displays


My code:

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;

/**
 * Implements hook_ENTITY_TYPE_view() for node entities.
 */
function sitelog_node_view(array &amp;$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
    drupal_set_message(t('Test result: @result', array('@result' =&gt; 'Node viewed!')));
}


Is this the intended behaviour (it doesn't fire every time an entity is viewed) of hook_ENTITY_TYPE_view()?
Yes, it is. Entities are render cached. If your output is dynamic/depends on something, you need to provide the necessary cache contexts, use placeholders or something like that.

See https://www.drupal.org/docs/8/api/render-api/cacheability-of-render-arrays.

If it's just about debugging locally, there is an example.settings.local.php file that has a example lines to disable those caches, but make sure you always test it with enabled caches before deploying.