6

[Drupal 7]

I have a block I created in Views called "handy_tips-block_1".

Now I want to insert it into the body field of my node.

How do I do this?

I found a guide on here, which suggests to use the following code:

<?php
print $block = module_invoke('views', 'block_view', 'handy_tips-block_1');
print render($block['content']['#content']);
?>

However, all what is printed out are the words "Array". (I have enabled the PHP input filter for the body field).

Note: in the posted code, I have also tried substituting 'views' with various items such as "block". I have used 'handy_tips-block_1' as the ID, because that is what is displayed on the configuration link of the block administration page.

4 Answers 4

15

Output is "array" because indeed is an array, then I guess that you want to print your array to see what fields you can use:

print_r($block);
print_r($block['content']['#content']);
print_r(render($block['content']['#content']);

About display a View, you can use this line of code inside your template, to call a view:

echo views_embed_view('view-name', $display_id = 'display-name');

for example, inside my node.tl.php I want to insert a view called test view and I want to use his Block display with machine name block_test, then I can use:

echo views_embed_view('test_view', $display_id = 'block_test');

(Remember to use the machine name of your view and machine name of your display).

I hope this information will be useful.

2
  • If i've used above method, then contextual links is not showing?
    – Guru
    Sep 14, 2015 at 5:00
  • print views_embed_view('my_view_name', 'block', 'student', 'one'); My arguments like student/one. Thanks for the solution. Used both(student, one) in contextual filter.
    – Kalidasan
    May 5, 2016 at 13:37
8

My favorite way to embed views in template files is with the views_embed_view() function.

It's really easy to use:

<?php print views_embed_view('view-name', 'display-name','arguments'); ?>
  • The 'display-name' argument corresponds to the type of display in your view, and the number of those types of there are multiple. So if in your view you created two blocks, you could call one or the other by using 'block-1' or 'block-2' in the 'display-name' argument.
  • The third argument, 'arguments', is optional, and can be used to pass any contextual filter arguments your view depends on.
2
  • Truly a great solution. Worked like a charm!
    – hockey2112
    Mar 28, 2013 at 2:55
  • Has anyone figured out how to render the block title with views_embed_view()?
    – jay-ess
    May 13, 2013 at 16:32
5

I recommend using either the Entity Views Attachment module, which allows you to insert a view into your module as if it's a field, or the Display Suite module which allows you to customize your node lay-out and insert all kinds of blocks into it.

3
  • Thanks for the suggestion. I only want to embed one view on page, so using a whole module seems like overkill. However, thanks for taking the time to reply.
    – big_smile
    Oct 16, 2011 at 10:08
  • You're welcome, thanks for the feedback. To be clear, I think there is nothing wrong with the other approach, I just wanted to mention the modules. Oct 16, 2011 at 18:08
  • Another option would be the blockreference module. Aug 29, 2015 at 1:28
1

I realise this is an old thread now, but I was looking for this too and found the BlockReference module. It allows you to associate a block with a node as a field.

It works a treat!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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