~/drupal/drupal-how-hide-view-block-content-empty.md

Drupal 8 - How to hide a view block if content is empty

1 min readApril 10, 2020by Gorav Singal#drupal#drupal-8
// TL;DR

Configure the view's 'No results behavior' or use the 'Hide block if the view output is empty' setting to prevent empty view blocks from rendering.

Drupal 8 - How to hide a view block if content is empty

Introduction

I have created a view, with some filters and content fields. I will show it under user profile. You might want to show under whatever path. The problem is when the content is empty, only title gets displayed.

Lets see how to tackle this issue

Hide the View block if content is empty

There are two ways to hide the block:

1. Hide complete view

  • In your view edit page, goto Advanced section.
  • See the last option Hide block if the view output is empty
  • Check it
  • Save the view

And, thats it.

2. Hide block, alternate

  • In block settings, Uncheck the box which says show title.
  • Goto view edit page
  • In middle col, there is a setting to add Header text
  • You can add an HTML there.
  • Put your title there, and you can use header tags
  • So, header becomes part of your content.
  • Save the view

Alternate Solutions, add custom text

  • Goto view edit page
  • See section: NO RESULTS BEHAVIOR
  • You can an Unfiltered Text, and show some message. Example: Content is not available.

See our old post, If you want to show your view only to owner user only

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Drupal - How to rename column of a content type

Drupal - How to rename column of a content type

Introduction You already have a content type with one or more fields in it…

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement I’ve been using image styles, and heavily used “Scale and crop…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…