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

@@ -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;
}
}
}