Hello
I'm programmatically creating a block in a module and simply want to add a css class to the block wrapper. There seems to be some advice on how manipulate block classes in template preprocess functions, but I just want to be able to set it where I create the block. Is this possible?
This is what I have at the moment
function tp_core_block_info() {
$blocks['myblock'] = array(
'info' => t('My Block'),
'cache' => DRUPAL_NO_CACHE,
);
}
function mymodule_core_block_view($delta = '') {
case 'myblock' :
$block['content'] = 'My Content';
break;
}
return $block;
}
This produces a block with the following html wrapper
<div id="block-my-module-my-block" class="block my-block content-region block-my-block block-my-module-my-block odd block-without-title">
*rest of block html here*
All I want to do is add a new class "no-background" to the class attribute at the time the block is created. I know this won't work, but I'm looking for something like this:
function tp_core_block_info() {
$blocks['myblock'] = array(
'info' => t('My Block'),
'cache' => DRUPAL_NO_CACHE,
'class' => 'no-background',
);
}The reason I want to do it this way is that for the most part, the blocks on my site will all have a white background, but in certain cases I want to turn this background off, so for these blocks I want to set a common css class that will disable it.
Is this possible?
I see that you can set a "properties" property in hook_block_info, but I don't see any information on what type of properties you can set or how.
Any help would be much appreciated!
Comments
You can use block_class
You can use block_class module to add the custom CSS class individually.
You can also use default generated class, because drupal generate classes based on block delta (key used in hook_block_info()).
-Imran
You can achieve by using hook_preprocess_block
You can achieve by using hook_preprocess_block...
Thank you very much :-)
Thank you very much :-)
I've looked into this in the
I've looked into this in the past, and as far as I can tell it cannot be done from the block view definition. It has to be done in a preprocess function as mentioned by other users.
I'd actually consider it a bug, and the 'content' section of a block is a render array, so you should be able to set #attributes, but setting #attributes does not work.
Contact me to contract me for D7 -> D10/11 migrations.
Japan, I've found that you
Japan, I've found that you can attach any array you like to the block object in the view definition, including an array of classes. It is then passed to preprocess_block where you can add it to your block as pointed out by razonklnbd.
I've found this to work in drupal 6 at least, yet to try it in 7.
The block system changed
The block system changed quite a bit between Drupal 6 and Drupal 7.
Contact me to contract me for D7 -> D10/11 migrations.
it tested in d7
for your information it tested in D7 - thanks