3

I need to get the node's view mode so that in the mymodule_page_attachments, I can add libraries conditionally if the node's view mode is set to full or default. I tried the following but not able to get the nodes view mode once I retrieve the$node` object.

How to get node's view mode in hook_page_attachments?

Here is what we tried:

function mymodule_page_attachments(array &$attachments) {
  $node = \Drupal::routeMatch()->getParameter('node');
  if(is_object($node)) {
     // we tried printing but that didn't result any view_mode field. 
     ksm($node);

    // Goal
    if($node['view_mode'] == 'full' or $node['view_mode'] == 'default') {
       // proceed with my logic. 
    }
  }
}
2
  • Drupal does not distinct between full and default here. If no full viewmode is given for a certain node type than the default viewmode is used. So you could check whether a given node type implements the full viewmode and apply your conditional logic based on this. Commented Oct 25, 2019 at 5:48
  • Is your question rather "How can I attach a library when a node is viewed in a certain view mode?" or "How can I attach a library when a certain view mode for the current single node exists?"
    – leymannx
    Commented Oct 25, 2019 at 8:26

2 Answers 2

5

To attach a library only when a node in a certain view mode is displayed you can use hook_entity_view/hook_ENTITY_TYPE_view like following.

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function MYMODULE_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  $node = $entity;
  if ($node->bundle() === 'page') {
    if ($view_mode === 'full') {
      $build['#attached']['library'][] = 'MYMODULE/foo';
    }
    elseif ($view_mode === 'default') {
      $build['#attached']['library'][] = 'MYMODULE/bar';
    }
  }
}
4
  • it will give you "full" view mode even if its using default view mode because full is not defined for particular bundle Commented Oct 25, 2019 at 8:45
  • @StefanKorn – True that. It's just I'm not entirely sure what OP really wants. Is it OP wants to look if a certain view mode is defined and add the libraries then (sounds a bit unrealistic but who am I) or is it OP really needs to add a lib if a node in a certain view mode is being displayed (sounds more realistic to me).
    – leymannx
    Commented Oct 25, 2019 at 8:55
  • You're right, question could have some clarification. Since OP was exactly asking between full and default and on routeMatch, I thought it's about distincting between full and default. And if you go on routeMatch you will imho by drupal default (entity.node.canonical) never getting anything else then full. Commented Oct 25, 2019 at 9:09
  • 2
    This is the correct hook, not only because the node is already available, but also for performance reasons, because the attached library is cached in the metadata of this node and then bubbles up to page level whenever the node is displayed. Always attach content specific libraries to the content and not to the page.
    – 4uk4
    Commented Oct 25, 2019 at 13:22
1

Drupal does not distinct between full and default here. If no full viewmode is given for a certain node type than the default viewmode is used. So you could check whether a given node type implements the full viewmode and apply your conditional logic based on this, like this:

function hook_page_attachments(array &$attachments) {
  /* @var \Drupal\node\Entity\Node $node */
  $node = \Drupal::routeMatch()->getParameter('node');
  if($node) {
    $view_modes = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle('node', $node->bundle());
    if (isset($view_modes['full'])) {
      // this is full
    }
    else {
      // this is default
    }
  }
}

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.