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:
parent
54ddd93277
commit
b5f5a1da8f
|
@ -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,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue