Skip to content

Commit

Permalink
Save unit term label to configuration and automatically generate term…
Browse files Browse the repository at this point in the history
… on form load.
  • Loading branch information
mstenta committed Jan 31, 2024
1 parent d8d29ee commit f5d053f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Expand Up @@ -6,8 +6,8 @@ farm_quick.settings.inventory:
type: integer
label: 'Default asset ID'
units:
type: integer
label: 'Default quantity units term ID'
type: string
label: 'Default quantity units'
measure:
type: string
label: 'Default quantity measure'
Expand Down
13 changes: 9 additions & 4 deletions modules/quick/inventory/src/Plugin/QuickForm/Inventory.php
Expand Up @@ -16,6 +16,7 @@
use Drupal\farm_quick\Traits\ConfigurableQuickFormTrait;
use Drupal\farm_quick\Traits\QuickFormElementsTrait;
use Drupal\farm_quick\Traits\QuickLogTrait;
use Drupal\farm_quick\Traits\QuickTermTrait;
use Drupal\log\Entity\Log;
use Drupal\taxonomy\TermInterface;
use Psr\Container\ContainerInterface;
Expand All @@ -36,6 +37,7 @@ class Inventory extends QuickFormBase implements ConfigurableQuickFormInterface
use ConfigurableQuickFormTrait;
use QuickLogTrait;
use QuickFormElementsTrait;
use QuickTermTrait;

/**
* The entity type manager service.
Expand Down Expand Up @@ -121,7 +123,7 @@ public function defaultConfiguration() {
return [
'asset' => NULL,
'measure' => NULL,
'units' => NULL,
'units' => '',
'inventory_adjustment' => 'reset',
'log_type' => 'observation',
];
Expand Down Expand Up @@ -181,7 +183,7 @@ public function buildForm(array $form, FormStateInterface $form_state, string $i
'#size' => 16,
];
if (!empty($this->configuration['units'])) {
$form['quantity']['units']['#default_value'] = $this->entityTypeManager->getStorage('taxonomy_term')->load($this->configuration['units']);
$form['quantity']['units']['#default_value'] = $this->createOrLoadTerm($this->configuration['units'], 'unit');
}
$form['quantity']['measure'] = [
'#type' => 'select',
Expand Down Expand Up @@ -453,7 +455,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#size' => 16,
];
if (!empty($this->configuration['units'])) {
$form['units']['#default_value'] = $this->entityTypeManager->getStorage('taxonomy_term')->load($this->configuration['units']);
$form['units']['#default_value'] = $this->createOrLoadTerm($this->configuration['units'], 'unit');
}

// Measure.
Expand Down Expand Up @@ -494,7 +496,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['asset'] = $form_state->getValue('asset');
$this->configuration['units'] = $form_state->getValue('units');
$this->configuration['units'] = '';
if (!empty($form_state->getValue('units'))) {
$this->configuration['units'] = $form_state->getValue('units')['entity']->label();
}
$this->configuration['measure'] = $form_state->getValue('measure');
$this->configuration['inventory_adjustment'] = $form_state->getValue('inventory_adjustment');
$this->configuration['log_type'] = $form_state->getValue('log_type');
Expand Down

0 comments on commit f5d053f

Please sign in to comment.