0

In Drupal 7, I use the following code to print more than one view inside a block.

<?php print views_embed_view('view_name', 'display_name'); ?>

In Drupal 8, it doesn't work and it returns Array. Although the documentation for views_embed_view() says it's the same as in Drupal 7, it doesn't work in blocks using the PHP filter module.

2 Answers 2

2

First of all, it is bad practice to use the PHP Filter that comes with core in D7 and the contributed module for D8. PHP Filter was removed from D8 core to prevent new Drupal devs from falling in this bad practice trap. Unfortunately, some evil genius decided to make a contrib module for D8.

Have a read at What are the downsides of using PHP Filter code in blocks, nodes, views-args, etc? to learn why it's bad.


Now to answer your question:

  • You could simply place the View blocks by clicking the place block button in the Manage Blocks UI and perhaps utilizing Block Group if you have lots of blocks.

  • Another option is to Create a block via the UI and then print your View in a your block's template file.

    • You do this by coping /core/modules/block/templates/block.html.twig and renaming it to block--block--[block-id].html.twig which you then move & place it in your theme's /templates folder. See How to embed a view in a specific node?. for printing the block in the template file.
1
5

I can't help with the PHP filter and I don't think it is safe to use it in D8 by looking at the project's page.

There are better solutions. You could for example add a Views Reference Field to the block type or use Twig Tweak in a template. You can also try to embed the View in the WYSIWYG text area, which is a bit more work to set up, but then should give you all options you have now in D7. See Views entity embed.

Your Answer

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

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