All pages

Drupal send cache invalidation to Varnish on content update or delete

How to automatically purge Varnish cache when a node is updated or deleted using the Advanced Varnish module.

function mymodule_node_inv_varnish($node) {
  $uri = $node->toUrl()->toString();
  $cm = \Drupal::service('adv_vanish.cache_manager');
  if (strpos($uri, '/node/') === 0)
    $uri = \Drupal::service('path_alias.manager')->getAliasByPath($uri);
  $cm->purgeUri($uri);
  if ($node->getType() === 'news') $cm->purgeUri('/news');
}

function mymodule_node_update($node) { mymodule_node_inv_varnish($node); }
function mymodule_node_delete($node) { mymodule_node_inv_varnish($node); }
Drupal send cache invalidation to Varnish on content update or delete | condurachi.ro