Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ReportBaseController
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
1 / 1
 renderViews
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3namespace Drupal\visitors\Controller\Report;
4
5use Drupal\Core\Controller\ControllerBase;
6
7/**
8 * Report base controller.
9 */
10class ReportBaseController extends ControllerBase {
11
12  /**
13   * Render the registered blocks as output.
14   *
15   * @param array $blocks
16   *   An array of block items formatted for rendering a view.
17   * @param string|null $class
18   *   An optional class to add to the wrapper div.
19   * @param array|null $args
20   *   An optional array of arguments to pass to the view.
21   */
22  public function renderViews(array $blocks, $class = NULL, $args = NULL) {
23    $output = [];
24    // Render each block element.
25    foreach ($blocks as $block) {
26      if (empty($block['#view_id'])) {
27
28        $build = $block;
29      }
30      else {
31        $view_id = $block['#view_id'];
32        $display_id = $block['#view_display'];
33
34        if (is_null($args)) {
35          // Create a view embed for this content.
36          $build = \views_embed_view($view_id, $display_id);
37        }
38        else {
39          if (!is_array($args)) {
40            $args = [$args];
41          }
42          // Create a view embed for this content.
43          $build = \views_embed_view($view_id, $display_id, ...$args);
44        }
45
46      }
47      if (!isset($build['#attributes']) && isset($block['#attributes'])) {
48        $build['#attributes'] = $block['#attributes'];
49      }
50
51      $output[] = $build;
52    }
53    $prefix = '<div>';
54    if (!empty($class)) {
55      $prefix = '<div class="' . $class . '">';
56    }
57    return [
58      '#prefix'   => $prefix,
59      'blocks'    => $output,
60      '#suffix'   => '</div>',
61    ];
62  }
63
64}