First of all, thank you for this great module. Works awesome with standard blocks as well as blocks created by other modules such as Rotor.

However, I am having an issue getting block refresh to work with the Homebox module. Here is my test scenario:

1. Base install with the following modules:
- Homebox
- jquery UI
- block refresh
- PHP filter
2. Create a block with the following code:

<?php

echo rand(5, 15);
?>

3. Enabled manual and 1 second autorefresh for the new block.
4. Add block to content section of garland theme.
5. Watch the number refresh and randomly change every second.

So, that works great. Now, when I add a homebox page and the block to any column on the page, the block doesn't refresh nor does the manual refresh button show up. Can someone look at the following code from the homebox-block.tpl.php file and see if anything in there is suspect?:

<?php
// $Id: homebox-block.tpl.php,v 1.1.2.2 2009/05/26 14:18:32 jchatard Exp $

/**
 * @file
 * homebox-block.tpl.php
 * Default theme implementation each homebox block.
 */
?>
<div id="homebox-block-<?php print $block->module .'-'. $block->delta; ?>" class="<?php print $block->homebox_classes ?> clear-block block block-<?php print $block->module ?>">
  <div class="homebox-portlet-inner">
    <h3 class="portlet-header"><?php print $block->subject ?></h3>
    <div class="portlet-config">
      <?php if (variable_get('homebox_users_use_colors_'. $pid, FALSE)): ?>
        <div class="homebox-colors">
          <?php for ($i=0; $i < HOMEBOX_NUMBER_OF_COLOURS; $i++): ?>
            <span href="#" class="homebox-color-selector" style="background-color: <?php print variable_get('homebox_color_'. $pid .'_'. $i, '#E4F0F8') ?>;">&nbsp;</span>
          <?php endfor ?>
        </div>
      <?php endif ?>
      <?php if ($block->module == 'views'): ?>
        <?php $filters = theme('homebox_views_exposed_filter', $block) ?>
        <?php print $filters ?>
      <?php endif ?>
      <?php if (variable_get('homebox_users_use_colors_'. $pid, FALSE) || $block->module == 'views' && !is_null($filters)): ?>
        <div class="clear-block"></div>
      <?php endif ?>
    </div>
    <div class="portlet-content"><?php print $block->content ?></div>
  </div>
</div>

Also, here's the homebox.tpl.php code:

<?php
// $Id: homebox.tpl.php,v 1.1.2.1 2009/05/19 13:58:39 jchatard Exp $

/**
 * @file
 * homebox.tpl.php
 * Default layout for homebox.
 */
?>
<div id="homebox" class="column-count-<?php print $column_count ?>">

  <a href="#" id="homebox-add"><?php print t('Add content') ?></a>

  <ul id="homebox-settings" class="homebox-settings-hidden">
    <?php foreach ($available_blocks as $key => $block): ?>
      <li>
        <input type="checkbox" class="homebox_toggle_box" <?php print $block['checked'] ?> id="homebox_toggle_<?php print $block['dom_id'] ?>" /> <?php print $block['subject'] ?>
      </li>
    <?php endforeach ?>
  </ul>

  <?php for ($i = 1; $i <= count($regions); $i++): ?>
    <div class="homebox-column" id="homebox-column-<?php print $i ?>">
      <?php foreach ($regions[$i] as $key => $weight): ?>
        <?php foreach ($weight as $block): ?>
          <?php if ($block->content): ?>
            <?php print theme('homebox_block', $block, $pid) ?>
          <?php endif ?>
        <?php endforeach ?>
      <?php endforeach ?>
    </div>
  <?php endfor ?>

  <div class="clear-block"></div>
</div>

<?php
// Print CSS classes based on colors
print $color_css_classes;
?>
<!-- End Homebox -->

My assumption is that the way homebox is displaying the block in the code. Can anyone assist, please?

Thank you!

Comments

jlslatton’s picture

Also, here is some code from the block_refresh.module file that may be of interest in getting it to work:

/**
 *  call the js for an enabled block div.
 *  you might need to override $div if your blocks have different id's than the drupal standard.
 *  or you may wish to manually put this code in your block.tpl.php file for fine-tune control,
 *  although that would take some finagling to do properly.
 */
