Tos Web Developer provides insights, tutorials, and advice around topics including content strategy, design, Drupal Development, Drupal Custom Module Development, Drupal 8 Custom themes, PHP, Server, Twig, and more

The code snippet that can use to disable cache for a specific page in drupal 8.

Disable cache for a specific content type

Example:

 <?php  
 use Drupal\node\NodeInterface;  
 /**  
  * @param array $build  
  * @param NodeInterface $node  
  * @param $display  
  * @param $view_mode  
  */  
 function mymodule_node_view(array &$build, NodeInterface $node, $display, $view_mode) {  
  $type = $node->getType();  
  if ($type == 'article') {  
   $build['#cache']['max-age'] = 0;  
   \Drupal::service('page_cache_kill_switch')->trigger();  
  }  
 }  

Add 'no_cache' route option

Example:

In a route definition (in a *.routing.yml file):

 mymodule.route:  
   path: '/some/path'  
   defaults:  
    _controller: '\Drupal\Some\Controller::response()'  
   options:  
    no_cache: TRUE  


No comments:

Post a Comment

| Designed And Blog Post by www.toswebdeveloper.com