Is there a simple way to embed a block into content?

I've found ways using regions and some other ways but the blocks created do not div tags are not exact to those in the side bars.

Is there anyway you could use something as simple as:

<?php
block
=(block ID);
?>
I don't know PHP but to custom embed a ad banner is similar.

Comments

I remember there was a module that said it did exactly that: put blocks into content. I don't remember the name exactly ... it was something like BlockPage or BlockNode or NodeBolock or something like that.

If you come up with an easy solution that works optimally please post it back here, so that others will benefit as well.

I remember there was a module that said it did exactly that: put blocks into content. I don't remember the name exactly ... it was something like BlockPage or BlockNode or NodeBolock or something like that.

If you come up with an easy solution please post it back here, so that others will benefit as well.

I haven't looked at the modules list yet but I did find one called Insert_block that can do the job will let you know once I've tried it.

$block = module_invoke('nice_menus', 'block', 'view', 1);
print $block['content'];

Michelle

--------------------------------------
My site: http://shellmultimedia.com

I found a similar snippet before I started this threat. What I wanted was something that created all the css tags like the default black design.

I Found this one but I haven't tried it yet:

<?php $block = module_invoke('block', 'block', 'view', 25); ?>
<h2><?php print $block['subject']; ?></h2>
<?php print $block['content']; ?>

I think it would work but still missing the div and class tags, right?

What are the module_invoke parameters? http://api.drupal.org/?q=api/function/module_invoke/HEAD just explains the first two.

Try something like print theme('block',$block);

Senior Drupal Web Developer
Wellcome Trust Sanger Institute

Example is the Guestbook

<?php
 
if(!$user->guestbook_status){  // guestbook_status seems to store 0 for enabled and 1 for disabled
    
print module_invoke('guestbook', 'page', $user->uid);
  }
?>

I think I want to place this in a block, I will let you know if it works or it wrecks :)

________________________________________________________________________________
Internet for ISLAM, get to know Islam and Muslims :) May Allah brings you to the Straight Path
http://muslimin.org/Islam
----------------------------

Make sure you do a "global $user" before that code or it won't know what $user is.

Michelle

--------------------------------------
Drupal articles and tutorials: http://shellmultimedia.com
Current project: http://couleeregiononline.com/

If you don't want to work through creating new regions a module that may help you is the Panels module.
It allows a nice ui to drop your blocks and views into the main content region of the site. The only catch is that if you need a unique panel layout you will need to modify or add new panel output templates. At any rate its worth a look.
Hope this helps.
JK

Panels Module: http://drupal.org/project/panels

Looked at it and it's not the best solution for what I want. I think this might be handy for me in the future. It's would certainly be great for a news oriented website.

I think some module specific templates for this module would be great.

Nick Lewis has an excellent post on how to create block regions for node.tpl.php

http://www.nicklewis.org/node/846

Although it is for Drupal 4.7.x, you can easily modify for Drupal 5.x
For example, if you have a custom content type, you can pass the block region into your custom content type (e.g., node-customcontenttype.tpl.php) instead of the all encompassing node.tpl.php

My current design has that but when you want to do a lot of custom pages I don't think this is the best way to go.

It leads to a lot of PHP code and .css in single files.

I think anyone who is interested in using regions should take a look at the Panels.module is MAY be a better solution for their needs.

Thanks to jwolf for the nice reference link.

