I followed steps given in this link to create a custom Page.

Page is created and 'Hello world' is printing in the page, now i want to render some blocks in that page. How can I achieve this task?

  • If you are not looking for a coding approach this can be achieved easily with Context.
    – pierostz
    Commented Jan 20, 2017 at 8:16
  • 1
    Context is in D8 still alpha and you don't need it, because the block layout is now in core.
    – 4uk4
    Commented Jan 20, 2017 at 10:47
  • The question is a little vague - but, I'm assuming because the work "render" was used, @kenorb is attempting to place a block via the Render API. If that's the case "Context" has nothing to do with it.
    – sea26.2
    Commented Feb 9, 2019 at 23:24

2 Answers 2


Given that you're using a custom controller for your page, you have the option to load the block from your controller, or place the block in another region outside of that part of the page that the Controller controls.

For blocks such as global elements such as headers and footers, I would recommend you place them through the CMS at /admin/structure/block

For blocks that sit above or below the content that your controller loads, you can also place them from /admin/structure/block or you can load them from within the the controller.

If you need to place content above and below your block, you'd probably want to load it programmatically in your controller.

Here's a guide on loading blocks programmatically.

basically in your myPage() method that is returning the build array, you'd want the build array for the block you want to load.

$build['events'] = \Drupal::service('plugin.manager.block')

$build['recent_insights'] = \Drupal::service('plugin.manager.block')
  ->createInstance('recent_articles', ['title' => 'Some Recent Articles', 'offset' => 0])

return $build;

The code above works for the types of blocks that you custom create in your module, such as at /mymodule/src/Plugin/Block/RecentArticles.php

It doesn't work for loading an instance of a block that was created through the CMS.

For those you need to use:

$block = \Drupal::entityTypeManager()->getStorage('block')->load('myblock');
$build['myblock'] =  \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);

To render block on your custom page, go to Structure, then Block Layout, from there you can either create a custom new block (enable Custom Block for that), or configure the existing block. To place on your page, in Configure page, go to Pages under Visibility section and add path of your custom page to the list, then save. Your block should be shown according to assigned region.

If your custom page uses Twig template, see: How to render a template in Drupal 8.

If you'd like to create your own block programatically, see: Block API overview.

Your Answer

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

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