Questions tagged as ['blocks']

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, ...
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--categories-block-1.html.twig
block--views-block--categories-block-2.html.twig
block--views-block--categorie ...
I have a view_block with the following filters,
I want to change the product tag, Release tag, Document type values dynamically based on url.
if the url is, example.com/mars/21.43.00/ProductGuide
, then the filters should change like this
I don't know how to achieve this, any suggestion would be helpful
Thank you!
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 ...

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:
edit:
//fetch status from database then check condition
if ($status = 1){
//make block visible
}else{
//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 ...

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 ...

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 ...

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 ...
This is what I have:
I have three twig files:
- block--paragraph-images.html.twig (which extends block.html.twig)
- field--field-paragraph-image-block.html.twig (the paragraph field which is located in the custom block type, block--paragraph-images.html.twig.)
- paragraph--field-paragraph-image.html.twig (which houses three fields: field_paragraph_image, field_paragraph_title, and field_paragraph_text ...
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 ...

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 ...
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: https://www.drupal.org/project/drupal/issues/2916876#comment-14279293 to allow standard block visibility settings to be made available to Layout Builder blocks; but this only all ...
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 ...
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:
- field--field-hover-image-block.html.twig
- paragraph--hover-images.html.twig
The paragraphs are being loaded through the paragraph entity field in a custom block. I'm counting the numb ...

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 = ...
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')
->setEntity($entity);
...

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 ...
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]=> ...
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 ...

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 ...
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:
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.
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:
- Directly go to the Content tab and manage the content. However, I do not know into ...

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 ...

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 ...

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 ...
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 ...

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.
On the organisation node, I put another "view block" to list all the "categor ...
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 ...
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?
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.