function theme_block_refresh_js($block) {
  // this is the block div css id and content class
  $div = "#block-{$block['block']['block']}-{$block['block']['delta']} .content";

  // we store seconds, but js expects milliseconds
  $timer = $block['timer'] * 1000;

  $base = base_path();
  $js .= "  _block_refresh_data['$div'] = new block_refresh_data({$timer}, '{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n";
  $js .= "  block_refresh_timer('$div');";
  return $js;
}

/**
 *  Add a manual refresh button to the header...
 */
function theme_block_manual_refresh_js($block) {
  // this is the block div css id and content class
  $div = "block-{$block['block']['block']}-{$block['block']['delta']}";

  $base = base_path();
  $url = "{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}";
  $content_url = "#$div .content";
  $js .= "  block_refresh_add_button('$div', '$url', '$content_url');";

  return $js;
}

I've tried changing to code to reflect the div ID that homebox uses:

/**
 *  call the js for an enabled block div.
 *  you might need to override $div if your blocks have different id's than the drupal standard.
 *  or you may wish to manually put this code in your block.tpl.php file for fine-tune control,
 *  although that would take some finagling to do properly.
 */
function theme_block_refresh_js($block) {
  // this is the block div css id and content class
  $div = "#homebox-block-{$block['block']['block']}-{$block['block']['delta']} .content";

  // we store seconds, but js expects milliseconds
  $timer = $block['timer'] * 1000;

  $base = base_path();
  $js .= "  _block_refresh_data['$div'] = new block_refresh_data({$timer}, '{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n";
  $js .= "  block_refresh_timer('$div');";
  return $js;
}

/**
 *  Add a manual refresh button to the header...
 */
function theme_block_manual_refresh_js($block) {
  // this is the block div css id and content class
  $div = "homebox-block-{$block['block']['block']}-{$block['block']['delta']}";

  $base = base_path();
  $url = "{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}";
  $content_url = "#$div .content";
  $js .= "  block_refresh_add_button('$div', '$url', '$content_url');";

  return $js;
}

The manual refresh button shows up now but doesn't work, nor does the autorefresh. Getting closer. Anyone with some actual code knowledge able to help me here?

Any help is greatly appreciated!

jlslatton’s picture

Ok, so I got it working! Kinda. See the below code change:

/**
 *  call the js for an enabled block div.
 *  you might need to override $div if your blocks have different id's than the drupal standard.
 *  or you may wish to manually put this code in your block.tpl.php file for fine-tune control,
 *  although that would take some finagling to do properly.
 */
function theme_block_refresh_js($block) {
  // this is the block div css id and content class
  $div = "#homebox-block-{$block['block']['block']}-{$block['block']['delta']} .portlet-content";

  // we store seconds, but js expects milliseconds
  $timer = $block['timer'] * 1000;

  $base = base_path();
  $js .= "  _block_refresh_data['$div'] = new block_refresh_data({$timer}, '{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n";
  $js .= "  block_refresh_timer('$div');";
  return $js;
}

/**
 *  Add a manual refresh button to the header...
 */
function theme_block_manual_refresh_js($block) {
  // this is the block div css id and content class
  $div = "homebox-block-{$block['block']['block']}-{$block['block']['delta']}";

  $base = base_path();
  $url = "{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}";
  $content_url = "#$div .portlet-content";
  $js .= "  block_refresh_add_button('$div', '$url', '$content_url');";

  return $js;
}

As you can see, I added the correct css class that homebox was using for the div (homebox-block) and content (.portlet-content). The manual refresh works as well as the autorefresh!

Well, of course, now it doesn't work for any blocks outside of homebox. While this isn't a huge deal for me currently, I can see a need for a refresh of a block outside of homebox at some point in the near future.

Can someone change the code logic above to check for "homebox-block" and ".portlet-content" (or any variant thereof) as well as the default? I could change it on the homebox end but it seems like it would be easier to get block_refresh to look for both.

Again, any help is greatly appreciated!

velosol’s picture

Total shot in the dark here, and it may break other things, but...

You could try adding the original '$div' line back in as, say, $div_two and write an additional callback a la:

