Allow log type to be selected.

This commit is contained in:
Michael Stenta 2023-12-08 06:45:51 -05:00
parent 74f3ee4b5d
commit 5cbd753905
2 changed files with 32 additions and 3 deletions

View File

@ -165,6 +165,26 @@ class Inventory extends QuickFormBase implements QuickFormInterface {
'#required' => TRUE,
];
// Build list of log type options.
// Limit to log types the user has access to create.
$log_access_control_handler = $this->entityTypeManager->getAccessControlHandler('log');
$log_types = array_filter($this->entityTypeManager->getStorage('log_type')->loadMultiple(), function ($log_type) use ($log_access_control_handler) {
return $log_access_control_handler->createAccess($log_type->id(), $this->currentUser);
});
$log_type_options = array_map(function ($log_type) {
return $log_type->label();
}, $log_types);
// Log type.
$form['log_type'] = [
'#type' => 'select',
'#title' => $this->t('Log type'),
'#description' => $this->t('Select the type of log to create.'),
'#options' => $log_type_options,
'#default_value' => 'observation',
'#required' => TRUE,
];
// Notes.
$form['notes'] = [
'#type' => 'details',
@ -214,7 +234,7 @@ class Inventory extends QuickFormBase implements QuickFormInterface {
$timestamp = $form_state->getValue('date')->getTimestamp();
$status = $form_state->getValue('done') ? 'done' : 'pending';
$log = [
'type' => 'observation',
'type' => $form_state->getValue('log_type'),
'timestamp' => $timestamp,
'quantity' => [$quantity],
'notes' => $form_state->getValue('notes'),

View File

@ -25,6 +25,7 @@ class QuickInventoryTest extends QuickFormTestBase {
* {@inheritdoc}
*/
protected static $modules = [
'farm_activity',
'farm_equipment',
'farm_inventory',
'farm_observation',
@ -39,6 +40,7 @@ class QuickInventoryTest extends QuickFormTestBase {
protected function setUp(): void {
parent::setUp();
$this->installConfig([
'farm_activity',
'farm_equipment',
'farm_observation',
'farm_quantity_standard',
@ -81,6 +83,7 @@ class QuickInventoryTest extends QuickFormTestBase {
'value' => 'Lorem ipsum',
'format' => 'default',
],
'log_type' => 'observation',
'done' => TRUE,
];
$this->submitQuickForm($form_values);
@ -111,7 +114,8 @@ class QuickInventoryTest extends QuickFormTestBase {
$this->assertEquals('', $inventory[0]['units']);
$this->assertEquals('', $inventory[0]['measure']);
// Programmatically submit the inventory quick form (increment by 1).
// Programmatically submit the inventory quick form (increment by 1 with an
// activity log).
$form_values = [
'date' => [
'date' => $today->format('Y-m-d'),
@ -126,6 +130,7 @@ class QuickInventoryTest extends QuickFormTestBase {
'measure' => '',
],
'inventory_adjustment' => 'increment',
'log_type' => 'activity',
'done' => TRUE,
];
$this->submitQuickForm($form_values);
@ -134,8 +139,10 @@ class QuickInventoryTest extends QuickFormTestBase {
$logs = $this->logStorage->loadMultiple();
$this->assertCount(2, $logs);
// Check that the log name was populated correctly.
// Check that the log is an activity and that the name was populated
// correctly.
$log = $logs[2];
$this->assertEquals('activity', $log->bundle());
$this->assertEquals('Increment inventory of Tractor by 1', $log->label());
// Check that the asset has a single inventory of 2.
@ -160,6 +167,7 @@ class QuickInventoryTest extends QuickFormTestBase {
'measure' => '',
],
'inventory_adjustment' => 'decrement',
'log_type' => 'observation',
'done' => TRUE,
];
$this->submitQuickForm($form_values);
@ -203,6 +211,7 @@ class QuickInventoryTest extends QuickFormTestBase {
'measure' => 'volume',
],
'inventory_adjustment' => 'reset',
'log_type' => 'observation',
'done' => TRUE,
];
$this->submitQuickForm($form_values);