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

delta created an issue. See original summary.

delta’s picture

Issue summary: View changes
dawehner’s picture

What are you trying to do? In general with enabled caching of views, stuff like that is not possible any longer.

delta’s picture

I 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 ??

dawehner’s picture

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.

Ideally you put that kind of information into #attached, as they would be moved to the top level of the HTML.

delta’s picture

Category: Bug report » Support request
Status: Active » Closed (works as designed)

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