views:

117

answers:

2

I want to change content inside $block->content, which drupal hook i should to implement?

+1  A: 

AFAIK, there is no 'real' hook to change a blocks content. You could implement a yourModule_preprocess_block() function, checking/manipulating the passed in $variables array. Alternatively, as block implementations are usually pretty short, you could copy the original block generation logic to your own hook_block() implementation, thus providing your own version with your desired customizations.

Henrik Opel
+1  A: 

Depending on what your needs are, I can think of various possible ways:

  1. Using template_preproces_block(): this hook essentially "intercepts" the variables on their way to the block and gives you a chance to intervene on those. This is quite an efficient way of doing it.
  2. Creating your own template file for the block: This allows you for a few more tricks, but in terms of speed is not the best (theming via .tpl.php files is about 5x slower than using functions, I read)
  3. Creating your own brand new and shiny block by implementing hook_block(). As suggested by Henrik, you could easily go by coping the code from the original module as a starter. This is a good way to do it if your change must affect all possible themes in use on the site, not so much if it only have to affect one theme (although you might implement some logic based on weather a given theme is in use or not, but this seems to me highly inelegant).
  4. If the block display forms or other content that has its own set of hooks, than you could also change the content that way. For example: if you are trying to modify the login block (which shows a form to authenticate) you could implement either hook_form_FORM_ID_alter() or hook_form_alter().
  5. Using jQuery: this is a good solution if the content of the block needs to change asynchronously (or dependently) from the rest of the content of the page. Keep in mind that legacy browsers (or search engines) won't trigger the jQuery code though.

On a different note, you could totally change the approach to the problem by changing the very nature of the "block" into something that is more flexible. I would tend to discourage this approach unless you really find out that a simple block won't do what you need to... yet it is a possibility you should know about. You can change the nature of a block, amongst others by:

  • Using the nodeasblock module, which allows you to get a node into a block (thus being able to use in the template many more variables than the simple $title and $content ones.
  • Using the views module. This is a must-know for any serious drupal user/admin... but the applicability of views to this specific case varies a lot depending on what is the content of the block you are trying to produce.
  • Using the panels module, which allows you to create block-like content and play with it through a graphical interface (bare in mind this is a huge module in terms of overhead and has a steep learning curve).

HTH, Mac.

mac

related questions