class VisitStatistic in Dashboards with Layout Builder 8
Same name and namespace in other branches
- 2.0.x modules/dashboards_matomo/src/Plugin/Dashboard/VisitStatistic.php \Drupal\dashboards_matomo\Plugin\Dashboard\VisitStatistic
Show account info.
Plugin annotation
@Dashboard(
id = "matomo_visit_statistic",
label = @Translation("Visit report."),
category = @Translation("Matomo"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\dashboards\Plugin\DashboardBase implements ContainerFactoryPluginInterface, DashboardInterface uses StringTranslationTrait
- class \Drupal\dashboards\Plugin\DashboardLazyBuildBase implements DashboardLazyBuildInterface
- class \Drupal\dashboards_matomo\Plugin\Dashboard\MatomoBase uses ChartTrait
- class \Drupal\dashboards_matomo\Plugin\Dashboard\VisitStatistic
- class \Drupal\dashboards_matomo\Plugin\Dashboard\MatomoBase uses ChartTrait
- class \Drupal\dashboards\Plugin\DashboardLazyBuildBase implements DashboardLazyBuildInterface
- class \Drupal\dashboards\Plugin\DashboardBase implements ContainerFactoryPluginInterface, DashboardInterface uses StringTranslationTrait
Expanded class hierarchy of VisitStatistic
File
- modules/
dashboards_matomo/ src/ Plugin/ Dashboard/ VisitStatistic.php, line 17
Namespace
Drupal\dashboards_matomo\Plugin\DashboardView source
class VisitStatistic extends MatomoBase {
/**
* Get columns for query.
*
* @return array
* Return columns.
*/
protected function getChartColumns() {
return [
'nb_visits' => $this
->t('Visits'),
'avg_time_on_site' => $this
->t('Average time on site'),
'nb_uniq_visitors' => $this
->t('Unique visitors'),
'nb_actions' => $this
->t('Actions'),
'sum_visit_length' => $this
->t('Visit length summary.'),
'max_actions' => $this
->t('Max actions.'),
'nb_users' => $this
->t('Users'),
];
}
/**
* {@inheritdoc}
*/
public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration) : array {
$form = parent::buildSettingsForm($form, $form_state, $configuration);
$form['fields'] = [
'#type' => 'checkboxes',
'#options' => $this
->getChartColumns(),
'#multiple' => TRUE,
'#default_value' => $configuration['fields'],
'#title' => $this
->t('Stats to show'),
];
return $form;
}
/**
* Lazy build callback.
*
* @param \Drupal\dashboards\Plugin\DashboardBase $plugin
* Matomo base plugin.
* @param array $configuration
* Configuration.
*/
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,
],
];
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChartTrait:: |
protected | property | Empty flag. | |
ChartTrait:: |
protected | property | Labels. | |
ChartTrait:: |
protected | property | Rows. | |
ChartTrait:: |
protected | property | Chart type. | |
ChartTrait:: |
public | function | Add a label. | |
ChartTrait:: |
public | function | Add a row. | |
ChartTrait:: |
public | function | Get allowed styles. | |
ChartTrait:: |
public | function | Set all rows. | |
ChartTrait:: |
public | function | Add a label. | |
ChartTrait:: |
public | function | Set this chart is empty. | |
ChartTrait:: |
public | function | Set all labels. | |
ChartTrait:: |
public | function | Set all rows. | |
DashboardBase:: |
protected | property | Cache backend. | |
DashboardBase:: |
protected | function | Get cache for cid. | |
DashboardBase:: |
public | function | Validate settings form. | |
DashboardBase:: |
protected | function | Set a new cache entry. Cache is prefixed by pluginid. | |
DashboardBase:: |
public | function | Validate settings form. | 1 |
DashboardLazyBuildBase:: |
public | function |
Build render array. Overrides DashboardBase:: |
|
DashboardLazyBuildBase:: |
public static | function |
Helper for lazy build render. Overrides DashboardLazyBuildInterface:: |
|
MatomoBase:: |
protected | property | Entity query. | |
MatomoBase:: |
protected | function | Helper function for build rows from matomo. | |
MatomoBase:: |
public static | function |
Creates an instance of the plugin. Overrides DashboardBase:: |
|
MatomoBase:: |
public static | function | Helper function for short date ranges. | |
MatomoBase:: |
protected | function | Translate date matomo string. | |
MatomoBase:: |
public | function | Get matomo query. | |
MatomoBase:: |
protected | function | Helper function for query matomo. | |
MatomoBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides DashboardBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
VisitStatistic:: |
public | function |
Build render array. Overrides MatomoBase:: |
|
VisitStatistic:: |
protected | function | Get columns for query. | |
VisitStatistic:: |
public static | function |
Lazy build callback. Overrides DashboardLazyBuildInterface:: |