added batch work log

This commit is contained in:
2024-12-10 16:50:53 +01:00
parent 7ee75dd7f3
commit 5dc329d416
12 changed files with 179 additions and 64 deletions

View File

@@ -4,9 +4,9 @@ dependencies:
enforced: enforced:
module: module:
- farm_agrikern - farm_agrikern
id: usage_substance id: batch_usage
label: SubstanceUsage label: BatchUsage
description: 'Substance Usage Asset' description: 'Batch Usage Asset'
name_pattern: 'SubstanceUsage asset [asset:id]' name_pattern: 'BatchUsage asset [asset:id]'
workflow: asset_default workflow: asset_default
new_revision: true new_revision: true

View File

@@ -4,7 +4,7 @@ dependencies:
enforced: enforced:
module: module:
- farm_agrikern - farm_agrikern
id: usage_machine id: machine_usage
label: MachineUsage label: MachineUsage
description: 'Machine Usage Asset' description: 'Machine Usage Asset'
name_pattern: 'MachineUsage asset [asset:id]' name_pattern: 'MachineUsage asset [asset:id]'

View File

@@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_agrikern
id: batch_work
label: BatchWork
description: 'Batch work log'
name_pattern: 'Batch work log [log:id]'
workflow: farm_log_workflow
new_revision: true

View File

