From 4ef19c79ce9fe62962b2321a5214c5a29123f3a8 Mon Sep 17 00:00:00 2001 From: Paul Weidner Date: Wed, 6 Sep 2023 17:34:40 -0700 Subject: [PATCH] Render metrics using item_list theme --- modules/core/ui/metrics/css/metrics_block.css | 4 +- .../src/Plugin/Block/FarmMetricsBlock.php | 78 ++++++++----------- 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/modules/core/ui/metrics/css/metrics_block.css b/modules/core/ui/metrics/css/metrics_block.css index 5b30d9f9f..cacd8aade 100644 --- a/modules/core/ui/metrics/css/metrics_block.css +++ b/modules/core/ui/metrics/css/metrics_block.css @@ -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; } diff --git a/modules/core/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php b/modules/core/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php index 4c4bf92dd..0d0311030 100644 --- a/modules/core/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php +++ b/modules/core/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php @@ -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'] = '

' . $this->t('No assets found.') . '

'; - } - $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'] = '

' . $this->t('No logs found.') . '

'; - } - $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;