A theme preprocess function to automatically allow view-based node templates if called from a view.
The 'modules/node.views.inc' file is a better place for this, but we haven't got a chance to load that file before Drupal builds the node portion of the theme registry.
function views_preprocess_node(&$vars) {
Drupal::moduleHandler()
->loadInclude('node', 'views.inc');
// The 'view' attribute of the node is added in views_preprocess_node()
if (!empty($vars['node']->view) && $vars['node']->view->storage
->id()) {
$vars['view'] = $vars['node']->view;
$vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->storage
->id();
if (!empty($vars['node']->view->current_display)) {
$vars['theme_hook_suggestions'][] = 'node__view__' . $vars['node']->view->storage
->id() . '__' . $vars['node']->view->current_display;
// If a node is being rendered in a view, and the view does not have a path,
// prevent drupal from accidentally setting the $page variable:
if ($vars['page'] && $vars['view_mode'] == 'full' && !$vars['view']->display_handler
->hasPath()) {
$vars['page'] = FALSE;
}
}
}
// Allow to alter comments and links based on the settings in the row plugin.
if (!empty($vars['view']->rowPlugin) && $vars['view']->rowPlugin
->getPluginId() == 'entity:node') {
node_row_node_view_preprocess_node($vars);
}
}