function theme_block_refresh_js($block) {
  // this is the block div css id and content class
  $div = "#homebox-block-{$block['block']['block']}-{$block['block']['delta']} .portlet-content";
  $div_two = "#block-{$block['block']['block']}-{$block['block']['delta']} .content";

  // we store seconds, but js expects milliseconds
  $timer = $block['timer'] * 1000;

  $base = base_path();
  $js .= "  _block_refresh_data['$div'] = new block_refresh_data({$timer}, '{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n";
  $js .= "  block_refresh_timer('$div');\n";
  $js .= "  _block_refresh_data['$div_two'] = new block_refresh_data({$timer}, '{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n"
  $js .= "  block_refresh_timer('$div_two');\n";
  return $js;
}

And then similarly for the manual refresh portion (although I think you might end up with two buttons, only one of which works). This may also cause a bunch of calls to hit your server and generate errors because the requested div doesn't exist. But I'd encourage you to give it a try!

bocaj’s picture

Status: Active » Closed (won't fix)

Marking as "won't fix" since there has been no activity on this forum in quite some time.

ah0’s picture

I understand this thread was ended long ao but the issue is still out there...

I just tried doing the above ( #2 ) but didn't seem to work for me;
Ps. I'm using the Block Refresh 6.x-1.x-dev.

could please provide any pointers as what could be the case?
below is my edited block_refresh.module :




<?php

define('BLOCK_REFRESH_DEFAULT_AUTOMATIC', FALSE); // autorefresh disabled by default
define('BLOCK_REFRESH_DEFAULT_MANUAL', FALSE); // manual refresh disabled by default
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER', 120);  // default refreshes every two minutes, if enabled

/**
 * Implementation of hook_init()
 * Calls the jquery to refresh blocks automatically, but only if the blocks exist on the current page and are enabled
 */
function block_refresh_init() {
  // If the contrib module jQ is not installed...
  if (!module_invoke('jq', 'add', 'block_refresh')) {
    drupal_add_js(drupal_get_path('module', 'block_refresh') .'/js/block_refresh.js');
    drupal_add_css(drupal_get_path('module', 'block_refresh') . '/css/block_refresh.css');
  }
}

/**
 *  Implementation of hook_perm()
 *  Add permission for accessing auto/manually refreshed block content
 */
function block_refresh_perm() {
  return array('access block refresh content');
}

/**
 * Implementation of hook_help().
 */
function block_refresh_help($path, $arg) {
  switch ($path) {
    case 'admin/help#block_refresh':
      $block_refresh_help  = '<div class="form-item">';
      $block_refresh_help .= t("Ensure that you have !configured for user roles. Adding a permission to %access will allow a block, when configured, to be refreshed automatically and/or manually.", array('%access' => 'access block refresh content', '!configured' => l(t('configured permissions'), 'admin/user/access', array(), NULL, 'module-block_refresh')));
      $block_refresh_help .= '</div><div class="form-item">';
      $block_refresh_help .= t("You will also need to set the appropriate settings for each block that you wish to automatically and/or manually refresh by clicking on the appropriate %configure link(s) on the !admin.", array('%configure' => t('configure'), '!admin' => l(t('blocks administration page'), 'admin/build/block')));
      $block_refresh_help .= '</div>';  
      return $block_refresh_help;      
  }
}

/**
 * Implementation of hook_form_FORM_ID_alter()
 * Add a 'Block Refresh' settings fieldset to the block admin form
 */
function block_refresh_form_block_admin_configure_alter(&$form, $form_state) {
  $settings = variable_get('block_refresh_settings', array());
  $form['block_refresh'] = array(
    '#type' => 'fieldset',
    '#title' => t('Block refresh settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -1,
  );
  $form['block_refresh']['block_refresh_enable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be refreshed automatically'),
    '#description' => t('If checked, then the content of this block will be refresh automatically every x seconds defined below.'),
    '#default_value' => isset($settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['enabled']) ? $settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['enabled'] : variable_get('block_refresh_default_automatic', BLOCK_REFRESH_DEFAULT_AUTOMATIC),
  );
  $form['block_refresh']['block_refresh_timer'] = array(
    '#type' => 'textfield',
    '#title' => t('Block refresh timer'),
    '#description' => t('If this block is set to be refreshed automatically (checkbox above is checked), enter the number of <strong>seconds</strong> between each refresh.'),
    //Assuming that Block Autorefresh is enabled, then the content of this block will refresh itself periodically, every number of seconds equal to this setting. If this block is grouped to refresh with other blocks, then this setting will be ignored, instaed using the global setting of @seconds.', array('@seconds' => format_plural(variable_get('block_refresh_default_automatic_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER), '1 second', '@count seconds'))),
    '#default_value' => $settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['timer'] ? $settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['timer'] : variable_get('block_refresh_autorefresh_default_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER),
  );
  $form['block_refresh']['block_refresh_manual'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable block to be refreshed manually'),
    '#description' => t('If checked, then the content of this block may be refreshed manually by the user, by clicking on a provided (themeable) button in the block\'s subject header.'),
    '#default_value' => isset($settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['manual']) ? $settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['manual'] : variable_get('block_refresh_default_manual', BLOCK_REFRESH_DEFAULT_MANUAL),
  );
/*
  $form['block_refresh']['block_refresh_group'] = array(
    '#type' => 'checkbox',
    '#title' => t('Group with other refreshing blocks'),
    '#description' => t('If checked, then this block will be refreshed with other blocks. If also set to autorefresh, then the timer setting will be ignored, instead using the global setting of @seconds.', array('@seconds' => format_plural(variable_get('block_refresh_default_automatic_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER), '1 second', '@count seconds'))),
    '#default_value' => isset($settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['group']) ? $settings['homebox-block-' . $form['module']['#value'] . '-' . $form['delta']['#value']]['group'] : variable_get('block_refresh_group_auto', FALSE),
  );
*/
  $form['#submit'][] = 'block_refresh_submit';
}

/**
 *  Submission handler for for block_refresh_menu()
 *  This handles the submission on the specific block configuration page
 */
function block_refresh_submit($form, &$form_state) {
  $settings = variable_get('block_refresh_settings', array());
  $settings['homebox-block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['enabled'] = $form_state['values']['block_refresh_enable'];
  $settings['homebox-block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['manual'] = $form_state['values']['block_refresh_manual'];
  $settings['homebox-block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['group'] = $form_state['values']['block_refresh_group'];
  $settings['homebox-block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['timer'] = $form_state['values']['block_refresh_timer'];
  $settings['homebox-block-' . $form_state['values']['module'] . '-' . $form_state['values']['delta']]['block'] = array('block' => $form_state['values']['module'], 'delta' => $form_state['values']['delta']);
  
  variable_set('block_refresh_settings', $settings);
}

/**
 * Implemention of hook_menu()
 */
function block_refresh_menu() {
  $items = array();
/*
  $items['admin/settings/block_refresh'] = array(
    'title' => t('Block refresh'),
    'description' => t('Settings for automatic and manual refreshing of configured blocks.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('block_refresh_settings'),
    'access arguments' => array('administer site configuration'),
  );
*/
  // this will display the contents of a block, if it's configured with Block Refresh
  $items['block_refresh'] = array(
    'title' => t('Block refresh block content'),
    'page callback' => 'block_refresh_block_content',
    'access arguments' => array('access block refresh content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_footer().
 */
function block_refresh_footer($main = 0) {
  // don't bother to continue if the user can't refresh content anyway...
  if (!user_access('access block refresh content')) {
    return;
  }

  global $theme_key;
  $blocks = array();
  // Get an array of regions for the given theme
  $regions = system_region_list($theme_key);
  // Get all blocks that are in each region of the theme and put into $blocks array
  foreach ($regions as $region => $value) {
    $blocks = array_merge($blocks, block_list($region));
  }
  // Get the variable that holds all of the configured blocks information (this is an array)
  $settings = variable_get('block_refresh_settings', array());
  $js = '';
  // Test every block that is currently being displayed
  foreach ($settings as $block) {
    // If the block is configured for Block Refresh...
    if (isset($blocks[$block['block']['block'] . '_' . $block['block']['delta']])) {
      // I believe (though have not thoroughly tested) that this is an unnecessary test
      if ($block['enabled']) {
        // this is the block div css id and content class
        $div = "#homebox-block-{$block['block']['block']}-{$block['block']['delta']} .portlet-content";
      
        // we store seconds, but js expects milliseconds
        $timer = $block['timer'] * 1000;
      
        $base = base_path();
        // Set jQuery array to be set in page footer ... also checks to see whether clean URLs are enabled or not
        $js .= "  _block_refresh_data['$div'] = new block_refresh_data({$timer}, '{$base}" . (variable_get('clean_url', 0) == 0 ? "?q=" : "") . "block_refresh/{$block['block']['block']}/{$block['block']['delta']}');\n";
        $js .= "  block_refresh_timer('$div');";
      }
      // If the block is configured to be user-refreshed
      if ($block['manual']) {
        // this is the block div css id and content class
        $div = "homebox-block-{$block['block']['block']}-{$block['block']['delta']}";
      
        $base = base_path();
        $url = "{$base}block_refresh/{$block['block']['block']}/{$block['block']['delta']}";
        $content_url = "#$div .portlet-content";
        $js .= "  block_refresh_add_button('$div', '$url', '$content_url', '" . t('Refresh') . "');";
      }
    }
  }
  
  // If there is a block that is currently being displayed that is configured to work with Block Refresh...
  if (!empty($js)) {
    drupal_add_js("\$(document).ready(function() {\n$js\n})", 'inline', 'footer');
  }
}

/**
 *  Callback for admin/settings/block_refresh
 */
function block_refresh_settings() {
  $form = array();
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Global settings'),
    '#collapsible' => FALSE,
  );
/*
  $form['settings']['block_refresh_group_auto'] = array(
    '#type' => 'checkbox',
    '#title' => t('Group block refreshes'),
    '#description' => t('If checked, then all grouped block refresh enabled blocks will be refreshed together, whether manually or using the timer settings below. To group blocks together, check the appropriate block on that block\'s configuration settings page.'),
    '#default_value' => variable_get('block_refresh_group_auto', FALSE),
  );
*/
  $form['settings']['block_refresh_default_automatic_timer'] = array(
    '#type' => 'textfield',
    '#title' => t('Default block refresh timer'),
    '#description' => t('Enter the time, <strong>in seconds</strong>, that blocks will refresh when set to automatic and are set to use the default timer.'),
    '#default_value' => variable_get('block_refresh_default_automatic_timer', BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER),
  );
  return system_settings_form($form);
}

/**
 *  page callback for /block_refresh/[module]/[delta]
 *  displays the block content, without any other page information
 */
function block_refresh_block_content($block = NULL, $delta = NULL) {
  if (!isset($block) || !isset($delta) || ($block != 'block_refresh' && !module_hook($block, 'block'))) {
    drupal_not_found();
  }
  if ($block == 'block_refresh' && $delta == 'all') {
    $query = isset($_GET['blocks']) ? $_GET['blocks'] : '';
    $pairs = explode(',', $query);
    foreach ($pairs as $pair) {
      list($module, $delta) = explode('|', $pair);
      $block = module_invoke($module, 'block', 'view', $delta);
      $output .= '<div id="block-refresh-data-' . $module . '-' . $delta . '">' . $block['content'] . '</div>';
    }
    if ($output) {
      print '<div id="block-refresh-data-all" class="block-refresh-hidden">' . $output . '</div>';
    }
    exit();
  }
  $settings = variable_get('block_refresh_settings', array());
  if (!$settings['homebox-block-' . $block . '-' . $delta]['enabled'] && !$settings['homebox-block-' . $block . '-' . $delta]['manual']) {
    drupal_not_found();
  }
  $block = module_invoke($block, 'block', 'view', $delta);
  print $block['content'];
  exit();
}

/**
 * Implementation of hook_jq()
 * Allows optional integration with the jQ module.
 */
function block_refresh_jq($op, $plugin = NULL, $args = array(), $already_loaded = NULL) {
  switch ($op) {
    case 'info':
      $info = array();
      $info['block_refresh'] = array(
        'name' => t('Block Refresh'),
        'description' => t('Block Refresh allows an administrator to set up any block to automatically refresh its content every x seconds. Uses jQuery. Configure on individual blocks.'),
        'url' => 'http://drupal.org/project/block_refresh',
        'version' => t('1.2'),
        'files' => array(
          'js' => array(
            drupal_get_path('module', 'block_refresh') . '/js/block_refresh.js',
         ),
          'css' => array(
            drupal_get_path('module', 'block_refresh') . '/css/block_refresh.css',
          ),
        ),
      );
    return $info;
  }
}