How do I create a static block in code?

wipeout_dude's picture

Hi,

Hopefully someone can help me out here.. Firstly I am not a coder but I have a problem that needs code to be resolved..

I am creating a feature using "features" and I need to create a static block where the content is a simple HTML table.. Features doesn't export blocks so my plan is to add it in code to the .module file of the exported feature..

I haven't been able to find any code examples for a simple block to start from so if anyone can give me a pointer and perhaps a sample that would be great!!

TIA

EDIT: I am using Drupal 7 if that makes any difference..

Comments

Hey wipeout_dude, you are

mrconnerton's picture

Hey wipeout_dude, you are looking for the http://drupal.org/project/boxes module which are basically exportable blocks.


Matthew Connerton | matthew@aspiringweb.com
Aspiring Web a design & development agency

If you really really want to

mrconnerton's picture

If you really really want to code a block, then here is an example http://api.drupal.org/api/examples/block_example--block_example.module/7

you can put this right in your my_feature.module file, just don't mess with the include at the top of your .module file


Matthew Connerton | matthew@aspiringweb.com
Aspiring Web a design & development agency

Awesome.. 2 potential

wipeout_dude's picture

Awesome.. 2 potential solutions.. :)

Thanks mrconnerton..

This code in the feature.module will create a block

esod's picture

This code in the feature.module will create a block.

<?php
/**
* Implements hook_block_info().
*/
function FEATURENAME_block_info() {
 
$blocks['my_block_id'] = array(
   
'info' => t('Example Block'),
   
'status' => TRUE,
   
'region' => 'sidebar_first',
   
'weight' => 0,
   
'visibility' => 0,
   
'pages' => 'admin*
coder*
node/add*
node/*/edit
node/*/devel
'
,
  );

  return
$blocks;
}
 
/**
  * Implements hook_block_view().
  */
function FEATURENAME_block_view($delta='') {
 
$block = array();

  switch (
$delta) {
    case
'my_block_id':
     
$block['subject'] = t('Block Name');
     
$block['content'] = theblock_contents();
      break;
  }
 
  return
$block;
}

/**
* custom html block
* @return string
*/
function theblock_contents() {
  return
'
   <table border="1">
  <thead>
    <tr><th>Roll</th><th>Name</th><th>Marks</th></tr>
  </thead>
  <tbody>
    <tr><td>101</td><td>John</td><td>87</td></tr>
    <tr><td>102</td><td>Naman</td><td>90</td></tr>
    <tr><td>103</td><td>Chirag</td><td>85</td></tr>
    <tr><td>104</td><td>David</td><td>92</td></tr>
  </tbody>
</table>
  '
;
}
?>

Note that I placed the block into a region, set its visibility to All pages except those listed, and included those pages.

Hope this helps.

@esod - Thanks a lot.. You

wipeout_dude's picture

@esod - Thanks a lot.. You have saved my non-coding brain a lot of confusion and the members of this group from probably lots of dumb questions.. :)

You're welcome. Glad I could help

esod's picture

You're welcome. Glad I could help.

I checked hook_block_info in the api. Only 'info' is required in the return value. A more succinct answer to your question is:

<?php
function FEATURENAME_block_info() {
 
$blocks['my_block_id'] = array(
   
'info' => t('Example Block'),
  );

  return
$blocks;
}
?>

The administrator would then need to place the block into a region or a context. Also

<?php
function FEATURENAME_block_info() {
 
$blocks['my_block_id'] = array(
    ...
   
'visibility' => 0,
    ...
  );

  return
$blocks;
}
?>

is incorrect for Drupal 7 although it worked. 'visibility' should be:

<?php
function FEATURENAME_block_info() {
 
$blocks['my_block_id'] = array(
    ...
   
'visibility' => BLOCK_VISIBILITY_LISTED,
    ...
  );

  return
$blocks;
}
?>

So we're all still learning.

Cheers

Hi esod, Thats great! Thank

wipeout_dude's picture

Hi esod,

Thats great!
Thank you very much..

I have a question relating to this though..

Is the contents of FEATURENAME_block_info only read on the first execution?

I used your example and now I am trying to change some of the parameters in the code but they are not changing for the block.. I have tried clearing caches etc.. but its still not accepting the changes.. Do I need to do something else or does control of the block pass to the admin/structure/block screen after the initial load?

Thanks..

Hi wipeout_dude. The caching on this is tricky

esod's picture

To clear this cache in Drupal 6, you needed to go to admin/build/block and click Save after disabling your block module. In Drupal 7, the caching seems to be even trickier, although it's the same whether the block code is in a features module or in its own module. I tested this.

  1. I put the block code into its own module and didn't include any optional return values in hook_block_info.
  2. I enabled the module and manually placed the block into a Triptych first.
  3. I disabled the custom module and the block went away, obviously.
  4. I went to admin/structure/block and clicked Save.
  5. I cleared all the caches.
  6. I re-enabled the module and there it is again in Triptych first. Argh.

So it's not the features or the optional return values. Phew. Wouldn't want to have steered you wrong.

Okay, Let's find where Drupal 7 is storing block placement information and how to clear it.

An immediate workaround would be to rename things in the block code although that would only be a band-aid solution.

Is there a way to make this block editable?

madhaze's picture

Thanks btw. This was really helpful. Is there a way to make this block Body content editable in the block config.

Reinstall the module with devel's Reinstall modules

esod's picture

Reinstalling the module with devel's Reinstall modules fixes this. Then clear the caches on your site to make the block appear per optional return values in the code.

Ok.. My newbieness is about

wipeout_dude's picture

Ok.. My newbieness is about to show..

I have Devel installed but can't see any option to reinstall modules.. Where do I find it?

Thanks..

Two ways to get to the Reinstall modules page

esod's picture

You can go to the Reinstall modules page directly in the URL bar:

YOURSITENAME/devel/reinstall

Or place the block titled Development into an available region and click Reinstall modules.

That worked.. Thanks.. As you

wipeout_dude's picture

That worked.. Thanks..

As you say always learning.. Just some more than others.. :)

t

mail2jatingarg's picture

ttt

Jatin Garg
@ jatingarg1989

You can Also use context

ajay547's picture

You can Also use context module for exporting blocks in feature.

Drupal Dojo

Group organizers

Group events

Add to calendar

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week