You are here

public static function VisitStatistic::lazyBuild in Dashboards with Layout Builder 8

Same name and namespace in other branches
  1. 2.0.x modules/dashboards_matomo/src/Plugin/Dashboard/VisitStatistic.php \Drupal\dashboards_matomo\Plugin\Dashboard\VisitStatistic::lazyBuild()

Lazy build callback.

Parameters

\Drupal\dashboards\Plugin\DashboardBase $plugin: Matomo base plugin.

array $configuration: Configuration.

Overrides DashboardLazyBuildInterface::lazyBuild

File

modules/dashboards_matomo/src/Plugin/Dashboard/VisitStatistic.php, line 60

Class

VisitStatistic
Show account info.

Namespace

Drupal\dashboards_matomo\Plugin\Dashboard

Code

public static function lazyBuild(DashboardBase $plugin, array $configuration) : array {
  try {
    $fields = array_values($configuration['fields']);
    $fields = array_filter($fields);
    $response = $plugin
      ->query('VisitsSummary.get', [
      'filter_limit' => 20,
      'period' => $configuration['period'],
      'date' => $plugin
        ->getDateTranslated($configuration['date']),
      'flat' => 1,
      'columns' => $fields,
    ]);
    $rows = [];
    foreach ($response as $date => $row) {
      if (!isset($row[0])) {
        $r = [
          $date,
        ];
        foreach ($fields as $field) {
          $r[] = 0;
        }
        continue;
      }
      $r = [
        $date,
      ];
      foreach ($fields as $field) {
        $r[] = $row[0][$field];
      }
      $rows[] = $r;
    }
    $plugin
      ->setRows($rows);
    $plugin
      ->setChartType('line');
    $labelsFields = $plugin
      ->getChartColumns();
    $labels = [
      $plugin
        ->t('Period'),
    ];
    foreach ($fields as $field) {
      $labels[] = $labelsFields[$field];
    }
    $plugin
      ->setLabels($labels);
    $plugin
      ->setChartType($configuration['chart_type']);
    return $plugin
      ->renderChart($configuration);
  } catch (\Exception $ex) {
    return [
      '#markup' => $plugin
        ->t('Error occured: @error', [
        '@error' => $ex
          ->getMessage(),
      ]),
      '#cache' => [
        'max-age' => 0,
      ],
    ];
  }
}