Drupal 8: entityQuery and render custom blocks by bundle

In below example, 'testimonial' is our custom block type (bundle) which we added in /admin/structure/block/block-content/types. Drupal 8 default install sets you up with one type called 'basic'.

$html = '';

$query = \Drupal::entityQuery('block_content')
  ->condition('type', 'testimonial');
$ids = $query->execute();

$blocks = entity_load_multiple('block_content', $ids);
$view_mode = 'default';

foreach ($blocks as $entity) {
  $html .= render(entity_view($entity, $view_mode));
}

return $html;

Note: the entity type is 'block_content', and NOT 'block'. The latter will return active blocks for all active themes or something like that.


Links:
https://www.drupal.org/node/1827278
http://www.sitepoint.com/drupal-8-version-entityfieldquery

About

This is the company blog of
Drupal specialist Merge.nl

We are located in Breda (Netherlands) and build websites using Drupal. More about us.

Content on this blog is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.

Creative Commons License

Recent Comments

Social