Render metrics using item_list theme

This commit is contained in:
Paul Weidner 2023-09-06 17:34:40 -07:00 committed by Michael Stenta
parent 91a6fccf85
commit 4ef19c79ce
2 changed files with 33 additions and 49 deletions

View File

@ -1,5 +1,4 @@
.metrics-container {
margin-top: .5em;
.metrics-container ul {
display: flex;
flex-flow: row wrap;
gap: .25em .5em;
@ -7,6 +6,5 @@
.metrics-container a.metric {
flex-basis: 20%;
margin: 0;
white-space: nowrap;
}

View File

@ -72,55 +72,41 @@ class FarmMetricsBlock extends BlockBase implements ContainerFactoryPluginInterf
public function build() {
$output = [];
// Create a container for asset metrics.
$assets_label = $this->entityTypeManager->getStorage('asset')->getEntityType()->getCollectionLabel();
$output['asset']['label'] = [
'#type' => 'html_tag',
'#tag' => 'h6',
'#value' => Link::createFromRoute($assets_label, 'view.farm_asset.page')->toString() . ' (' . $this->t('active') . ')',
];
$output['asset']['metrics'] = [
'#type' => 'container',
'#attributes' => [
// Create a list of asset metrics.
$assets_label = $this->entityTypeManager->getStorage('asset')->getEntityType()->getCollectionLabel() . ' (' . $this->t('active') . ')';
$output['asset'] = [
'#theme' => 'item_list',
'#title' => Link::createFromRoute($assets_label, 'view.farm_asset.page')->toRenderable(),
'#items' => $this->getEntityMetrics('asset'),
'#empty' => $this->t('No assets found.'),
'#wrapper_attributes' => [
'class' => ['assets', 'metrics-container'],
],
];
$metrics = $this->getEntityMetrics('asset');
foreach ($metrics as $metric) {
$output['asset']['metrics'][] = [
'#markup' => $metric,
];
}
if (empty($metrics)) {
$output['asset']['metrics']['empty']['#markup'] = '<p>' . $this->t('No assets found.') . '</p>';
}
$output['#cache']['tags'][] = 'asset_list';
$output['#cache']['tags'][] = 'config:asset_type_list';
// Create a section for log metrics.
$logs_label = $this->entityTypeManager->getStorage('log')->getEntityType()->getCollectionLabel();
$output['log']['label'] = [
'#type' => 'html_tag',
'#tag' => 'h6',
'#value' => Link::createFromRoute($logs_label, 'view.farm_log.page')->toString(),
];
$output['log']['metrics'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['logs', 'metrics-container'],
'#cache' => [
'tags' => [
'asset_list',
'config:asset_type_list',
],
],
];
// Create a list of log metrics.
$logs_label = $this->entityTypeManager->getStorage('log')->getEntityType()->getCollectionLabel();
$output['log'] = [
'#theme' => 'item_list',
'#title' => Link::createFromRoute($logs_label, 'view.farm_log.page')->toRenderable(),
'#items' => $this->getEntityMetrics('log'),
'#empty' => $this->t('No logs found.'),
'#wrapper_attributes' => [
'class' => ['logs', 'metrics-container'],
],
'#cache' => [
'tags' => [
'log_list',
'config:log_type_list',
],
],
];
$metrics = $this->getEntityMetrics('log');
foreach ($metrics as $metric) {
$output['log']['metrics'][] = [
'#markup' => $metric,
];
}
if (empty($metrics)) {
$output['log']['metrics']['empty']['#markup'] = '<p>' . $this->t('No logs found.') . '</p>';
}
$output['#cache']['tags'][] = 'log_list';
$output['#cache']['tags'][] = 'config:log_type_list';
// Attach CSS.
$output['#attached']['library'][] = 'farm_ui_metrics/metrics_block';
@ -157,7 +143,7 @@ class FarmMetricsBlock extends BlockBase implements ContainerFactoryPluginInterf
$count = $query->count()->execute();
$route_name = "view.farm_$entity_type.page_type";
$metrics[] = Link::createFromRoute($bundle_info['label'] . ': ' . $count, $route_name, ['arg_0' => $bundle], ['attributes' => ['class' => ['metric', 'button', 'button--small']]])->toString();
$metrics[] = Link::createFromRoute($bundle_info['label'] . ': ' . $count, $route_name, ['arg_0' => $bundle], ['attributes' => ['class' => ['metric']]])->toRenderable();
}
return $metrics;