3

In drupal 7, I want to render block into another block, for example block-1 into block-2. I guess I should put some PHP code into block-2, but I don't know what the code could be?

  • Putting "some PHP code into block" is a very bad idea. Please elaborate on what you want to accomplish, as this doesn't make sense without more context. – Letharion Apr 15 '12 at 8:40
  • Are looking to multiple, related pieces of content together like a mini-panel? (drupal.stackexchange.com/questions/28266) – Ashlar Apr 15 '12 at 14:54
  • 1
    like <?php print render(block_load($module, $delta)); ?>? – artfulrobot Apr 15 '12 at 19:56
  • @Letharion, "newsletter" module created "newsletter" block which I want to put that into some reserved place in other block – Sadegh Apr 16 '12 at 8:00
  • @Ashlar, Thanks for your response, I don't know what mini panel is, but I'm trying to figure that out :) – Sadegh Apr 16 '12 at 8:05
3

Based on Ashlars's comment.

A minipanel, provided by Panels, is for anyone not used to Panels, essentially a block, that supports internal layouts. You can use a mini-panel to wrap several different blocks (and many other things, views, nodes, etc) into a single "block".

Unfortunately, I must admit that documentation is scarce on this topic. I will refer you to this other question: looking for a good mini panels tutorial.

3

Completely agree with Letharion's comment but if there is absolute need for such, this code snippet can help:

<?php
//D7
$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);
?>

If you are unsure what are exact parameters for 'module_name' and 'block_delta' I'd recommend searching the database table block and locating your specific

  • module_name under column module
  • block_delta under column delta

second parameter block_view remains exactly like that.

Reference: https://www.drupal.org/node/26502

  • Good solution, however, it might not a best practice, especially if the client has write access to the block (which they probably would, because that's what a CMS is for). The client breaks any PHP code, and the entire site goes down. – dayuloli Feb 26 '15 at 5:01
  • Beyond that issue, this sometimes is the sharpest way to go. To invoke a block, you'd use this code, for example: <?php $block = module_invoke('block', 'block_view', BLOCK_ID_PASTE_HERE); – Meetai.com Apr 16 '15 at 3:14
  • It was good to point out that find out the 'module_name' and 'block_delta' by searching DB, useful trick! – hailong May 11 '18 at 3:46
0

Late answer for anyone who comes across this.

The cfrblock module, based on renderkit, allows to combine blocks and elements in any way you want, also supports conditional logic.

I am not sure if this covers the specific requirement of the original question: The original question wants to insert block-1 into block-2. What I am proposing here allows to create a new block which combines two existing blocks.

Follow the instructions on the module page:

  • Install cfrblock, renderkit, cfrpreset. This involves some composer packages, you will find instructions on renderkit module page.
  • Go to admin/structure/cfrpreset, add a preset for "BlockInterface".
  • Configure the block as you like.
  • Go to admin/structure/block. The presets you just created will appear there as blocks.

Possibilities when configuring the block:

  • Sequence of elements (blocks or other things). This is, I think, what you want here.
  • Views display.
  • Views exposed form.
  • Existing block.
  • Element wrapped into a special container.
  • Combination of the above, with endless nesting.

If you are crafty and creative, you can create your own elements and then recombine them into a block.

I am using this on a website where I have more custom plugins than you currently find in renderkit. I should add some of those to the contrib module..

NOTE: This is essentially the same answer I gave here, https://drupal.stackexchange.com/a/288747/2974. It applies in both cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.