Building an "artificial" view in Drupal 8

Hey!

What I would like to do is render custom data as a view without actually creating / saving a view. This allows me to build the view differently every time it is displayed, show anything as its results but still use the hooks provided by the views module. Even though this is most probably not the cleanest approach it turned out to be really handy in Drupal 7.

This was my (simplified) Drupal 7 approach:

$view = new View();
$view->name = 'my_view_name';
$view->description = 'My custom view.';
$view->human_name = 'My View Name';
$handler = $view->new_display('default', 'Master', 'default');

// Building fields like this:
$handler->display->display_options['fields']['created']['id'] = 'created';
$handler->display->display_options['fields']['created']['table'] = 'node';
$handler->display->display_options['fields']['created']['field'] = 'created';
$handler->display->display_options['fields']['created']['label'] = '';

$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';

// Adding the results like this.
$view->result = array();

foreach ($custom_results as $custom_result) {
  $result = new stdClass();
  $result->title = $custom_result['title'];
  $result->created = $custom_result['created'];
  $view->result[] = $result;
}

$view->build();
$view->pre_execute();

$vars = array(
  '#theme' => 'views_view',
  '#view' => $view,
  '#theme_wrappers' => array('views_container'),
  '#attributes' => array('data-id' => $this->id),
);

return drupal_render($vars);

I tried to rebuild this functionality in Drupal 8, and even though no error is thrown the rendered view is empty. This is my attempt:

$view = new View(array(), 'view');
$view->set('id', 'my_view_name'');
$view->set('description', 'My custom view.');
$view->set('title', 'My View Name');
$view->addDisplay('default', 'Master', 'default');
$display = $view->getDisplay('default');

// Building fields like this:
$display['display_options']['fields']['created']['id'] = 'created';
$display['display_options']['fields']['created']['table'] = 'node';
$display['display_options']['fields']['created']['field'] = 'created';
$display['display_options']['fields']['created']['label'] = '';

$display['display_options']['fields']['title']['id'] = 'title';
$display['display_options']['fields']['title']['table'] = 'node';
$display['display_options']['fields']['title']['field'] = 'title';
$display['display_options']['fields']['title']['label'] = '';

$executable_view = $view->getExecutable();
$executable_view->initHandlers();
$executable_view->display_handler->setOption('fields', $display['display_options']['fields']);

// Adding the results like this.
$executable_view->result = array();
foreach ($custom_results as $custom_result) {
  $result = new array();
  $result['title'] = $custom_result['title'];
  $result['created'] = $custom_result['created'];
  $executable_view->result[] = new ResultRow($result);
}

$executable_view->build();
$executable_view->preExecute();

$render = $view->buildRenderable();
return Drupal::service('renderer')->render($render);

Now my question is: can anybody help me getting this approach working in Drupal 8? Does this approach maybe even not work anymore at all in Drupal 8? I am quite new to D8 development, so any help would be really appreciated.

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed