I have an odd issue where the variable $block_html_id seems not to be found for a custom block that I have been trying to get working.

I get the notice: Notice: Undefined variable: block_html_id in include() (line 48 of /var/www/drupal/project.loc/sites/all/modules/custom/ng_blocks/ng-header.tpl.php)

This notice happens regardless of the theme being used.

The block is defined like so:

/**
* Implements hook_block_info().
*/
function ng_blocks_block_info() {
  $blocks = array();
  $blocks['ng_header'] = array(
    'info' => t('NG Header block'),
  );
  return $blocks;
}
/**
* Implements hook_block_view().
*/
function ng_blocks_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'ng_header':
      $block['subject'] = 'This is the header';
      $block['content'] = theme('ng_header', array('testing' => 'This is a test.'));
      break;
  }
  return $block;
}

function ng_blocks_theme() {
  $path = drupal_get_path('module', 'ng_blocks');
  
  return array(
    'ng_header' => array(
      'template' => 'ng-header',
      'render element' => 'elements'
    )
  );
}

And the template:

<div id="<?php print $block_html_id; ?>" class="<?php print $classes; ?>"<?php print $attributes; ?>>
  Test content
</div>

I have been scratching my head for a while with this one. This error occurs even if I implement my own template_preprocess_bllock(&$variables) function and set said variable within it.

What might I be doing wrong here?

Thanks!

Comments

Jaypan’s picture

$block['content'] = theme('ng_header', array('testing' => 'This is a test.'));

You are theming the content of the block, not the block itself. This means two things:
1) Your template will not contain the block elements. It will only contain the block content. This means that your variables will not contain variables that relate to the block wrappers - and $block_html_id, $classes and $attributes are all variables that relate to the block wrappers. So your template should only contain:

Test content

2) If you want to change the wrapper (div) elements of the block, you don't need to create a new theme at all. All you do is create the template block--module--delta.tpl.php (if I recall correctly) and make your changes to the block wrappers in this template.

derekwebb1’s picture

It's been awhile since I did anything with blocks. Your suggestion worked. I typically use panels panes that can be themed separately. However the project I am working on at the moment is a not big enough for panels/panelizer etc. to be called for.