I have a custom module "jsblock." When the page renders, the module should get the title of the page, pass it to drupal_add_js() and ultimately render an output in a block.
I tested the module with the template name suggested by the system. The output rendered in the block as expected. Now, I'm trying to add a custom template to the module but the variable appears to no longer be passed to the block.
Directory structure
I have been playing around with hook_block_view() and a helper function to handle the drupal_add_js() function, but I think I am not attaching it to the block correctly.
jsblock.module file
// Helper
function get_the_page_title() {
if ($node = menu_get_object('node')) {
$title = drupal_get_title();
drupal_add_js(array('jsblock' => array('test' => $title)), 'setting');
}
}
// Theme
function jsblock_theme($existing, $type, $theme, $path){
return array(
'jsblock_custom_theme' => array(
'variables' => array('content' => NULL),
'template' => 'jsblock_custom_theme',
'path' => drupal_get_path('module', 'jsblock') .'/theme/',
)
);
}
// Print
function jsblock_block_view($delta = 'jsblock') {
if($delta == 'jsblock')
{
$block = array();
$block['content'] = get_the_page_title();
$block['content']['#attached']['css'][] = drupal_get_path("module", "jsblock")."/css/styles.css";
$block['content'] = theme('jsblock_custom_theme');
return $block;
}
}
I checked the console for Drupal.settings.jsblock.test and it is returning the expected value. I can see the block is printing based on an expected outline, but the data is not rendered.
How can I pass the JS variable from the page to the block that uses a custom template?