1

I'm having trouble with refreshing content in a block created with a custom module, placed within a panels page. My end goal is to display either regular or seasonal hours based on a set date range. My hook_theme() implementation looks like the following:

function daily_hours_theme() {
  if (daily_hours_is_summer()) {
    $table_template = daily_hours_get_summer_vars();
  } else {
    $table_template = daily_hours_get_regular_vars();
  }
  $items =  array(
    'todays_hours_table' => $table_template,
    'daily_hours_text_content' => array(
      'render element' => 'element',
    )
  );
  return $items;
}

I'm using hook_block_info() to test cache settings; the following doesn't seem to have an effect on caching:

function daily_hours_block_info() {
  $blocks['hours_table'] = array(
    'info' => t('Daily Hours Table'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

And hook_block_view() to call the theme function:

function daily_hours_block_view($delta = '') {
  switch ($delta) {
    case 'hours_table':
      $block['content'] = theme('todays_hours_table');
      break;
  }
  return $block;
}

Ideally, I'd clear the module's block data at midnight every day, but I'd settle for just knowing how to implement any caching rules in this context.

  • If you place the block outside the panel, same result? – No Sssweat May 9 '16 at 23:39
  • Like if I add it via the normal block interface rather than panels? Yes, the content is still caching. – nkanderson May 9 '16 at 23:47
  • could this be the answer Block: DRUPAL_NO_CACHE not working ? – No Sssweat May 10 '16 at 0:03
  • I don't believe so - I don't have either "Cache pages for anonymous users" or "Cache blocks" set to be true on /admin/config/development/performance. – nkanderson May 10 '16 at 0:15
  • I don't have either "Cache pages for anonymous users" or "Cache blocks" set to be true Then it's very odd that your blocks are caching. Are you using Varnish/Redis or some other caching contrib module/tool ? – No Sssweat May 10 '16 at 0:22
0

The variables were being cached at some point in the hook_theme() function, so after reading the documentation on hook_theme(), I moved the logic to conditionally use different variables in to my hook_block_view() implementation, passing them in to the call to theme() at that point. So that function looks like the following:

function daily_hours_block_view($delta = '') {
  if (daily_hours_is_summer()) {
    $variables = daily_hours_get_summer_vars();
  } else {
    $variables = daily_hours_get_regular_vars();
  }
  switch ($delta) {
    case 'hours_table':
      $block['content'] = theme('todays_hours_table', $variables);
      break;
  }
  return $block;
}

And my hook_theme() function:

function daily_hours_theme() {
   $items =  array(
     'todays_hours_table' => array(
       'template' => 'hours_table',
       'variables' => array(
          // set default variables here
       )
     )
   );
   return $items;
 }

The cache setting in my hook_block_info() implementation did not seem to have an impact once I set up the logic in the hook_block_view() function.

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.