Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When I'm calling views_get_current_view()
in a hook_preprocess_html() on a views page display that's return me a NULL results.
To retrieve the current view in the preprocess_html I'm forced to use that code below, and so I didn't get the view in his good state (with arguments, if they are...) :
$route_match = \Drupal::routeMatch();
$route_name = $route_match->getRouteName();
// Look for a view page.
$matches = array();
preg_match('/view\.(.*)/', $route_name, $matches);
if (empty($matches[1])) {
return NULL;
}
$view_id = $route_match->getParameter('view_id');
$view = Views::getView($view_id);
$view->setDisplay($route_match->getParameter('display_id'));
Need some help from a views maintainer here, thanks !
Comments
Comment #2
delta CreditAttribution: delta commentedComment #3
dawehnerWhat are you trying to do? In general with enabled caching of views, stuff like that is not possible any longer.
Comment #4
delta CreditAttribution: delta commentedI port the metatag views integration from drupal 7, https://www.drupal.org/node/2563647
I have a display extender plugin that is working well. You can try the little module called page_title_views to see.
So In the preprocess I need to know if the current page is a views and if its the case I get the settings about the views page title (metatags) and set it to the page head title and html attributes.
Edit: Maybe I have to use a static function to "save" the metatag directly in the preExecute of the view display extender plugin and retrieve that later in the preprocess html ??
Comment #5
dawehnerIdeally you put that kind of information into
#attached
, as they would be moved to the top level of the HTML.Comment #6
delta CreditAttribution: delta commentedok I change this issue to a support request that may help others. Basically you can't use this function..
In my case using #attached in a hook_views_pre_render() has solved my issue.