views_embed_view(($name, $display_id = 'default')
Embed a view using a PHP snippet.
This function is meant to be called from PHP snippets, should one wish to embed a view in a node or something. It's meant to provide the simplest solution and doesn't really offer a lot of options, but breaking the function apart is pretty easy, and this provides a worthwhile guide to doing so.
Here is my example, it works well.
<?php
$view = \Drupal\views\Views::getView('views_name');
$view->setDisplay('default');
$view->setArguments(array('arg1'));
$view->setItemsPerPage(10);
$view->setOffset(0);
$view->usePager();
$view->execute();
$view->serialize();
$result = $view->result;
foreach ($result as $key => $value) {
$entity = $value->_entity;
$title = $entity->get('title')->getValue()[0]['value'];
$custom_field = $entity->get('field_custom')->getValue();
}
?>
- Add new comment
- 551 views