3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Add an Inventory field formatter and display current inventory on asset pages.

This commit is contained in:
Michael Stenta 2021-03-27 14:41:45 -04:00
parent 54ddd93277
commit b5f5a1da8f
2 changed files with 50 additions and 0 deletions

View file

@ -666,6 +666,12 @@ class FarmFieldFactory implements FarmFieldFactoryInterface {
*/
protected function modifyInventoryField(BaseFieldDefinition &$field, array $options = []) {
// Build view display settings.
$field->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'inventory',
'weight' => $options['weight']['view'] ?? 0,
]);
}
/**

View file

@ -0,0 +1,44 @@
<?php
namespace Drupal\farm_inventory\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Plugin implementation of the 'inventory' formatter.
*
* @FieldFormatter(
* id = "inventory",
* label = @Translation("Inventory"),
* field_types = {
* "inventory"
* }
* )
*/
class InventoryFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$summary = $item->value;
if (!empty($item->units)) {
$summary .= ' ' . $item->units;
}
if (!empty($item->measure)) {
$measures = quantity_measures();
if (!empty($measures[$item->measure]['label'])) {
$summary .= ' (' . $measures[$item->measure]['label'] . ')';
}
}
$elements[$delta]['value'] = ['#markup' => $summary];
}
return $elements;
}
}