TopUrls.php in Dashboards with Layout Builder 8
File
modules/dashboards_matomo/src/Plugin/Dashboard/TopUrls.php
View source
<?php
namespace Drupal\dashboards_matomo\Plugin\Dashboard;
use Drupal\Core\Form\FormStateInterface;
use Drupal\dashboards\Plugin\DashboardBase;
class TopUrls extends MatomoBase {
public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration) : array {
$form = parent::buildSettingsForm($form, $form_state, $configuration);
$form['chart_type']['#access'] = FALSE;
return $form;
}
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,
],
];
}
}
}