Refactor hook_farm_metrics to use a 'key' to identify arrays of metrics.

Without this, the 'farm_metrics.json' endpoint returns an array indexed by element ID, which would be more difficult for consumers of the 'farm_metrics.json' API to parse.
This commit is contained in:
Paul Weidner 2020-01-15 14:08:00 -08:00 committed by Michael Stenta
parent ec82d5d9d6
commit b7cb769191
3 changed files with 15 additions and 12 deletions

View File

@ -15,7 +15,7 @@ function farm_area_farm_metrics() {
// Count the number of areas.
$areas = db_query("SELECT COUNT(tid) FROM {taxonomy_term_data} t LEFT JOIN {taxonomy_vocabulary} v ON t.vid = v.vid WHERE v.machine_name = 'farm_areas'")->fetchField();
if (!empty($areas)) {
$metrics[] = array(
$metrics['areas'] = array(
'label' => t('Areas'),
'value' => $areas,
'link' => 'farm/areas',
@ -51,7 +51,7 @@ function farm_area_farm_metrics() {
}
// Add a metric.
$metrics[] = array(
$metrics[$area_type] = array(
'label' => t('@area_type area', array('@area_type' => $label)),
'value' => $total_area,
'link' => 'farm/areas',

View File

@ -25,22 +25,25 @@
* Defines farm metrics.
*
* @return array
* Returns an array of metrics. Each should
* be an array containing the following keys:
* Returns an array of farm metrics. The key should be a unique metric
* name, and each should be an array of metric values including the
* following keys:
* - label: Translated metric label.
* - value: The metric's value.
* - link: A path to link the value to.
* - weight: Weight for ordering (optional - defaults to alphabetical).
*/
function hook_farm_metrics() {
return array(
array(
'label' => t('Example'),
'value' => '100',
'link' => 'farm/example',
'weight' => -10,
),
$metrics = array();
$metrics['example'] = array(
'label' => t('Example'),
'value' => '100',
'link' => 'farm/example',
'weight' => -10,
);
return $metrics;
}
/**

View File

@ -35,7 +35,7 @@ function farm_ui_farm_metrics() {
}
// Build a metric.
$metrics[] = array(
$metrics[$type] = array(
'label' => $info['label_plural'],
'value' => $count,
'link' => farm_ui_view_page_path($info['view']),