@@ -22,7 +22,7 @@ class Batch extends FarmAssetType {
'substance_field' => [ 'substance_field' => [
'type' => 'entity_reference', 'type' => 'entity_reference',
'label' => $this->t('Substance'), 'label' => $this->t('Substance'),
'description' => $this->t('What substance was added?'), 'description' => $this->t('What substance doas this batch track?'),
'target_type' => 'asset', 'target_type' => 'asset',
'target_bundle' => 'substance', 'target_bundle' => 'substance',
'multiple' => FALSE, 'multiple' => FALSE,

View File

@@ -5,14 +5,14 @@ namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType; use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/** /**
* Provides the usage_substance asset type. * Provides the batch usage asset type.
* *
* @AssetType( * @AssetType(
* id = "usage_substance", * id = "batch_usage",
* label = @Translation("Substance Usage"), * label = @Translation("Batch Usage"),
* ) * )
*/ */
class SubstanceUsage extends Usage { class BatchUsage extends Usage {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@@ -22,7 +22,7 @@ class SubstanceUsage extends Usage {
'batch_field' => [ 'batch_field' => [
'type' => 'entity_reference', 'type' => 'entity_reference',
'label' => $this->t('Batch'), 'label' => $this->t('Batch'),
'description' => $this->t('What batch of the substance was used?'), 'description' => $this->t('What batch was used?'),
'target_type' => 'asset', 'target_type' => 'asset',
'target_bundle' => 'batch', 'target_bundle' => 'batch',
'multiple' => FALSE, 'multiple' => FALSE,

View File

@@ -8,7 +8,7 @@ use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
* Provides the machineusage asset type. * Provides the machineusage asset type.
* *
* @AssetType( * @AssetType(
* id = "usage_machine", * id = "machine_usage",
* label = @Translation("Machine Usage"), * label = @Translation("Machine Usage"),
* ) * )
*/ */

View File

@@ -18,7 +18,9 @@ class Substance extends FarmAssetType {
*/ */
public function buildFieldDefinitions() { public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions(); $fields = parent::buildFieldDefinitions();
$options = [
$field_info = [
'ingredients_field' => [
'type' => 'entity_reference', 'type' => 'entity_reference',
'label' => $this->t('Ingredients'), 'label' => $this->t('Ingredients'),
'description' => $this->t('What ingredients are in the substance?'), 'description' => $this->t('What ingredients are in the substance?'),
@@ -26,11 +28,8 @@ class Substance extends FarmAssetType {
'target_bundle' => 'ingredient', 'target_bundle' => 'ingredient',
'multiple' => TRUE, 'multiple' => TRUE,
'required' => FALSE, 'required' => FALSE,
]; ],
$fields['ingredients_field'] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options) 'unit_field' => [
->addConstraint('AgriKernSubstanceIngredients');
$options = [
'type' => 'entity_reference', 'type' => 'entity_reference',
'label' => $this->t('Unit'), 'label' => $this->t('Unit'),
'description' => $this->t('What unit is the substance measured in?'), 'description' => $this->t('What unit is the substance measured in?'),
@@ -39,8 +38,15 @@ class Substance extends FarmAssetType {
'auto_create' => TRUE, 'auto_create' => TRUE,
'multiple' => FALSE, 'multiple' => FALSE,
'required' => TRUE, 'required' => TRUE,
],
]; ];
$fields['unit_field'] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options);
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
$fields['ingredients_field']->addConstraint('AgriKernSubstanceIngredients');
return $fields; return $fields;
} }

View File

@@ -20,13 +20,6 @@ class Usage extends FarmAssetType {
'description' => $this->t('How much was the quantiy used in the quantities unit of measurement?'), 'description' => $this->t('How much was the quantiy used in the quantities unit of measurement?'),
'required' => TRUE, 'required' => TRUE,
], ],
// This field is automatically filled using LogEventSubscriber
'cost_field' => [
'type' => 'fraction',
'label' => $this->t('Cost'),
'description' => $this->t('THIS FIELD IS GENERATED AUTOMATICALLY AND SHOULD NOT BE ADJUSTED!'),
'required' => FALSE,
],
]; ];
foreach ($field_info as $name => $info) { foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info); $fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);

View File

@@ -0,0 +1,42 @@
<?php
namespace Drupal\farm_agrikern\Plugin\Log\LogType;
use Drupal\farm_entity\Plugin\Log\LogType\FarmLogType;
/**
* Provides the batch work log type.
*
* @LogType(
* id = "batch_work",
* label = @Translation("Batch work"),
* )
*/
class BatchWork extends FieldWork {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
$field_info = [
'batch_usages_field' => [
'type' => 'entity_reference',
'label' => $this->t('Batch usages'),
'description' => $this->t('What batches were used and how much?'),
'target_type' => 'asset',
'target_bundle' => 'batch_usage',
'multiple' => TRUE,
'required' => TRUE,
],
];
foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
$fields['batch_usages_field']->addConstraint('AgriKernBatchWorkBatches');
return $fields;
}
}

View File

@@ -5,13 +5,13 @@ namespace Drupal\farm_agrikern\Plugin\Log\LogType;
use Drupal\farm_entity\Plugin\Log\LogType\FarmLogType; use Drupal\farm_entity\Plugin\Log\LogType\FarmLogType;
/** /**
* Provides the field work log type. * Provides the field work log type.
* *
* @LogType( * @LogType(
* id = "field_work", * id = "field_work",
* label = @Translation("Field work"), * label = @Translation("Field work"),
* ) * )
*/ */
class FieldWork extends WorkLog { class FieldWork extends WorkLog {
/** /**
* {@inheritdoc} * {@inheritdoc}
@@ -29,24 +29,21 @@ class FieldWork extends WorkLog {
'required' => TRUE, 'required' => TRUE,
'multiple' => FALSE, 'multiple' => FALSE,
], ],
'machine_usages_field' => [
'type' => 'entity_reference',
'label' => $this->t('Machine usages'),
'description' => $this->t('What machines were used and how much?'),
'target_type' => 'asset',
'target_bundle' => 'machine_usage',
'multiple' => TRUE,
'required' => TRUE,
],
]; ];
foreach ($field_info as $name => $info) { foreach ($field_info as $name => $info) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info); $fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
} }
$fields['machine_usages_field']->addConstraint('AgriKernFieldWorkMachines');
$options = [
'type' => 'entity_reference',
'label' => $this->t('Machine usages'),
'description' => $this->t('What machines were used and how much?'),
'target_type' => 'asset',
'target_bundle' => 'usage_machine',
'multiple' => TRUE,
'required' => TRUE,
];
$fields['machine_usages_field'] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($options)
->addConstraint('AgriKernFieldWorkMachines');
return $fields; return $fields;
} }

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Provides a BatchWorkBatches constraint.
*
* @Constraint(
* id = "AgriKernBatchWorkBatches",
* label = @Translation("BatchWorkBatches", context = "Validation"),
* )
*
* @DCG
* To apply this constraint on third party entity types implement either
* hook_entity_base_field_info_alter() or hook_entity_bundle_field_info_alter().
*
* @see https://www.drupal.org/node/2015723
*/
final class BatchWorkBatchesConstraint extends Constraint {
public string $notUnique = 'Some batches were entered twice.';
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
use Drupal\Core\Field\FieldItemListInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the BatchWorkBatches constraint.
*/
final class BatchWorkBatchesConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate(mixed $items, Constraint $constraint): void {
if (!$items instanceof FieldItemListInterface) {
throw new \InvalidArgumentException(
sprintf('The validated value must be instance of \Drupal\Core\Field\FieldItemListInterface, %s was given.', get_debug_type($items))
);
}
$seen_batches = [];
foreach ($items as $delta => $item) {
$batch_usage = \Drupal::entityTypeManager()->getStorage('asset')->load($item->target_id);
$batch_id = $batch_usage->batch_field->target_id;
if (isset($seen_batches[$batch_id])) {
$this->context->buildViolation($constraint->notUnique)
->atPath($delta)
->addViolation();
}
$seen_batches[$batch_id] = true;
}
}
}