Skip to content

Commit

Permalink
VACMS-19570: Adds Section validator
Browse files Browse the repository at this point in the history
  • Loading branch information
omahane committed Nov 25, 2024
1 parent caa7906 commit 14639d4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function entityUpdate(EntityUpdateEvent $event): void {
}

/**
* Disable pathauto for Lovell nodes.
* Disable pathauto for Manila nodes.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Drupal\va_gov_manila\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;

/**
* Validates that a Manila Section choice matches the listing page choice.
*
* @Constraint(
* id = "ManilaSectionListParity",
* label = @Translation("Manila Section List Parity", context = "Validation"),
* type = "string"
* )
*/
class ManilaSectionListParity extends Constraint {

/**
* The message shown if the listing page does not match the Manila section.
*
* @var string
* @see \Drupal\va_gov_Manila\Plugin\Validation\Constraint\ManilaSectionListParityValidator
*/
public $notSectionListMatch = 'The selected option for <strong>%fieldLabel</strong> is not part of the <strong>%section</strong> section. Please choose a different option or change the section settings to match.';

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Drupal\va_gov_manila\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
* Validates the ManilaSectionListParity constraint.
*/
class ManilaSectionListParityValidator extends ConstraintValidator {

/**
* The Manila VA system Section id.
*
* @var int
*/
protected $manilaVaSystemId = '1187';

/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
// This validator only applies to events, stories and news releases.
// This validator is attached to the field_listing field.
// Check to see if the section for the current entity is a Manila section.
// If it is, load the listing page referenced by field_listing.
// If the section for the listing page doesn't match throw an error.
$entity = $items->getEntity();
$sectionTermID = $entity->field_administration->target_id;
$fieldLabel = $items->getFieldDefinition()->getLabel();
if ($sectionTermID === $this->manilaVaSystemId) {
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$sectionName = $term_storage->load($sectionTermID)->getName();
foreach ($items as $item) {
/** @var \Drupal\va_gov_manila\Plugin\Validation\Constraint\ManilaSectionListParity $constraint */
$listPage = $node_storage->load($item->target_id);
if ($listPage->field_administration->target_id !== $sectionTermID) {
$this->context->addViolation($constraint->notSectionListMatch, [
'%section' => $sectionName,
'%fieldLabel' => $fieldLabel,
]);
return;
}
}
}
}

}
26 changes: 26 additions & 0 deletions docroot/modules/custom/va_gov_manila/va_gov_manila.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* @file
* Contains va_gov_manila.module.
*/

use Drupal\Core\Entity\EntityTypeInterface;

/**
* Implements hook_entity_bundle_field_info_alter().
*/
function va_gov_manila_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
$list_item_bundles = [
'event',
'news_story',
'press_release',
];
if (($entity_type->id() === 'node')
&& (in_array($bundle, $list_item_bundles))
&& (isset($fields['field_listing']))
&& (isset($fields['field_administration']))) {
// Limit the listing selection based on section.
$fields['field_listing']->addConstraint('ManilaSectionListParity');
}
}

0 comments on commit 14639d4

Please sign in to comment.