The Dashboards with Layouts module, accessible at https://www.drupal.org/project/dashboards, stands out as an impressive tool for creating personalized dashboards through blocks and an intuitive layout tool. Initially, I integrated custom blocks into specific modules, including blocks displaying views that required automatic refreshing. To address this, I initially employed JavaScript. However, I began to explore the possibility of leveraging the block rendering system to streamline the process for all blocks. Consequently, I developed a dashboards auto refresh module to achieve precisely that.
Enabling this module ensures that any blocks, whether they are views or custom, will automatically reload every 30 seconds. The module includes an endpoint responsible for rendering a block's markup. The following code demonstrates the implementation:
\Drupal::service('page_cache_kill_switch')->trigger();
$block_manager = \Drupal::service('plugin.manager.block');
$plugin_block = $block_manager->createInstance($pluginId, []);
$build = $plugin_block->build();
print \Drupal::service('renderer')->renderRoot($build);
var url = window.location.toString();
if (!url.includes('disablerefresh')) {
// Refresh blocks in the dashboard using polling.
interval = setInterval(function () {
refresh();
}, 30000);
}
function refresh() {
// Refresh all custom block panels in the dashboard.
$('.panel__content').each(function () {
if ($(this).attr('id')) {
$.ajax({
url: '/dar/block/' + $(this).attr('id'),
type: 'GET',
dataType: 'html',
context: $(this),
success: function (html) {
$(this).html(html);
}
});
}
});
}
This setup allows for the automatic reloading of blocks, enhancing the overall user experience in dashboard management.
Add new comment