2

I'm in Drupal 8 trying to load a Views Exposed Filter block based on its ID, similarly to how I would load a content block.

$bid = 'exposedformview_nameview_display_name'; // Found on mysite.test/admin/structure/block/manage/exposedformview_nameview_display_name/
$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityManager()
        ->getViewBuilder('block_content')
        ->view($block);
return $render;

Nothing ends up printing out, and in the error log I see this message:

Recoverable fatal error: Argument 1 passed to Drupal\block_content\BlockContentViewBuilder::view() must implement interface Drupal\Core\Entity\EntityInterface, null given

Sure enough, when I debug($block), it shows NULL.

I know this works for other blocks created in the content placement area, not sure why it wouldn't also work for views exposed filter blocks. They are both available for placement in the manage blocks page.

EDIT: It seems as though it will not be possible to load the block like this. This method depends on revisions being available for blocks. There is no revision for a views exposed filter block so there must be another way to accomplish this. Perhaps by loading the form directly.

0

1 Answer 1

10

The block content entity is not the block entity that is required there.

The actual block entity is a configuration entity. Use the \Drupal\block\Entity\Block::load method to load it.

Try the following:

$bid = 'exposedformview_nameview_display_name';
$block = \Drupal\block\Entity\Block::load($bid);
$render = \Drupal::entityTypeManager()
        ->getViewBuilder('block')
        ->view($block);
return $render;
5
  • 1
    Also note that the entity manager service is deprecated and should not be used.
    – Eyal
    Jun 13, 2016 at 21:50
  • Since the exposed form view blocks do not have revisions, I don't think it will be possible to load this in the same way a regular content block would be loaded.
    – CR47
    Jun 14, 2016 at 14:06
  • The above code has no relation to revisions. You tired to load the wrong entity.
    – Eyal
    Jun 14, 2016 at 14:11
  • This is the error received from the above code: PHP Fatal error: Call to undefined method Drupal\\block\\Entity\\Block::isDefaultRevision() in /var/www/html/src/core/lib/Drupal/Core/Entity/EntityViewBuilder.php on line 164
    – CR47
    Jun 14, 2016 at 14:13
  • Fixed I kept the block_content as the entity type by accident.
    – Eyal
    Jun 14, 2016 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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