Taxonomy term pages are a great way to display your content by category, however they do not play nice with layout builder out of the box. There is no block included with Drupal core that will render a term in a view mode. The Chaos tools suite module has a block to render node entities, but not taxonomy terms.

The default taxonomy term page and view included in core is great, but it also comes with some baggage. The page renders a view that returns all content related to that term. That is good if you only have one vocabulary that you want term pages for. But if you want more than one, you may want to render them using a different layout or view mode, and then you need more flexibility.

This simple block can save you the headache of publishing taxonomy term pages with a term's rendered entity in layout builder, or the regular block layout. It provides a selector to choose an available render mode. This allows you to display the rendered term on the page with or without results.

If you want to display related content on the same page, you can choose the taxonomy term view shipped with core in a separate block, or you can create a new view and further customize the results for your content needs.

Shared on Github

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a block for rendering Taxonomy Terms in a view mode
 *
 * @Block(
 *   id = "taxonomy_term_rendered_entity_block",
 *   admin_label = @Translation("Taxonomy Term Rendered Entity Block"),
 *   category = @Translation("Custom"),
 * )
 */
class TaxonomyTermRenderedEntityBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    if ($term = \Drupal::routeMatch()->getParameter('taxonomy_term')) {
      $config = $this->getConfiguration();
      $build = \Drupal::entityTypeManager()->getViewBuilder('taxonomy_term')
                      ->view($term, $config['view_mode']);
      return $build;
    }
    return [
      '#markup' => $this->t('Not a Taxonomy Term')
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();

    $view_modes_opts = ['' => '- Select -'];
    $view_modes = \Drupal::entityQuery('entity_view_mode')
                         ->condition('targetEntityType', 'taxonomy_term')
                         ->execute();
                         
    foreach ($view_modes as $key => $value) {
      $val = str_replace("taxonomy_term.", "", $key);
      $view_modes_opts[$val] = $val;
    }

    $form['view_mode'] = [
      '#type' => 'select',
      '#options' => $view_modes_opts,
      '#required' => true,
      '#title' => $this->t('View Mode'),
      '#description' => $this->t('The view mode to render the Taxonomy Term'),
      '#default_value' => $config['view_mode'] ?? '',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['view_mode'] = $form_state->getValue('view_mode');
  }

}
Last Updated October 23rd, 2021