entityTypeManager = $entity_type_manager; $this->bundleInfo = $bundle_info; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager'), $container->get('entity_type.bundle.info') ); } /** * {@inheritdoc} */ 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' => [ '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', ], ]; $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'; // Return the output. return $output; } /** * Gather metrics for rendering in the block. * * @param string $entity_type * The entity type machine name. * * @return array * Returns an array of metric information. */ protected function getEntityMetrics($entity_type) { $metrics = []; // Load bundles. $bundles = $this->bundleInfo->getBundleInfo($entity_type); // Count records by type. foreach ($bundles as $bundle => $bundle_info) { $query = $this->entityTypeManager->getStorage($entity_type)->getAggregateQuery() ->accessCheck(TRUE) ->condition('type', $bundle); // Only include active assets. if ($entity_type == 'asset') { $query->condition('status', 'active'); } $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(); } return $metrics; } }