1

What I want to do is show the contact person responsible for a language in two different regions simultaneously, but with different twig templates.

I therefore have two regions: footer and sidebar

I have a node type of Contact Person (contact_person) with several fields and I create one content of this node type per language (using Only One module)

I started with creating a View which get's me the correct content, but with two blocks. This gave me the same issue. I tried to circumvent this issue by creating two Views (Contact Person (Footer) and Contact Person (Sidebar)) each with one block display.

I changed the system names in the views to be block_sidebar and block_footer instead of the default block_1 for added clarity.

I used these blocks in the Block Layout, where I added both blocks to their respective regions.

If I examine the theme suggestions, the blocks behave as expected

For Sidebar:

block--views-block--contact-person-sidebar-block-sidebar.html.twig
___________________________________^^^^^^^_______^^^^^^^

For Footer:

block--views-block--contact-person-footer-block-footer.html.twig
___________________________________^^^^^^_______^^^^^^

But when I examine the node theme suggestions, the are the same

For Sidebar AND Footer:

node--view--contact-person-sidebar--block-sidebar.html.twig
___________________________^^^^^^^________^^^^^^^
if the Sidebar block get's rendered first.

If I swap the blocks in their regions (Sidebar Block goes inside the Footer region and Footer block inside the Sidebar region) and clear all the caches, the footer block is rendered first and so the theme suggestions changed for both of them:
node--view--contact-person-footer--block-footer.html.twig
___________________________^^^^^^________^^^^^^

These suggestions seem to generated in the Views module itself

// File core/modules/views/views.module
// ...
/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function views_theme_suggestions_node_alter(array &$suggestions, array $variables) {
  $node = $variables['elements']['#node'];
  if (!empty($node->view) && $node->view->storage->id()) {
    $suggestions[] = 'node__view__' . $node->view->storage->id();
    if (!empty($node->view->current_display)) {
      $suggestions[] = 'node__view__' . $node->view->storage->id() . '__' . $node->view->current_display;
    }
  }
}
// ...

If I try to debug this I discovered that the hook_theme_alter function is only called once for the node. I isn't called a second time, because the hook_theme_suggestions_HOOK_alter was already called for this very node.

How can I say Drupal that I want these suggestion alter function to be called a second time, if one node is shown a second time on the same page?

3

Nodes are rendered only once per view mode and then are cached. So the easiest solution would be to configure different view modes. Which makes sense in this case, these are in fact two different displays of the same node. Another benefit, you don't need this hook, because view modes have theme suggestions out-of-the-box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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