0

I've created a block type via the UI. This is a translatable block with 3 text-fields.

I am using hook_block_view_alter($build, $block) to massage the data. However, I can not seem to find the fields neither in $build or $block vars. Any idea on the fetch the field's values programmatically?

2 Answers 2

3

You are in the wrong entity. block is a config entity to configure where and how the block is displayed and which plugin is used to build the block.

The plugin can be a core, contrib or custom block class. In your case a core plugin, which fetches content from the database. The content is stored in the content entity block_content.

So the entity type to modify the fields of a custom block is block_content and you have to use this hook:

hook_block_content_view_alter(&$build, $block_content) 
0

This is not how it is done in Drupal 8. The Drupal 8 way is to define a custom Plugin (a cusotm Block) and the add the data directly in the plugin. No alter is required!

This is how you make a custom block: https://www.drupal.org/docs/8/creating-custom-modules/create-a-custom-block

Also the Drupal 8 block API: https://www.drupal.org/docs/8/api/block-api

2
  • Why isn't this the proper drupal 8 way? Then why do we have block types? Also hooks for blocks are still there. What I need is to load the fields for that block instance somehow. I can not seem to find it neither on the web nor on the docs(probably missing something) Commented Oct 19, 2016 at 14:55
  • You can load whatever you want in the build() in the block plugin, also you can add every dependency injection there. It is not the drupal 8 way because if you can do something without an alter, then you should. Commented Oct 20, 2016 at 7:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.