You are here

public static function TopUrls::lazyBuild in Dashboards with Layout Builder 2.0.x

Same name and namespace in other branches
  1. 8 modules/dashboards_matomo/src/Plugin/Dashboard/TopUrls.php \Drupal\dashboards_matomo\Plugin\Dashboard\TopUrls::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/TopUrls.php, line 36

Class

TopUrls
Show account info.

Namespace

Drupal\dashboards_matomo\Plugin\Dashboard

Code

public static function lazyBuild(DashboardBase $plugin, array $configuration) : array {
  try {
    $response = $plugin
      ->query('Actions.getPageUrls', [
      'filter_limit' => 20,
      'period' => $configuration['period'],
      'date' => $plugin
        ->getDateTranslated($configuration['date']),
      'flat' => 1,
      'expanded' => TRUE,
    ]);
    $lists = [];
    foreach ($response as $date => $row) {
      $items = [];
      foreach ($row as $r) {
        if (empty($r)) {
          continue;
        }
        $items[] = [
          '#type' => 'inline_template',
          '#template' => '<a href="{{ url }}">{{ url }}</a>',
          '#context' => [
            'url' => isset($r['url']) ? $r['url'] : $plugin
              ->t('Unkown'),
          ],
        ];
      }
      if (!empty($items)) {
        $lists[] = [
          '#theme' => 'item_list',
          '#title' => $date,
          '#items' => $items,
        ];
      }
    }
    return $lists;
  } catch (\Exception $ex) {
    return [
      '#markup' => $plugin
        ->t('Error occured: @error', [
        '@error' => $ex
          ->getMessage(),
      ]),
      '#cache' => [
        'max-age' => 0,
      ],
    ];
  }
}