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

Comments

cog.rusty’s picture

There is a module for that

http://drupal.org/project/insert_block

AndrewJarvis’s picture

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.

jeff h’s picture

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

norti’s picture

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

mariagwyn’s picture

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):
$block = module_invoke('views', 'block', 'view', Lectures);

print $block['subject'];

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

dmuir’s picture

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.

isaac77’s picture

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:

$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)

mitchepa’s picture

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

mariagwyn’s picture

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?

cflemm’s picture

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?

mariagwyn’s picture

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.

  $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

cflemm’s picture

Got it working, thanks!

markosef’s picture

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.

dyland’s picture

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:

print views_embed_view('VIEW_NAME', 'block_2');

hellomobe’s picture

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>';
		 }

mattys’s picture

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

emsearcy’s picture

I got this to work with webform, the difference was in the module_invoke arguments (2nd arg):

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

Oddly, the title didn't show up; just empty h2 tags. This didn't matter so much to me aside from the spacing, so I changed my theme's node.tpl.php to (addition is the "&& $title" on the first line):

<?php if (!$page && $title): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

Eric

dshumaker’s picture

Hi,
I was able to get my view to show up in a block via this call:

print views_embed_view('Myviewname', 'Myblock_1');

However now I'd like to use a view that has parameters, I mean arguements.

The view expects the node id from the url (Node:Nid) . I tried giving (passing) the view_embed_view function call the node id but that didn't do it.

Any suggestions on how to pass extra parameters to the views_embed_view function?

Many thanks,
-d

nevets’s picture

Pass the node id as the third argument to views_embed_view(), something like

views_embed_view('Myviewname', 'Myblock_1', $nid);

Details will depend on where the node id comes from.

dshumaker’s picture

yep, got it. I was plugging in some wrong numbers (node ids). Once I got that right, it's all good.
Thanks.

wOOge’s picture

To do this in Drupal 7 (D7):

print render(module_invoke('MODULE', 'block_view', 'DELTA'));

Where:
MODULE = the module — for webforms, its 'webform'
'block_view' = hook name like block_view, block_info
DELTA = id or delta of the block e.g 30, map-block_1

See also (read the comments for additional detail):
http://api.drupal.org/api/drupal/includes--module.inc/function/module_in...

--
wOOge | adrianjean.ca

ragavendra_bn’s picture

How to insert non view blocks for version 6. Like the tagadelic module of say 1. I tried the view function as above but couldn't make it work.

BigMike’s picture

@ragavendra_bn: Did you figure this out? I have the same question.

Thanks!
Mike