I believe that the most Drupal way to go about assigning blocks to specific positions within content is to create as few unique regions as you will need in your template.php file (http://drupal.org/node/29139). Then you can output these regions wherever you want in unique tpl.php files. Although I could be incorrect about this.
It probably takes a little more php work than you want to use, but in this Drupal world its hard to avoid php and I believe that you will inevitably be pulled into the code...lol.

This being said... if you do define say... 1 region which you want to place within your content. You can re-use this region in multiple tpl.php files and in different places or assign the position via a var in one tpl.php file. This way you don't end up defining numerous regions which you are only using once on different pages.
This is important, because Drupal will process all of your specified regions regardless if they are being output to the browser and this can bump up your overhead if you have a lot of them.

When it comes to styles you can assign unique class id's or import a new style sheet to display your unique page how you want using drupal_add_css() function: http://api.drupal.org/api/function/drupal_add_css/5

I hope this helps,
JK

Gonna make a post about Insert_block.module and see it anyone responds. I tried contacting the maintainer and someone who apparently got it to work.

regions can work. I know this because I'm already using it. But I think it's not elegant when you have many .tpl files.

if you have 25 template files for various nodes e.g. photos/weblinks/news/and so on and all show different block on different parts of the page it equals to a lot more coding than I would like.

If I can get simple block fixed I will have no choice but to use regions since I thinks its the best of the rest.

I know this post is old but anyways has anyone heard anything more about the insert_block module, is it possible to get it to work? Placing blocks within the content is such a key thing for web advertising I'm surprised more people aren't interested in this as this is the only post I could find about this topic other than the tutorials to embed additional regions.

So after some serious searching I finally realized that there is a module for this, the advertisement module, I hate to see posts like this left unsolved so for future reference: http://drupal.org/project/ad this should do everything related to what the people in this thread were discussing.

Yes ofcourse there is a simpler way to embed a block into content :

please use the below code :

<?php
$block
= module_invoke('block', 'block' , 'view', 25);
?>

<?php
print $block['subject'];
?>

<?php
print $block['content'];
?>

Where 2nd 'block' is the module name
and replace 25 with block i.d.

Drupalgeek
Ebizon technologies ( http://ebizontek.com/)
India

Great thread! But how would one embed a block that is produced by the Views module?

I have something along the same lines as you except in my site I would script

<?php
$block
= module_invoke('block', 'block' , 'view', 25);
?>

Where 1st 'block' is the module name
and replace 25 with block i.d.

Robert D. Smith

I don't think that would matter as long as the array saw the perimeters 'block' , 'view' in there somewhere, could one define the array $block as so....

$block = module_invoke('modulename', 'block' , 'view', block#);
as well as
$block = module_invoke('block' , 'modulename', 'view', block#);

"Code testing will find all the possible errors that you can think of, data testing will find all the errors you don’t. "
--Bioinformatics Zen

After running across this page and attempting to implement it, I could not for the life of me get a D6 install it to work with:

<?php
$block
= module_invoke('block', 'modulename' , 'view', 25);
?>

It wasn't until I put the modulename first that it began working for me. Like so:

<?php
$block
= module_invoke('modulename', 'block' , 'view', 25);
?>

<?php
$block
= module_invoke('block', 'views' , 'view', 25);
?>

to embedd block from views

drupalgeek
Ebizon technologies (http://ebizontek.com)
India
Recent Projects: (kickwork, Znips)

Thanks! But where do you get the id number from?

well, to get the block id. please follow the steps below :

firstly, enable ur block in any region .

secondly , copy the code frm programming , pls see the link

http://www.programmingbulls.com/drupal-important-debugging-php-snippet-w...

thirdly , go to this link n copy the code in ur block.tpl n replace $ form with $ block

Lastly it will display all the blocks in the array form
You will get the block id frm the array.

Drupal geek
Ebizon technologies (http://ebizontek.com)
India
Recent Projects: (kickwork, Znips)

please tell me theres an easier to find a block id number? Is it in a file or database? Where is it generated from?

I'd call this an easy way to find the block name and id...

Blocks are uniquely identified by the combination of "module name", "block id" (relative to module), and "theme name". There is not singular block id like there are for nodes/menus/etc....

If you go to Administer/Blocks and hover over the "configure" link for the block you want, you will see the "module name" and "block id" at the end of the URL.

details at this post: http://drupal.org/node/74384

In Drupal 6.5, for me it worked in the following way:

<?php $block = module_invoke('panels_mini' ,'block', 'view', 6); ?>
<?php print $block['subject']; ?>
<?php print $block['content']; ?>

Notice that the name of the module (depends on your needs, but in this example) 'panel_mini' comes first

Perhaps to help some others looking for the answer to how to embed a view block into a node, this works.

$block = module_invoke('views', 'block' , 'view', 'accordion_menu-block_1');
print $block['content'];

A block-view inserted into a node. "accordion_menu-block_1" is what is shown on hover over the config item.

Joel Box - Mondial-IT

Does anyone know if there is a way to pass one or more arguments to a view using this method? Just tacking it on does not seem to work.

What I want to work:

<?php
$block
= module_invoke('views','block','view','MyViewName-block_1',$node->uid);
echo
$block['content'];
?>

...but it does not.

TIA!

Found the function views_embed_view($name, $display_id)

<?php
$items
= views_embed_view('MyViewName', 'default', $node->uid);
echo
$items;
?>

Extra arguments get passed to the view.

I hope this helps someone else :-)

That helped me out. Thanks for posting.
This got a problem I had not being able to embed a region into a node.tpl file in Drupal 7.
A problem I've been bashing my head against for several hourz I might add :)

WordPress for Business
http://living-websites-2.eu

Thanks!

I'm trying to get this module_invoke to work with a user created menu. I've tried everything and can't seem to get it to work. This is the code that I'm using.

<?php
$block
=module_invoke('menu','block','view',menu-name);
print
$block['content'];
?>

menu-name is whatever the name of the menu is. It seems as though in Drupal 6 the menus aren't given ID's but names instead. Please let me know if you have a solution.

you are almost there - atleast as per what is working for me
in my case the block module is 'menu' and the block name is 'menu-resources'
hope you are putting everything in single quotes like my working example below


<?php
$block
= module_invoke('menu', 'block', 'view', 'menu-resources');
print
$block['content'];
?>

see this live at
http://geniekids.com/resources
all the links you see in the node are from the BLOCK resources which incidentally is also visible in the right-bar

That was what I needed, geniekids. (I was missing the quotes around the block ID.) Thanks!!!

I use this code and works reallly fine... If I want to add a block to my template I have two ways, first directly all the code:

<?php
$module
= 'user';
$delta = 0;
$block = (object) module_invoke($module, 'block', 'view', $delta);
$block->module = $module;
$block->delta = $delta;
print
theme('block', $block);
?>

If I want to add a block without subject or with a css then I use:

<div class="block block-gtranslate" id="block-gtranslate-0">
<?php
$block
=module_invoke('gtranslate','block','view',0);
print
$block['content'];
?>

</div>

If I want to place a menu I use:

<div class="block block-user" id="block-user-1">
    <h2 class="title"><?php print $user->name;?></h2>
<?php
$block
=module_invoke('menu','block','view','navigation');
print
$block['content'];
?>

  </div>

in this example I'm extracting the name of the user... works cool.

And if I want to open a View block and show it, I use:

<?php
$block
= module_invoke('views', 'block' , 'view', 'carrusel_de_productos-block_1');
print
$block['content'];
?>

this works fine, but no matter what I use, I have 2 problems:

1.- First of all, I need to place all the blocks in a region, the region will not show anything I place there, only works to load the css in his place because If I don't make this the css will not load and see ugly... Do you have another solution???

2.- This method make that I show any of this in all the site, but how can I specify that will show only in the < front > page or in the node/15 or something like this???

Do you have any idea???

Regards
Bruno Chávez

to have proper div tags, try this:

//from http://drupal.org/node/51900
$module = 'frontnews';
$delta = 0;
$block = (object) module_invoke($module, 'block', 'view', $delta);
$block->module = $module;
$block->delta = $delta;
echo theme('block', $block);

subscribing

Luis

subscribe

The block content comes with inner div tags, we just need to add the outer ones. Consider closing (and restarting) your node's content div to keep your css selectors working..

This example shows a block created with views, on drupal 6.7-6.9 or so...

<?php
             
print '<div class="block block-views" id="block-views-explore_menu-block_1">
<div class="blockwrap">'
;
             
$block = module_invoke('views','block', 'view','explore_menu-block_1');
              print
'<h2>'.$block['subject'].'</h2>';
              print
$block['content'];
              print
'</div></div>';
?>

I found Velitas' version of the code the easiest in this thread to reproduce block standard behaviour via PHP in Drupal 6.
Be aware that it bypasses any "title" set in block config, i. e. "".

Hello
I am new to Drupal and would appreciate any assistance I could get. I tried searching for the answer but couldn't find it. I want to know how you would make a block visible to specific roles when embedding the block into content. I specified what roles could see the block when creating the block, but when I embed the block into a content page the block appears to all roles and not just the ones I specified in the block. I used the following snipplet.

<?php
$block
= module_invoke('block' ,'block', 'view', 2);
?>

<?php
print $block['subject'];
?>

<?php
print $block['content'];
?>

Any help is appreciated. Thx

I think you can load the user and then make something if the user belongs to some role..

global $user;
if ($user->role[4]) {
  // do something
}

Luis

Thanks for the fast response. I played with it a bit and used

<?php
global $user;
if(
is_array($user->roles) && in_array('anonymous user', $user->roles))
{
$block = module_invoke('block' ,'block', 'view', 2);
print
$block['subject'];
print
$block['content']; }
?>

and it works.

I am using module_invoke to display a block in a page. This is a multilingual site and I would like to automatically display the correct translation of the block. Is there a way to accomplish this?

subscribed

I'm a complete newbie to php, so forgive me. But, using the embed code here, is there a way to control the height of the block?

You can use css. Use one of the good snippets above to put a div around the block with a class or id.

<?php
             
print '<div class="block block-views inner-content-block-1" >
              $block = module_invoke('
views','block', 'view','explore_menu-block_1');
              print '
<h2>'.$block['subject'].'</h2>';
              print $block['
content'];
              print '
</div>';
?>

Then in a css file give it a height or min-height.

.inner-content-block-1 {
min-height: 100px;
}

This is riddled with typos so I wanted to repost for anyone that cares.

<?php
  
print '<div class="block block-views inner-content-block-1">';
  
$block = module_invoke('views', 'block', 'view', 'explore_menu-block_1');
   print
'<h2>' . $block['subject'] . '</h2>';
   print
$block['content'];
   print
'</div>';
?>

Exactly what I needed.

To embed a Views2 block anywhere, you can extend this by passing the arguments for the view.

Case study:

I have a view that contains the header content for a particular now. It has images, a title, and a fivestar widget. I call the view "show_header." To make show_header unique to a node (i.e. have a unique title, fivestar widget, and images, I pass that view one argument, the node id (nid).

In this example, the $arg is node #75, or just 75 in Drupal speak.

So, to pass that nid to module_invoke as the 5th argument:

<?php
TEMPLATENAME_proprocess_node
(&$vars, $hook) {
 
$vars['show_header'] = module_invoke('views', 'block', 'view', 'show_header-block_1', 75);
}
?>

Now, in my node.tpl.php I have an array called $show_header with two values, content and subject. To print them out, do this in node.tpl.php:

<?php
print $show_header['subject']; // <-- the block's subject from admin/build/block...
print $show_header['content']; // <-- the rows of the view from views
?>

Note that there are FIVE (5) args sent to module_invoke. It works.

Joe Hyde
http://www.hydeinteractive.com/

To get a block output closer to the real thing, you should cast the module_invoke call as an object and pass it through the theming layer. This is very close to normal block output (with region counts, etc.) except that the block delta is not set and the block id comes out as id="block--" but other that that I believe it's complete and better than just outputting the content and subject manually.

<?php
$block
= (object) module_invoke('views', 'block', 'view', 'community_best-block_1'); // $module, $hook, $args...
print theme('block', $block);
?>

If you want to have the block delta set properly you can accomplish it totally in the theming layer (no hacks) with template.php and block.tpl.php. Read more: http://drupal.org/node/753516#comment-2769068

Here's the block address from the configure link (all after the domain name):
?q=admin/build/block/configure/aggregator/category-4

Here's the code I've tried in the body of a Story node:

<?php
$block
= module_invoke('block', 'block', 'view', category-4);
print
$block['content'];
?>

Only the straight php code shows and not the block content in the Story body. I'm using Drupal 6. Please tell me what I need to tweak.

Thanks,
Mark

Mark Caldwell Walker
Contact info at marwalk.net/About
GnuPG public key AA482E46

None of these methods work for D7 anymore. I found the best way is to create a custom region for node.tpl and do something like

<?php
print render(block_get_blocks_by_region('region_containing_block'));
?>

Or use hook_page_alter(&$page) - but currently I have no idea how to use it. not much documentation.

In Drupal 7 the 2 middle arguments that never changed: block and view "merged" into block_view.

Drupal 6:
$block = module_invoke('block', 'block', 'view', 'block_name');

Drupal 7:
$block = module_invoke('block', 'block_view', 'block_name');

Trying to get my menublock but it's not really working out:

<?php $block = module_invoke('block', 'block_view', 1); ?>
<?php print $block['content']; ?>

Neither does

<?php $block = module_invoke('menu_block', 'block_view', 1); ?>

Any input?

If the module is menu then put menu instead of menu_block.
The first argument is the module generating the block and the third is the name of the block.

I think the module is system, not menu, at least that's my conclusion from the html markup D7 returns in a block.

You could also try this.

                $block->module = "system";
$block->delta = "some-delta";
$block->region = NULL;
$block->title = t("Some Title");
$block_content = _block_render_blocks(array($block));
$build = _block_get_renderable_array($block_content);
$ret = drupal_render($build);

I took that code from api.drupal.org but I just don't remember exactly from where.

Here is another interesting comment http://api.drupal.org/api/drupal/includes--module.inc/function/module_in...

Luis

I implemented the following:

<?php
    $block
= module_invoke('menu', 'block', 'view', 'menu-product-category');
    print
'<h2 class="title">Online <br/>Shop</h2>';
    print
$block['content'];
?>

And straight away my server load hit the roof - is their any known performance issue with module_invoke on a D6 site?

I didn't read through the whole thread so I apologize if this is redundant. But it takes slightly different syntax for D6 or D7 to hardcode a block with PHP in content or anywhere for that matter.

D6:

<?php
$block
= module_invoke('search', 'block', 'view', 'form');
print
$block['content'];
?>

D7:

<?php
$block
= module_invoke('search', 'block_view', 'form');
print
render($block);
?>

This prints the search form anywhere, simply change the "search" to any MODULENAME and change "form" to any BLOCK_ID. you can find these at the end of the URL while you are in Structure ---> Blocks ---> Configure (desired block to print).

The end will look like this in D6: modulename/block_id
In D7: modulename/block_id/configure (just ignore the configure on the end)

-Schnelle

www.thebrewpal.com

Subscribe. I will try it. Thanks.

Is possible to keep the visibility properties from the block??
I am using Drupal 6 with this code in the content template:

<?php $block = module_invoke('xxx', 'block', 'view', '0');
print
$block['content'];
?>

my result is that all the content that is created have this block, but the block should be only able in those pages that I allowed on the block's configuration.

How can I do that?
Thanks in advance.