removed cost calculation (do dynamically)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Provides a FieldWorkMachines constraint.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "AgriKernFieldWorkMachines",
|
||||
* label = @Translation("FieldWorkMachines", 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 FieldWorkMachinesConstraint extends Constraint {
|
||||
|
||||
public string $notUnique = 'Some machines were entered twice.';
|
||||
|
||||
}
|
||||
@@ -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 FieldWorkMachines constraint.
|
||||
*/
|
||||
final class FieldWorkMachinesConstraintValidator 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_machines = [];
|
||||
foreach ($items as $delta => $item) {
|
||||
$machine_usage = \Drupal::entityTypeManager()->getStorage('asset')->load($item->target_id);
|
||||
$machine_id = $machine_usage->machine_field->target_id;
|
||||
if (isset($seen_machines[$machine_id])) {
|
||||
$this->context->buildViolation($constraint->notUnique)
|
||||
->atPath($delta)
|
||||
->addViolation();
|
||||
}
|
||||
$seen_machines[$machine_id] = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\farm_agrikern\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Provides a SubstanceIngredients constraint.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "AgriKernSubstanceIngredients",
|
||||
* label = @Translation("SubstanceIngredients", 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 SubstanceIngredientsConstraint extends Constraint {
|
||||
|
||||
public string $notUnique = 'Some chemicals were entered twice.';
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?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 SubstanceIngredients constraint.
|
||||
* Ensures there are no duplicate Chemicals in the Ingredients of a Substance.
|
||||
*/
|
||||
final class SubstanceIngredientsConstraintValidator 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_quantities = [];
|
||||
foreach ($items as $delta => $item) {
|
||||
$ingredient = \Drupal::entityTypeManager()->getStorage('asset')->load($item->target_id);
|
||||
$chemical_id = $ingredient->chemical_field->target_id;
|
||||
if (isset($seen_quantities[$chemical_id])) {
|
||||
$this->context->buildViolation($constraint->notUnique)
|
||||
->atPath($delta)
|
||||
->addViolation();
|
||||
}
|
||||
$seen_quantities[$chemical_id] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user