function example_callback in Views Layout 8
Callback for skipped regions.
Parameters
$plugin_id: The id of the layout being rendered.
$region: The name of the skipped region being rendered.
$view: The view being rendered.
Return value
array A render array for the skipped region.
File
- ./
views_layout.api.php, line 25 - Documentation for Views Layout API.
Code
function example_callback($plugin_id, $region, ViewExecutable $view) {
// Find two distinct random nodes and store in a static variable.
$random =& drupal_static(__FUNCTION__, array());
if (empty($random)) {
$query = \Drupal::service('entity.query');
$ids = $query
->get('node')
->condition('type', 'article')
->condition('status', 1)
->execute();
$ids = array_values($ids);
shuffle($ids);
$random = array_slice($ids, 0, 2);
}
// Render the random values in the skipped regions.
switch ($region) {
case 'three':
$id = $random[0];
break;
case 'seven':
$id = $random[1];
break;
}
$view_builder = \Drupal::entityTypeManager()
->getViewBuilder('node');
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load($id);
return $view_builder
->view($node, 'teaser');
}