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

Use default quantity type in QuickQuantityTrait.

This removes the dependency on farm_quantity_standard.
This commit is contained in:
Michael Stenta 2022-11-22 06:46:05 -05:00
parent 0825dc7a50
commit 6c5f42b98f
8 changed files with 44 additions and 7 deletions

View file

@ -6,5 +6,6 @@ core_version_requirement: ^9
dependencies:
- drupal:taxonomy
- farm:asset
- farm:farm_quantity_standard
- farm:farm_log_quantity
- farm:quantity
- log:log

View file

@ -52,7 +52,7 @@ trait QuickLogTrait {
// If the quantity is an array of values, pass it to createQuantity.
if (is_array($qty)) {
$log->quantity[] = $this->createQuantity($qty);
$log->quantity[] = $this->createQuantity($qty, $log->bundle());
}
// Otherwise, add it directly to the log.

View file

@ -18,20 +18,24 @@ trait QuickQuantityTrait {
*
* @param array $values
* An array of values to initialize the quantity with.
* @param string|null $log_type
* Optionally specify the log type this quantity will be added to. This is
* used to automatically determine what the default quantity type of the
* log should be.
*
* @return \Drupal\quantity\Entity\QuantityInterface
* The quantity entity that was created.
*/
protected function createQuantity(array $values = []) {
protected function createQuantity(array $values = [], ?string $log_type = NULL) {
// Trim the quantity label to 255 characters.
if (!empty($values['label'])) {
$values['label'] = $this->trimString($values['label'], 255);
}
// If a type isn't set, default to "standard".
// If a type isn't set, get the default type.
if (empty($values['type'])) {
$values['type'] = 'standard';
$values['type'] = farm_log_quantity_default_type($log_type);
}
// Split value into numerator and denominator, if it isn't already.

View file

@ -6,3 +6,6 @@ description: ''
name_pattern: 'Test log [log:id]'
workflow: log_default
new_revision: true
third_party_settings:
farm_log_quantity:
default_quantity_type: test

View file

@ -0,0 +1,11 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_quick_test
id: test
label: Test
default_measure: ''
description: 'Test quantity type.'
new_revision: true

View file

@ -0,0 +1,11 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_quick_test
id: test2
label: Test2
default_measure: ''
description: 'Test2 quantity type.'
new_revision: true

View file

@ -76,6 +76,7 @@ class Test extends QuickFormBase {
'value' => $value,
'units' => 'tests',
'label' => $this->t('test label'),
'type' => 'test2',
]);
// Create a term.

View file

@ -24,7 +24,9 @@ class QuickFormTest extends KernelTestBase {
*/
protected static $modules = [
'asset',
'farm_quantity_standard',
'entity_reference_revisions',
'farm_field',
'farm_log_quantity',
'farm_quick',
'farm_quick_test',
'farm_unit',
@ -102,8 +104,12 @@ class QuickFormTest extends KernelTestBase {
// Confirm that the log is linked to the quick form.
$this->assertEquals('test', $storage['logs'][0]->quick[0]);
// Confirm that a quantity was created.
// Confirm that the log's quantity type is test.
$this->assertEquals('test', $storage['logs'][0]->get('quantity')->referencedEntities()[0]->bundle());
// Confirm that a quantity was created and its type is test2.
$this->assertNotEmpty($storage['quantities'][0]->id());
$this->assertEquals('test2', $storage['quantities'][0]->bundle());
// Confirm that three terms were created or loaded.
$this->assertEquals(3, count($storage['terms']));