How to get node's view mode in hook_page_attachments? 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 &amp;$attachments) {
  $node = \Drupal::routeMatch()-&gt;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. 
    }
  }
}
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 &amp;$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {

  $node = $entity;
  if ($node-&gt;bundle() === 'page') {
    if ($view_mode === 'full') {
      $build['#attached']['library'][] = 'MYMODULE/foo';
    }
    elseif ($view_mode === 'default') {
      $build['#attached']['library'][] = 'MYMODULE/bar';
    }
  }
}