Score:4

Enable layout builder on custom controller page

al flag

I created a simple page in a controller class:

  public function taxonomyPage($term) {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('<p>Need to render layout builder here.</p>'),
    ];
  }

Routing:

knmt_taxonomy.detail:
  path: '/tags/{term}'
  defaults:
    _controller: '\Drupal\knmt_taxonomy\Controller\TaxonomyController::taxonomyPage'
  requirements:
    _permission: 'edit own content'

For node pages i already use layout builder to render blocks, this is working fine.

I also want to use layout builder within this page. I have zero display options for now, is there a way i can enable layout builder specific for this page so i can render blocks this way?

Score:2
cn flag

It's not possible to enable the layout builder without an entity which is supporting view modes.

However, it seems like you have such an entity already. So you could add a view mode at admin/structure/display-modes/view/add/taxonomy_term. Then visit admin/structure/taxonomy, select Manage Display of a vocabulary and enable the view mode in Custom display settings at the bottom of the page. Save and select the tab of the new view mode at the top. Check Use Layout Builder in Layout options, save again and click the button "Manage Layout".

In the controller render the term with the new view mode:

  public function taxonomyPage($term) {
    // load the term or enhance the route to load the term automatically
    // See www.drupal.org/docs/8/api/routing-system/parameters-in-routes/using-parameters-in-routes
    return $this->entityTypeManager()
      ->getViewBuilder('taxonomy_term')
      ->view($term, 'my_view_mode');
  }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.