You are here

public function FarmMetricsBlock::build in farmOS 2.x

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

modules/core/ui/metrics/src/Plugin/Block/FarmMetricsBlock.php, line 72

Class

FarmMetricsBlock
Provides a 'Metrics' block.

Namespace

Drupal\farm_ui_metrics\Plugin\Block

Code

public function build() {
  $output = [];

  // Create a container for asset metrics.
  $output['asset'] = [
    '#markup' => '<strong>' . Link::createFromRoute('Assets', 'view.farm_asset.page')
      ->toString() . '</strong>',
    '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'] = '<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.
  $output['log'] = [
    '#markup' => '<strong>' . Link::createFromRoute('Logs', 'view.farm_log.page')
      ->toString() . '</strong>',
    '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'] = '<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';

  // Return the output.
  return $output;
}