5

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?

1 Answer 1

3

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');
  }

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.