Questions tagged as ['blocks']

In Drupal, a self-contained collection of content and functionality that is displayed in theme regions is called block.
Score: 2
Add a custom variable to an existing block
mx flag

Is there a way to alter the render array of an existing block? I want to do it for a commerce block (cart block) but the question remains for any core or contrib module block.

When a block uses a custom #theme for it's block, is there a way to alter the definition?

Let's say the module defines the following theme :

 * Implements hook_theme().
function commerce_cart_theme($existing, $type, $theme, ...
Score: 1
Steve Siddu avatar
How to add class to multiple view-blocks using twig?
br flag

I have four blocks to a view. I want to add class 'fancy-body' to all four blocks. How can I add that.

view_id : categories

display_id's of blocks : block_1, block_2, block_3, block_4

I know, I can add classes by creating individual twig template file for each block. like this,

block--views-block--categorie ...
Score: 0
Steve Siddu avatar
How do I dynamically add filters?
br flag

I have a view_block with the following filters,

enter image description here

I want to change the product tag, Release tag, Document type values dynamically based on url.

if the url is,, then the filters should change like this

enter image description here

I don't know how to achieve this, any suggestion would be helpful

Thank you!

Score: 0
Steve Siddu avatar
How to add contextual filter?
br flag

I am working with books in drupal 8. I want to display a specific page("table of contenets") on the sidebar whenever any page of that book is accessed. "table of contents" is a book page. every page of book has two id's.

  • page_id
  • book_id

Page_id is unique for every page but book id is same for all pages. I believe contextual filter based on book_id might solve my issue. but I don't know how to add c ...

Score: 0
Hide and unhide custom block programatically drupal 9
cn flag

Hello I have created a module to display a block. I would like to set the block visibity to visible and hidden in the homepage based on a condition.

like this:


//fetch status from database then check condition
if ($status = 1){
//make block visible
//hide block

I have searched all over the internet and could not find how to make it work.

I have a form that is used to add contents to a ...

Score: 0
Custom block plugin: Give access to edit form
in flag

I am creating a custom block plugin with custom configuration options (blockForm()).

I would like to allow content editors to edit the configuration of this block, without giving them "administer blocks" permission. Also, I don't want them to create new block instances..

I would like to avoid installing an additional module like block_permissions, as this would introduce a wide range of settings or pe ...

Score: 1
How do I get the selected image style from a referenced media field?
cn flag

I created a custom block with an entity reference to a media field (field_background_image).
In the display settings I have chosen 'Thumbnail' as format and selected a custom image style 'background'.

In my _preprocess_block(&$variables) function I would like to get the selected format (thumbnail) and especially the selected image style ('background').
How can I load both these information from the $v ...

Score: 0
How can I make certain fields of my block implementation translatable?
vn flag

I've created a simple module, which extends BlockBase to provide a custom block.

This block class has a few fields (e.g., "Header"), which I would like to be translatable.

How can I accomplish this? The Drupal docs have a section titled "Make your block translatable", but it hasn't actually been written yet.

I've found some posts which suggest that my module needs a .schema.yml file – but I'm n ...

Score: 0
user3038672 avatar
Extending block, a block with a paragraph field, and 3 paragraph fields... how to loop through the paragraphs from the block extension
cn flag

This is what I have:

I have three twig files:

  1. block--paragraph-images.html.twig (which extends block.html.twig)
  2. field--field-paragraph-image-block.html.twig (the paragraph field which is located in the custom block type, block--paragraph-images.html.twig.)
  3. paragraph--field-paragraph-image.html.twig (which houses three fields: field_paragraph_image, field_paragraph_title, and field_paragraph_text ...
Score: 2
New To 'C' avatar
Block Settings inside Form
in flag

Please I need help with my code. I have a form and a block created programmatically. The block embeds the form to show it on the frontend. The block also has some settings (form preffix text). I want to load the block settings inside my buildForm in my Form.php to get the preffix text and show it in the form markup field. But I can't seem to find a way to pass on the $block_id to the buildForm() so it c ...

Score: 1
Attempting to return region in theme suggestions for the book module book tree block
cn flag

Editing question based on information from @NoSssweat

The book module uses the same hook for the book tree block and the rendered index view in the book page.

I have used the below code to return file name suggestions, so that I can override the html output for the book tree block in a specific region - "sidebar-first". I expected the below code would spit out something like book-tree--book-toc-180--sid ...

Score: 1
liquidcms avatar
How to alter Layout Builder block visiblity?
us flag

Is there a hook to alter block access for blocks placed using Layout Builder? I see there is hook_block_access; but sadly this is not triggered when the block is placed using Layout Builder.

There is also work going on here: to allow standard block visibility settings to be made available to Layout Builder blocks; but this only all ...

Score: 0
Steve Siddu avatar
How to change the condition to AND in accessing content by role in a view-block?
br flag

I have a view-block 'Expo', I want to display this block only if current user has two specific user roles i.e., 'Actor' and 'Singer'.

When I checked the both 'Actor' and 'Singer' , The block is displaying in users who have either 'Actor' or 'Singer' user role. When the user has two user roles it's displaying two blocks.

As per my understanding, It's using conditional OR in between the checked us ...

Score: 0
user3038672 avatar
Trying to access a "count" variable inside a block field from paragraphs template
cn flag

I'm trying to get the number of paragraphs an authenticated user inputs by counting them in the parent block template and trying to access that number in the paragraphs template. I have two templates I'm working with:

  1. field--field-hover-image-block.html.twig
  2. paragraph--hover-images.html.twig

The paragraphs are being loaded through the paragraph entity field in a custom block. I'm counting the numb ...

Score: 1
Render view inside block with caching
lc flag

My setup is a bit unconventional. I have a view displaying a block, relying on a contextual filter (let's call it product ID). I also have a custom block that renders this view programmatically because I need to include this block in multiple places on the page and I have some custom logic that pulls the actual product ID to call the view with. Basically:

$view = Views::getView('view_id'); 
$args = ...
Score: 1
Hasina Njaratin avatar
Register form block into modal
de flag

I want to display the register form in a modal. For that, I created a custom plugin block which returns the rendering of the form register.

 * {@inheritdoc}
public function build() {
   $entity = \Drupal::entityTypeManager()->getStorage('user')->create(array());
   $formObject = \Drupal::entityTypeManager()
     ->getFormObject('user', 'register')

Score: 0
Create a list of blocks and the pages they are currently being used on
cn flag

We have a D9 site that makes extensive use of Layout Builder. One of the features our content editors use is creating a global custom block in the Custom Block Library and then on various pages they may place that global block using layout builder. They made a request asking us if there was a way to tell what pages in the system certain blocks are currently being used. They have about 700 pages and they ...

Score: 0
SomebodySysop avatar
modify custom block using hook_entity_type_view_alter()
gb flag

In Drupal 9, I am trying to use hook_ENTITY_TYPE_view_alter() to modify the body field of a custom block.

However, I don't know how to access the data in the $build array to change it. This is a partial dump of the $build array. I want to modify the HTML links in the body field.

array(8) {
  ["#block_content"]=> object(Drupal\block_content\Entity\BlockContent)#4946 (30) {
    ["theme":protected]=> ...
Score: 0
SomebodySysop avatar
How to use hook_block_access() with custom block created in UI
gb flag

I have a custom block created using Drupal 9 Custom block library. I want to use hook_block_access() to control access to it, but, as it is a custom block, it does not have a block machine name. The only ID I can find (using Devel variables) is:

stdClass Object ( [CLASS] => Drupal\block_content\Entity\BlockContent [theme:protected] => [values:protected] => Array
( [id] => Array ( [x-d ...

Score: 0
admin/modules takes 40 seconds to load after uninstalling custom module
lb flag

During the development, I made a couple typos in the block which resulted in Drupal seeing the module as being deleted without having been uninstalled first. When I realized what I had done, I put the typo back in place then did a proper uninstall of the module. I then went back, fixed the typo and re-installed the module. From what I can tell, the bad module was uninstalled properly and the good mod ...

Score: -1
John avatar
Show two content types in same view
rs flag

Drupal 8 newb here,

I have two basic types I need to show in my front page: There’s a “Restaurants” content type and a “Chefs” content type.

These are shown in my site like this:

Site layout

The types are unrelated, I just need to show them in the same page as featured.

I’ve found documentation on how to make blocks and views but nothing about mixed content, which seems to me like a common use case.

Score: 0
Gokulakrishnan Shankar avatar
Is a block the same thing as content?
ng flag

I'm new to Drupal and I would like to know how to manage content in a block. This is what I understood so far:

  • The Drupal page is divided into regions and each region consists of one or more blocks which can be configured in the Appearance/Block Layout page.
  • According to my understanding, there are 2 ways to add content:
    1. Directly go to the Content tab and manage the content. However, I do not know into ...
Score: 0
Blocks activated on Article content type
us flag

My website has heading blocks that appear for certain pages and (as expected) I have to specify that each heading block should only appear for /node/xxxx.

I have an Events content type, which are all articles. I would like to have a specific header block show up only for Events, if possible. I will have many, many events and would like to automate that process (if possible.)

I have not yet found a m ...

Score: 1
The form argument Drupal\terminal\Form\TerminalCliForm is not a valid form
cl flag

I've read through other questions regarding the same error message. They all seem to end up being due to typos or the file path. I don't believe that is the case here, but we'll see. I have a working instance of the same code aside from the names, which is all the more confusing.

I enable the module below. Select place its block. And then when I hit the page, receive:

InvalidArgumentException: The for ...
Score: 0
How can I have a custom block show different content on the front page?
za flag

I have a custom module, in Drupal 9, (showing a block in a region) that has an admin form with a checkbox for showing only on the "front page" or if unchecked on "all" pages.

In order for the block to show on the homepage or on all pages do I need some sort of preprocess function to run before the block loads? Does the build() only happen once and then is cached? If so, how do we let the block kn ...

Score: -1
Alireza Tabatabaeian avatar
How to handle different output for a same block (Caching Problem)?
cn flag

I have a theme which has two different appearance for main menu block, so I wrote a theme suggestion like this:

function MYTHEME_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
  if ($variables['menu_name'] == 'main' and Drupal::service('path.matcher')
  ->isFrontPage()) {
    array_splice($suggestions, 1, 0, 'menu__main__front');

so that I can theme main menu for fr ...

Score: 0
One Taxonomy with multiple views to show content related to a specific term and referenced content
gb flag

I have two content-type:

  • Job
  • Organisation

and a taxonomy:

  • categories

The content type "Job" has a field referenced to "categories" and another field referenced to "organisation".

On the front page, I put a "view block" to list all the "categories", therefore I can browse all the jobs related to a specific category. enter image description here

On the organisation node, I put another "view block" to list all the "categor ...

Score: 0
stckvrw avatar
How to get all the pages where a block is placed
gb flag

If I have a block as an entity inside of the function hook_entity_update(), then how to get the paths of all the pages where the block is placed? Can't find an anwser via Google.

Or maybe first I should get the regions? If so, how to get the regions and then the pages?

I see that I have the following four properties inside of the hook_entity_update() when the entity type is block: provider, visibilit ...

Score: 0
youni avatar
Show a block only in in the front page and not in every pager page
cn flag

I configured a block to be shown only on the front page, but the front page is actually a view of the last published nodes, with a pager, and the block is shown on each of its pages.

Is there any convenient way to show the block only once? I know it is possible to write a hook that checks if the page parameter is set and hide the block in that case.

What hook should I use to achieve this?

Score: 0
Delford Chaffin avatar
Is there a way to specify which content should be shown from the the "Entity View (Content)" block?
gg flag

As part of CTools, there is the "Entity View (Content)" block that can be used in Layout Builder, but that only shows the current content. Is there a way to specify another piece of content?

For example, on my front page, I would like to place a block that renders the teaser view for another content without having to build a view or add a reference field or something.