Creating a block from a View

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.

Getting ready

This recipe assumes you have installed the standard installation profile and have the default node content types available for use.

How to do it…

  1. Visit Structure and then Views. This will bring you to the administrative overview of all the views that have been created.
  2. Click on Add new view to begin creating a new view.
  3. The first step is to provide the View name of Tags, which will serve as the administrative and (by default) displayed title.
  4. Next, we modify the View settings. We want to display Taxonomy terms of the type Tags. This will make the view default to only displaying taxonomy terms created under the Tags vocabulary
  5. Check the Create a block checkbox.
  6. Choose the HTML List option from the Display format choices. Leave the row style as Fields.
    How to do it…
  7. We want to display all of the available tags. To change this, click the current pager style link. Pick the Display all items radio and click Apply (all displays). On the next model, click Save to keep the offset at 0.
  8. Next we will sort the view by tag name instead of order of creation. Click Add on the Sort criteria section. Add Taxonomy term: Name and click Apply (all displays) to use the default sort by ascending.
    How to do it…
  9. Press Save to save the view.
  10. Visit Structure and Block layout to place the block on your Drupal site. Press Place block for the Sidebar region in the Bartik theme.
  11. Filter the list by typing your view's name. Press Place block to add your view's block to the block layout.
  12. Finally click on block to commit your changes!

How it works…

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.

There's more…

We will explore some of the other ways in which Views interacts with blocks.

Exposed forms as 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!

Exposed forms as blocks

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.

See also

  • Chapter 7, Plug and Play with Plugins, to learn more about derivatives
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset