I got a question regarding the loading of a view within a preprocess function of Drupal 8.

The only working solution I found is this one here:

$_block_level_1 = \Drupal::entityManager()->getStorage('block')->load('views_block__myview_ov_block_1');

So by using the entity manager.

The view is expecting parameters. How do I send them?

  • Have you tried using views_get_view('xyz_view'); – Shreya Shetty Nov 21 '16 at 10:50

You can render view programatically using views_embed_views() and pass views arguments to it. Check example below:

\Drupal::service('renderer')->render(views_embed_view('VIEW_NAME', 'DISPLAY_NAME', $ARGS_1, $ARGS_2));
| improve this answer | |
  • Yes. When doing this kint() goes crazy. It throws errors. I also cannot choose a display. The view is shown by the way. – mogio Nov 21 '16 at 10:59
  • Ok Sorry. Got it. Had a cache problem – mogio Nov 21 '16 at 11:02

If you prepare a render element of the view

$variables['view_block_1'] = [
  '#type' => 'view',
  '#name' => $name,
  '#display_id' => $display_id,
  '#arguments' => $args,

you can then render this view in twig:

{{ view_block_1 }}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.