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:
module:
- farm_agrikern
id: usage_substance
label: SubstanceUsage
description: 'Substance Usage Asset'
name_pattern: 'SubstanceUsage asset [asset:id]'
id: batch_usage
label: BatchUsage
description: 'Batch Usage Asset'
name_pattern: 'BatchUsage asset [asset:id]'
workflow: asset_default
new_revision: true

View File

@@ -4,7 +4,7 @@ dependencies:
enforced:
module:
- farm_agrikern
id: usage_machine
id: machine_usage
label: MachineUsage
description: 'Machine Usage Asset'
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' => [
'type' => 'entity_reference',
'label' => $this->t('Substance'),
'description' => $this->t('What substance was added?'),
'description' => $this->t('What substance doas this batch track?'),
'target_type' => 'asset',
'target_bundle' => 'substance',
'multiple' => FALSE,

View File

@@ -5,14 +5,14 @@ namespace Drupal\farm_agrikern\Plugin\Asset\AssetType;
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
/**
* Provides the usage_substance asset type.
* Provides the batch usage asset type.
*
* @AssetType(
* id = "usage_substance",
* label = @Translation("Substance Usage"),
* id = "batch_usage",
* label = @Translation("Batch Usage"),
* )
*/
class SubstanceUsage extends Usage {
class BatchUsage extends Usage {
/**
* {@inheritdoc}
*/
@@ -22,7 +22,7 @@ class SubstanceUsage extends Usage {
'batch_field' => [
'type' => 'entity_reference',
'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_bundle' => 'batch',
'multiple' => FALSE,

View File

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

View File

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

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?'),
'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) {
$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

@@ -29,24 +29,21 @@ class FieldWork extends WorkLog {
'required' => TRUE,
'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) {
$fields[$name] = \Drupal::service('farm_field.factory')->bundleFieldDefinition($info);
}
$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');
$fields['machine_usages_field']->addConstraint('AgriKernFieldWorkMachines');
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;
}
}
}