Learn how to use the Block Exclude Pages module to exclude paths added into the visibility settings.
We’ll cover the following:
- How to download and install the module
- How to exclude paths
composer require drupal/block_exclude_pages
In the previous video, you learned how to display this block, the powered by Drupal block on all user pages which start with slash user slash and it worked as expected. But what if you had a requirement where you need to exclude the block from a single page? So have the block appear on all user pages, but then exclude it from the login page, which is slash user slash login. How would you implement this? Now, this cannot be implemented using just Drupal core. Luckily there is a third party module called block exclude pages, which allows you to exclude pages in this condition. If you want to learn more about the module, just go to Drupal.org/project/block_exclude_pages.
This module allows you to exclude paths added to the pages condition by adding an exclamation mark in front of the path. So if we don’t want our block to appear on the login page, all we need to do is add an exclamation mark in front of the path. Let’s go ahead and install this module on our site. But before we can install it, let’s download it using composer. So jump in to your terminal and then type in the following command. Composer require Drupal slash block underscore exclude underscore pages, and then hit enter.
Once the module has been downloaded, jump back into your site and I will close this tab, then go to extend and search for block, and you should see block exclude pages selector, and then click on instal. Now we can close this tab and let’s go back to our configure page and let’s refresh. And also let’s make sure all of the other conditions are switched off. So here we can see no content types are selected.
There are no pages. But if we click on roles, we can see that anonymous has been checked. Let’s just uncheck that. And let’s go back to pages. Now you may notice this new message just above the radio buttons, which tells you how to exclude pages. This is a good way to know that the module has been installed. Let’s now implement the requirement that we discussed earlier, which is to have the block appear on all user pages, but exclude just the login page. To do this, let’s add slash user slash and then an asterisk on the first line. And then in the second line, add in an exclamation mark then slash user slash login. This means that we’ll exclude just the login page. Click on save block.
Let’s now open up a private browser so we can access the login page and we’ll go to the website. And if we click on login in the top right, we cannot see the block in the sidebar, but if we click on create new account, we can see the powered by Drupal block. And if we click on reset your password, we can see the block as well. Let’s close this browser and go back to the configure block page. Now, one thing to be aware of is that these radio buttons change how things work depending on what is selected. If the show for the listed pages option is selected, then any path with the exclamation mark will be excluded. That is to be expected. And we just saw how that works.
But if you select hide for the list of pages, then the will be hidden on all user pages except the login page, but it will be displayed on all paths that do not start with slash user slash. So just be aware of that. Let’s test this out now. Make sure hide for the list of pages is selected and then click on save block.
And then if I open up a new private browser and go to the website straight away you can see on the homepage, the powered by Drupal block. It is visible. If we click on login, we can see the block, but if we click on create new account, we can’t see it. And also if we click on reset, your password, we cannot see it. So just remember if you want to exclude a single page while displaying on all other pages, then select show for the listed pages. But if you select hide for the listed pages, it’ll work in the exact opposite way.