5

In D7, using the blockreference contrib module, you could add a field to a node (say) that allowed the user to reference a block. When viewing the page the referenced block's rendered content would be included.

This was great for including widgety stuff on a per-page basis, especially when combined with the Paragraphs module.

I have a View which defines a number of Block displays. I want to reference a particular one from a node and I'm struggling to do this in D8.

When creating the reference field, I'm offered various things under the "References" optgroup, but none include Block. I tried "References » Other..." which seemed to be the generic entity reference that I thought would work.

Next I'm asked what type of entity to reference, which is split between content and config. I suppose this is config since a view is config, even if its config that provides content (ouch my brain). Under Config I see block. Great! I want it to reference a block.

However, after selecting that I cannot seem to reference the block created by my view - switching the form display to select list to see what is available I see all sorts of stuff (some, like "primary admin actions" apparently repeated 4 times?!) but NOT the view's blocks.

The other option that looked relevant for "which entity to reference" is View. But if I do that I just get a link to the view's edit screen.

I've since found that if I place the block in the disabled section, then I am able to reference it.

So is there a better way to reference a block or do I have to do this odd thing of create a block » place as "disabled" » reference. Alternatively, is there an explanation why having to do things this really confusing way brings an important benefit to site building that I ought to understand?

Thanks in advance.

6 Answers 6

4

Block field actually cover the same functionnality that Drupal blockreference did. It works like "block field" in display suite, it reference block plugin and create block instances related to it host entity, not any theme.

1
  • 1
    Just a note for those searching for a similar answer - I'm using this inside a Paragraph type and it's looking good so far.
    – NikLP
    Commented Feb 9, 2017 at 23:14
4

Things are confusing because that's how entity reference works. All those things are entities, so they are technically referencable. As you found, block are actual block placements, which is not what you want. And view doesn't allow to select the display, so for your case, that's also not what you want. Drupal 8 core simply can't do that without custom code or a module (yet).

You either need a contrib module like Viewfield, but that doesn't have an official 8.x release yet. The blue box points to the contrib tracker issue, which in turn points to https://www.drupal.org/node/2220929, which has a link to a github repository where someone did an initial port. If and how well that is working already, I can't say.

An approach that would require a bit of custom code would be to have a list field with a few options ( since you don't want to allow all views anyway..), and based on that, there needs to be a bit of code that loads and calls the view and displays the results.

6
  • Thanks. FYI Referencing a block placement entity does enable me to put the rendered block on the page. Commented Apr 21, 2016 at 6:27
  • 1
    Right, but there are things that can go wrong. Access will respect block visibility conditions for example, so if you use a block and for example only show on the frontpage, it won't be displayed in your content. The disabled region is also a bit strange, there was an issue to remove that and instead have blocks enabled/disabled but always in a region. And last, blocks always belong to a theme. Which is a problem if you'd for example want to make a module with default config, it would depend on a specific theme.
    – Berdir
    Commented Apr 21, 2016 at 6:48
  • Do you mean block placement always belongs to a theme? A block is always generated by a module, surely, and then, yes, placed by theme logic and configuration. Seems D8's really shot itself in the foot with some of this stuff - argh! Massively losing faith! Thanks for your help. I'll go find counseling. Commented Apr 21, 2016 at 8:05
  • Modules provide block plugins. Those can be used in different ways. One are block placements by block.module, then it is theme specific. page manager uses them differently. you could reference the plugins directly with e.g. plugin module, but that's still somewhat work in progress.
    – Berdir
    Commented Apr 21, 2016 at 8:13
  • Viewfield is now available for Drupal 8 in an alpha version. Commented Mar 20, 2017 at 10:13
4

At my work we are testing Drupal 8 (currently 8.2.5) and had the same issue, but we are using the Paragraphs Module for page content layouts. We have found a solution that has been working so far.

First, you would need to install the Paragraphs module. Then you would install the Views Reference Field module. Once you create a "paragraph" you can add a field for a "view reference" and it will load the data for that view into the specified section of the "paragraph". Once we added the Paragraph field to the content type we wished to use it on, we are now able to load the data for any of our views into any part of the pages fields.

NOTE: we have not tested the Views Reference Field module on it's own, but it may be worth a try!

The documentation for each of these modules is pretty good and got us where we needed to be so I won't bother going over all the details of each here, you should read and play with them accordingly.

7
  • Hm, when I add the views reference field to a content type I can select the view and its block, but after saving the content it doesn't show the block. And when the field is inside a paragraph I can't even select the display ID. Did you have similar issues at the beginning?
    – 0711master
    Commented Jan 8, 2017 at 13:38
  • Ok, I can select the display ID after saving the node. But the view block is still not showing.
    – 0711master
    Commented Jan 8, 2017 at 14:01
  • I did not come across this issue. The steps I took were: Created paragraph type, Added new field, Selected "Views Reference" and created a label, Limited to one option, and then on the last step at the bottom I also selected "Block" under "View display plugins to allow". Then when I update my content type that I added the paragraph module to, it allows me to select any view I want, and it displays the content. When you go in and "Manage Display" for the paragraph field in your content type it should only give you the option for "Rendered Entity" as the format to display.
    – Ron
    Commented Jan 9, 2017 at 14:20
  • That's what I did too. In order to test if this is an issue with my view I added a block field (see answer of Dominique Fabre) to the same Paragraph type. Block field works, views reference not.
    – 0711master
    Commented Jan 10, 2017 at 15:21
  • Hmmm... well I am not sure then. If the view is working correctly and the field is set to view as Rendered Entity, then as far as we know from our testing it should be working. We now have done about 12-15 different views that we created and each one is displaying correctly when using Paragraphs. Maybe there is something else going on that the teams who built Paragraphs or the Views Reference Field modules that may be able to assist with? Possibly some other module conflicting? Sorry, not sure if I can help beyond this.
    – Ron
    Commented Jan 11, 2017 at 16:59
1

You can actually do this with the Block Visibility Groups Module. When you install the module, an item will appear in the admin bar, "Block Visibility Groups." When you are on the page where you wish to place the block, just click on it, and it will give you a number of options on how to limit the visibility of the block you want to place on the page. I am using it to display a views block with a contextual NID filter on a node type I have called Photo Galleries, and it pulls in the images from the image field and formats them into a Views Slideshow. I limited the visibility to the Photo Gallery content type.

Then, after creating the Visibility Group, go to the block layout page, and a drop down will appear just above the regions, and you can select your Visibility Group from there. When you place your block with that Visibility group selected, it will limit the display of the block in the region to the visibility group you designated.

Works very well, just requires a slightly different way of thinking about it over the field formatters.

1
  • Sounds like a fancy ui for Context. Thanks but it's not what I was after; I want to pull in a block or blocks within a node template, as the author pleases. Commented May 21, 2016 at 6:12
1

This is one of my favorite questions. Although this relies on Display Suite and I haven't tried the solution, I want to share it:

  1. Install Display Suite
  2. Use DS on any of your content items (/admin/structure/ds)
  3. "Add a block field" (/admin/structure/ds/fields/manage_block)
  4. Select the Views Block from the Dropdown list

Display Suite "Add block field"

You find the more detailed explanation by Ivan Zugec here: https://www.webwash.net/using-display-suite-drupal-8-how-to-use-display-suite-fields/

1

I added the view block using the Entity reference field type (choose other).

There is a great tutorial here: https://www.mediacurrent.com/blog/drupal-8-how-reference-views-block-display-field/

This is will work great on nodes but it's super powerful in conjunction with Paragraphs.

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.