Views Tips And Tricks

Hit Escape

You can hit the escape key to exit modal windows without making changes.

Disable frontpage view

When you install Drupal, the home page will have a view that lists content that is promoted to the front page.

It has the url /node, which is set as the home page in Configuration > System > Basic site settings.

Disable the page, or else all your content will be visible at /node.

Published Status Or Admin User

This views filter will show unpublished content to admins, but hide it from other users.

Image
Drupal views published filters

Twig in Views

Group by content type, add s for plural, with check for specific types that need something else.

{% set type = type == 'Class' ? type ~ 'es' : type ~ 's' %}
<h2>{{ type }}</h2>

Hide empty blocks

If you create a block with views, you probably want to hide the title if there are no results.

You can find this setting under advanced settings in the view.

Image
Drupal hide block if view output empty

Rearrange fields/filters/etc

Click the dropdown arrow next to Add to rearrange fields, filters, sorts, etc.

And/Or logic

In the rearrange filters screen, you can configure and/or logic groups.

Advanced pane default

Views have an Advanced pane that is collapsed by default.

You can expose it by default in the Views settings.

Structure > Views | Click on Settings tab

 Views Settings

Image
Views settings - Always show advanced display settings

Find content with custom layout

You may have a content type configured with a default layout, but allow items to be overridden.

Later, you want to adjust all layouts, but need to find the existing content to adjust.

To find the content with a custom layout, create a view of the content type with a filter on Layout to Is not empty. This will match items that have an overridden layout.

You can use views in many ways to find the content you're looking for.

Sort view by last updated

Views are sorted by when the content was created by default. It may be more useful to change this to sort by last updated.

  1. Edit the view.
  2. Remove Content: Authored on (desc) under Sort criteria.
  3. Add Content: Changed (desc) sort.
Tags
Views
Modules