Previous recipes have shown how to create and manipulate a page created by a view. Views provides different display types that can be created, such as a block. In this recipe we will create a block powered by Views. The Views block will list all Tag taxonomy terms that have been added to the Article content type.
This recipe assumes you have installed the standard installation profile and have the default node content types available for use.
Tags
, which will serve as the administrative and (by default) displayed title.0
.In the Drupal 8 plugin system there is a concept called
Derivatives. Plugins are small pieces of swappable functionality within Drupal 8. Plugins and plugin development are covered in Chapter 7, Plug and Play with Plugins. A derivative allows a module to present multiple variations of a plugin dynamically. In the case of Views, it allows the module to provide variations of a ViewsBlock
plugin for each view that has a block display. Views implements the DrupalviewsPluginBlockViewsBlockViewsBlock
class, providing the base for the dynamic availability of these blocks. Each derived block is an instance of this class.
When Drupal initiates the block, Views passes the proper configuration required. The view is then executed and the display is rendered whenever the block is displayed.
We will explore some of the other ways in which Views interacts with blocks.
Pages and feeds have the ability to provide blocks, however not for the actual content displayed. If your view utilizes exposed filters you have the option to place the exposed form in a block. With this option enabled you may place the block anywhere on the page, even pages not for your view!
To enable the exposed filters as a block, you must first expand the Advanced section on the right side of the Views edit form. Click on the Exposed form in block option from the Advanced section. In the options modal that opens, select the Yes radio button and click Apply. You then have the ability to place the block from the Block layout form.
An example for using an exposed form in a block is for a search result view. You would add an exposed filter for keywords that control the search results. With the exposed filters in a block you can easily place it in your site's header. When an exposed filters
block is submitted, it will direct users to your view's display.