I am having a problem with my custom blocks rendering, and I want to programmatically manipulate the blocks to troubleshoot the issue.

I used this question to add this hook to my module:

function mymodule_block_view_alter(array &$build, Drupal\Core\Block\BlockPluginInterface $block) {
  drupal_set_message($block->getBaseId());
}

However, when I navigate to my site, I see only the base ids of system blocks:

- page_title_block
- system_main_block
- help_block
- local_tasks_block

My site's header and footer are made of custom blocks, so I should be seeing their ids.

How can I programmatically access my custom blocks?

Edit this is my actual code:

$ cat my_block_hooks.module
<?php

function my_block_hooks_block_view_alter(array &$build, Drupal\Core\Block\BlockPluginInterface $block) {
  drupal_set_message(__FUNCTION__ . "() id: " . $block->getBaseId() );
}

And here's a screenshot:

enter image description here

You can see that all the system blocks show up, but my custom block does not.

This is the custom block as I have it set up:

enter image description here

And here I have it placed in the layout:

enter image description here

And of course, I've cleared the cache many times to get to this point.

What am I doing wrong?

up vote 8 down vote accepted
+50

The type of entity you are dealing with defines the hook you need to implement; in this case it is NOT block but rather block_content, as provided by the "Custom Block" core module.

N.B.: It is generally good practice to use more specific hooks when possible, as it will have a general effect on your code's performance, i.e. your hook implementation will be called less times if it is more specific. In your case, instead of going through all blocks available on any page during page loading, you are going through only custom blocks (block_content entities).

In order to get your hook_ENTITY_TYPE_view()/hook_ENTITY_TYPE_view_alter() implementation to work, you need to replace ENTITY_TYPE with block_content. That should give you the following code (which I tested and works):

use Drupal\block_content\Entity\BlockContent;
...
function my_block_hooks_block_content_view_alter(array &$build, BlockContent $custom_block) {
  drupal_set_message(__FUNCTION__ . "() id: " . $custom_block->id() );
}

And the visual result:

enter image description here

Good luck!

You're using the built-in custom block which ships with Drupal. In your code your getting the baseId which is in your case block_content and it is shown in your screenshot actually.
I think you need to use $block->id() instead.

You can use getPluginId to get the id of the block:

function my_block_hooks_block_view_alter(array &$build, Drupal\Core\Block\BlockPluginInterface $block) {
  drupal_set_message(__FUNCTION__ . "() id: " . $block->getPluginId());
}

here is the result:

enter image description here

Yes, you should be seeing the IDs of your custom blocks. Here are some things you can try:

  • Make sure you actually have the custom block nicely defined and placed on your Manage blocks page.
  • Make sure your custom block is actually visible on the page? Return some temporary output like "foo" from your custom block plugin and make sure that the "foo" is visible on the page.
  • Try removing cache for your block temporarily by adding $build['#cache']['max-age'] = 0; to your block's output.
  • Drush drush cr or clear cache and see if your custom block is visible.
  • Check the visibility rules for your custom block in block configuration. Try removing all visibility rules temporarily and see if the block shows up.
  • If you've implemented any block access rules, try disabling them to see if your block becomes visible.
  • Multiple blocks can have the same Base ID. Try doing $block->getPluginId() or $block->id(). In your case, it should give you an item like block_content:UUID where UUID is the unique ID of the custom block you created in your custom block library.

The ID of your block not showing up is basically sounds like your block is not being displayed on the page.

  • I've updated my question to address some of the questions you've raised. It's on a new Drupal 8.6.3 clean install, do I don't have any custom visiblity rules (other than what would have come out of the box). Hopefully you can help spot my mistake : ) – user1359 Nov 29 at 21:36
  • Also check that it is placed in the currently active (default) theme. Sometimes when you have multiple active ones, it might be placed in block set of a non-default one. – Ismail Cherri Nov 29 at 22:28
  • The item block_content corresponds to your custom block. Since you're dealing with custom content blocks, the base ID for all blocks created in the custom block library will be block_content. So use $block->getPluginId() to get the unique block ID. It will be something like block_content:UUID. – Jigar Mehta Nov 30 at 22:08
  • Also, it seems like you're new to Drupal. It will be great if you could say the exact problem you're facing while rendering the blocks. Maybe there is an easier solution? – Jigar Mehta Nov 30 at 22:09

I am going to take a wild guess, but have you tried clearing the cache? Custom blocks normally do not contain dynamic data and as such can be heavily cached. This causes the block output to be rendered without your hook getting called.

In the configuration menu go to development > performance and hit the 'Clear all caches' button. Then go back to the page with your blocks and see if it made a change. Alternatively you can install the devel module to clear the cache from each page directly.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.