-3

I have custom block holiday this block is not updating once I'm updating data in database when I'm clearing the cache block is working. Is there any solution for this cache issue? I'm attaching the code, have a look at it:

<?php
global $base_url;
clearstatcache();
\Drupal::service('page_cache_kill_switch')->trigger();
$msg='<div id="notice">    
  <ul id="holiday">';
   $holiday_select = db_query("select id,student_holiday from student_holiday as b where  b.isactive = 't' and display_from <=now() and display_to>=now() order by id desc");
                     foreach ($holiday_select as $h_data)
                            {
                                $student_holiday[] = $h_data -> student_holiday;
                             }

                  if(count($id) > 0)
                                {
                                      $s = 0;
                                     while($s<count($id))
                                        {
                                             $holiday_view = $student_holiday[$s];
                                                $msg.='<li><a href="'.$base_url.'/holiday"> '.$holiday_view.' </a></li>';
                                                $s++;
                                            }

                    }                   
                    else{
                    $msg.='<li><a href="#">No holiday </a></li>';
                    }
                    $msg.='</ul>

</div>';

echo $msg;
?>
0

Add the below preprocesser function to your .theme file

/**
 * Implements hook_preprocess_HOOK() for block.html.twig.
 */
function YOURTHEME_preprocess_block(&$vars) {
  if ($vars['derivative_plugin_id'] == 'your_block_ID') {
    // This stops the block being cached.
    $vars['#cache']['max-age'] = 0;
  }
}
  • i have implement in my them but its not work for me function ngo_preprocess_block(&$vars) { if ($vars['derivative_plugin_id'] == '29') { // This stops the block being cached. $vars['#cache']['max-age'] = 0; } }i got 29 as block id while i click my custom block its leads to url as` /block/29` – sani Feb 20 at 10:05
  • already i have try purencool.com/… – sani Feb 20 at 10:15
  • Have you tried it in YOURTHEME_preprocess(&$vars) function than in the YOURTHEME_preprocess_block(&$vars)? It might work. – Sudheesh S Babu Feb 20 at 13:17
  • could you pls add some coding i'm using Pixture Reloaded theme as ngo so i have added function ngo_preprocess(&$vars){ ngo_preprocess_block(&$vars){ if ($vars['derivative_plugin_id'] == '29') { $vars['#cache']['max-age'] = 0; } } } but its not working – sani Mar 4 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.