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.
}
}
}