Insert Block into Content

mitchepa - July 23, 2007 - 13:17

Hey all, just another quick question... should be simple enough

Does anyone know how to insert a block directly into content? I looked at the php snippets section but under the snippets section for 5.0 there were only two snippets (both had nothing to do with this). I also tried using the standard php snippet that you would use to call a block in the template but that doesn't work either... what am i missing here? (If it helps I'm using the frontpage module and have it set to allow php).

Thanks everyone,
Paul

There is a module for

cog.rusty - August 12, 2009 - 11:20

There is a module for that

http://drupal.org/project/insert_block

Just to be clear, it's not

AndrewJarvis - July 23, 2007 - 14:00

Just to be clear, it's not an option for you to just change in the theme where the right (or left) sidebar shows up? Possibly just putting the call to the right sidebar below the main content area could solve your problem. That is, unless you need some of the content of the page above the block and some of it below it, in which case, yes, you'll have to use the module that was just posted.

try this

jeff h - July 23, 2007 - 14:35

I think this may be what you're after -- you can put it straight into a node body (with PHP input filter):

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

Make sure to change the 'view' and the 25 to whatever correctly identifies the block you want to see. An easy way to find out this info is to go to your block admin page and point at the configure link for the block you want to use.

Jeff

Thank you for that code. But

norti - July 30, 2007 - 22:43

Thank you for that code. But how can I give an argument to a View (printed as a block) ???

I am not sure this will help

mariagwyn - August 11, 2007 - 01:04

I am not sure this will help you in creating a view, but what I did was create a set of views as blocks which output what I want. Then, I created a node, into which I put the following PHP code (thanks to the hint from above):

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

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

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

I believe this 1) invokes the views module, 2)tells it to display a block, 3)I have no idea what the second 'view' means, and 4) picks the one I have called 'Lectures.'

Hope this helps a little.
Maria

Might be wrong, but I think

dmuir - June 11, 2008 - 06:25

Might be wrong, but I think the second 'view' refers to the action. As in you're going to view the block, not edit the block.

module_invoke arguments

isaac77 - February 25, 2009 - 15:39

The arguments to module_invoke are explained at http://open.madcap.nl/fotd/module_invoke

To print a block called "foo" that is supplied by a module named "bar," I _think_ you would do this:

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

(This is for Drupal 5.x -- not sure if anything's changed in 6.x)

Cool, that worked... thanks

mitchepa - July 27, 2007 - 19:46

Cool, that worked... thanks all ;-)

Now that I figured this out

mariagwyn - July 18, 2008 - 00:20

Now that I figured this out in drupal 5.x, I am having problems in 6.x, specifically, with views. So, here is the situation:

If I do this code:

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

I see the random image block, works fine. However, if I do the following:

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

Nothing shows up. I have tried every variation on the name of the block that might work, to no avail. Has something changed in Views regarding how you invoke blocks? Any ideas?

Same problem here, no matter

cflemm - August 8, 2008 - 18:05

Same problem here, no matter what I do cannot get anything to show up.

I am using Drupal 6.3 and Views 2. Has anybody been able to make this work?

I did figure this out with

mariagwyn - August 9, 2008 - 01:20

I did figure this out with Merlinofchaos' generous help, you can see the train here: http://drupal.org/node/263554

However, here is a summary:

This code embeds the view block as is. The first variable is the NAME of my view. the second is the NAME of the block. Still not quite sure how to figure out the name since it is actually called reflections_block, but views seems to give generic numbers.

<?php
 
print views_embed_view('archive_reflections', 'block_1');
?>

The code below embeds the view as well as the Title of the block. The code above only embeds the block itself, the title is a separate function.

<?php
  $view
= views_get_view('archive_reflections');
 
$view->set_display('block_1');
 
$output = $view->preview();
 
$title = $view->get_title();
  print
'<div class="title">' . $title . '</div>';
  print
$output;
?>


Be sure to use the PHP input filter on BOTH of these or nothing happens.
Hope that helps.

Maria

Got it working, thanks!

cflemm - August 18, 2008 - 16:35

Got it working, thanks!

works!

markosef - December 20, 2008 - 17:42

This works. I dont know why is it, but snippets like this ara always more flexible then actual drupal theming when u do visualy complex designed sites.

Working out the name of the block

dyland - July 29, 2009 - 18:18

This code embeds the view block as is. The first variable is the NAME of my view. the second is the NAME of the block. Still not quite sure how to figure out the name since it is actually called reflections_block, but views seems to give generic numbers.

To figure out the names of the fields to use click on the 'edit block' link from the block admin menu (/admin/build/block) and the URL will look something like:

http://www.sitename.com/admin/build/block/configure/views/VIEW_NAME-block_2

The bit at the end 'VIEW_NAME-block_2' are the parameters to the call - split them in the middle at the minus ('-') sign. Your end code would look like:

<?php
print views_embed_view('VIEW_NAME', 'block_2');
?>

If the results are empty

hellomobe - October 30, 2009 - 20:34

If the results are empty (none), what is the code to hide the title of the block? Thanks

Update: Got this to work http://drupal.org/node/446798

// At least in $view->result is the result.
if ($view->result){
$title = $view->get_title();
print '<div class="title">' . $title . '</div>';
}

Code snippet not working for webform block

mattys - August 12, 2009 - 10:37

Hi

I have an issue that I have tried to sort myself, but keep hitting the brick wall, can't go any further.

I have created a webform called test. I have the module webform block so form can be displayed in a block, so, when you create a webform it also creates a block in blocks section.

I used one the the code snippets in this section, see below, but changed so sources my block. (block is not enabled)

<?php $block = module_invoke('webformblock', 'view', 10); ?>
            <h2 class="title"><?php print $block['subject']; ?></h2>
            <?php print $block['content']; ?>

The code when inserted into node does display the block, see here, under shop sub heading http://www.coldharbourmill.org.uk/site/node/8, but, has subject of webform node, e.g. Test (the block Title/Subject should be Enquiry Form). Incidently, when i enable the actual webform block, to test, and display in closure block, it displays fine.

Ok, everything fine, apart from I would like to use collapsiblock module to be able to open and close the block, is quite a lengthy form, there will be a few for each sub heading, so would look messy. However, I think, coz the incorrect subject is being displayed collapsiblock is not working for this block, as you can see, even though it has been enabled, and works in actual block when diaplyed in closure section.

Can anyone suggest how the code snippet can be changed to fix this?

Thanks you

Matt

 
 

Drupal is a registered trademark of Dries Buytaert.