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. // 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(); $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)) { if (!empty($areas)) {
$metrics[] = array( $metrics['areas'] = array(
'label' => t('Areas'), 'label' => t('Areas'),
'value' => $areas, 'value' => $areas,
'link' => 'farm/areas', 'link' => 'farm/areas',
@ -51,7 +51,7 @@ function farm_area_farm_metrics() {
} }
// Add a metric. // Add a metric.
$metrics[] = array( $metrics[$area_type] = array(
'label' => t('@area_type area', array('@area_type' => $label)), 'label' => t('@area_type area', array('@area_type' => $label)),
'value' => $total_area, 'value' => $total_area,
'link' => 'farm/areas', 'link' => 'farm/areas',

View File

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

View File

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