-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...modules/custom/va_gov_manila/src/Plugin/Validation/Constraint/ManilaSectionListParity.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'; | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...ustom/va_gov_manila/src/Plugin/Validation/Constraint/ManilaSectionListParityValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |