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
There is a module for that
http://drupal.org/project/insert_block
Just to be clear, it's not
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
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
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
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);
?>
<?phpprint $block['subject'];
?>
<?phpprint $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
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
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
Cool, that worked... thanks all ;-)
Now that I figured this out
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
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
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.
<?phpprint 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!
Got it working, thanks!
works!
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
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:
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:
Elixir Interactive
If the results are empty
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
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