Display blocks inside nodes
No PHP knowledge required.
Goal: I was looking to display the list of all books in a node (and more
generally trying to understand the design and logic of Drupal).
I'm using the book.module has an example, but this might also apply to all other
modules.
I came across this good article:
http://drupal.org/node/26502
The following is what I learned and extrapolated, from that article (read it first) and by digging a little further:
<?php
$block = module_invoke('module_name', 'block', 'Operation', delta);
print $block['content'];
?>
module_name: Is the module name.
Operation is usually: 'view'
If you actually look inside the module file (ModuleName.module inside the modules folder) for "function theModuleName_block"
(example: function book_block()) you might see "if/else" statements with
additional values. This digging might reveal additional, valid, display options
depending on the module.
The "delta" value is often "0" (zero), I think you can find the "delta" but
hovering with the mouse above the item. For example on the "administer-->blocks"
page, hovering above the "configure" link for the block "Book navigation"
reveals, the link "admin/block/configure/book/0" the 0 might be the delta. You can also look
at the MySQL table "blocks" and you will see a "delta" column with a value.
Additionally, looking at the code at "function theModuleName_block"
(example: book_block()) might reveal some
different behavior bases on this "delta" value.
Knowledge for further understanding Drupal: I'm extrapolating that the first and second parameters of module_invoke(),
are used the build the function call. Example: module_invoke('book', 'block',
Param1_String, Param2_Num); will call the function book_block(Param1, Param2)
inside the book.module file. This logic, probably, works with all modules and
all functions (pretty powerful function...might be calling module_invoke() a lot
in the near future....).
----------------------------Book.module particularity-----------
Looking at the function " function book_block" inside book.module, and trying
many calls to module_invoke() with different parameters, I realized that the
book_block() is propably built to only display data when on a book node (SQL is
expecting some arguments....). Since the node, I want to display a list of all my books on, is
not a book node that function will not work.
Looking a little furthe in the book.module file, I saw a function called "function book_render()". I added the the following code in a node (with "Input format" set to "PHP code") it displayed a list of the books:
<?php
return book_render();
?>
It worked!
Note: return book_render(); or print book_render(); seem to produce the same results...
I'm also deducting here, that you can use that technique to display any module
using the logic: ModuleName_render() ...will have to verify this later....


delicious
digg
reddit
google
technorati
Re: Display blocks inside nodes
Hi,
I added as you say above:
?php
$block = module_invoke('module_name', 'block', 'Operation', delta);
print $block['content'];
?
code, but it seems it says error parsed failed ?
Did i miss something ?
Thanks,
Andy Colleman