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 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'], ], '#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', ], ], ]; // 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']]])->toRenderable(); } return $metrics; } }