One of the problems we have spent lot’s of energies on is related to Drupal 8 views.
As you probably know the contrib module views has been integrated as a core module in this version 8 of Drupal.
Eventough there’s a nice page with documentation on D8 views, as a Drupal developers we know we can not fully rely on it to get rid of problems.
Interviewer: Drupal 8 is amazing … isn’t it?
Angry me: No, it’s not!
The problem
When a view is printed into a webpage as block or in a shape different of “page” display type, you can not have ajax features working properly on it, furthermore you are also not able to manually (via javascript) refresh it (triggering “RefreshView”).
If you watch what happen in a standard views with page as display type, and then start to playing “find the difference” with the other display types, you will (soon or later) notice that lot’s Javascript files views-ajax related, are not even included into the DOM!
But, what is happening?
For some reason, after hours, we have understand that even if you flag the checkbox to “use ajax” you are not able to get
Drupal.views.instances
in browser console and neither, of course, the ajax views behavior.
The solution
When you are invoking the view do not forget to invoke “preview” method too, because it is responsible to prepare the view as desired.
<?php // Getting a view. $view = Views::getView('view_name'); // Invoking preview to pre and post execute the view. $view->preview('view_display_type'); // Getting the view (WITH AJAX WORKING! YEAH!!) $sample_reception = $view->render(); ?>
We found no documentation on the web, so we assume this article will be useful for some Drupal information thirsty developer.
Thanks to Luca Rossi that achieved the solution.