diff --git a/docroot/modules/custom/va_gov_facilities/src/EventSubscriber/FacilitiesSubscriber.php b/docroot/modules/custom/va_gov_facilities/src/EventSubscriber/FacilitiesSubscriber.php index c2f58d0a6a..6702be4f74 100644 --- a/docroot/modules/custom/va_gov_facilities/src/EventSubscriber/FacilitiesSubscriber.php +++ b/docroot/modules/custom/va_gov_facilities/src/EventSubscriber/FacilitiesSubscriber.php @@ -10,12 +10,14 @@ use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; +use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\core_event_dispatcher\EntityHookEvents; use Drupal\core_event_dispatcher\Event\Entity\EntityPresaveEvent; use Drupal\core_event_dispatcher\Event\Entity\EntityUpdateEvent; +use Drupal\core_event_dispatcher\Event\Entity\EntityViewAlterEvent; use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent; use Drupal\core_event_dispatcher\Event\Form\FormIdAlterEvent; use Drupal\core_event_dispatcher\FormHookEvents; @@ -69,6 +71,14 @@ class FacilitiesSubscriber implements EventSubscriberInterface { */ protected $userPermsService; + /** + * The renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + * The renderer. + */ + private $renderer; + /** * Constructs the EventSubscriber object. * @@ -84,6 +94,8 @@ class FacilitiesSubscriber implements EventSubscriberInterface { * The string translation service. * @param \Drupal\va_gov_user\Service\UserPermsService $user_perms_service * The user perms service. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer service. */ public function __construct( AccountInterface $currentUser, @@ -91,7 +103,8 @@ public function __construct( EntityTypeManager $entityTypeManager, MessengerInterface $messenger, TranslationInterface $string_translation, - UserPermsService $user_perms_service + UserPermsService $user_perms_service, + RendererInterface $renderer ) { $this->currentUser = $currentUser; $this->entityTypeManager = $entityTypeManager; @@ -99,6 +112,7 @@ public function __construct( $this->messenger = $messenger; $this->stringTranslation = $string_translation; $this->userPermsService = $user_perms_service; + $this->renderer = $renderer; } /** @@ -112,13 +126,58 @@ public static function getSubscribedEvents(): array { 'hook_event_dispatcher.form_node_vet_center_form.alter' => 'alterVetCenterNodeForm', 'hook_event_dispatcher.form_node_vba_facility_service_edit_form.alter' => 'alterVbaFacilityServiceNodeForm', 'hook_event_dispatcher.form_node_vba_facility_service_form.alter' => 'alterVbaFacilityServiceNodeForm', + 'hook_event_dispatcher.form_node_vet_center_facility_health_servi_edit_form.alter' => 'alterVetCenterServiceNodeForm', EntityHookEvents::ENTITY_PRE_SAVE => 'entityPresave', EntityHookEvents::ENTITY_UPDATE => 'entityUpdate', + EntityHookEvents::ENTITY_VIEW_ALTER => 'entityViewAlter', FieldHookEvents::WIDGET_COMPLETE_FORM_ALTER => 'widgetCompleteFormAlter', FormHookEvents::FORM_ALTER => 'formAlter', ]; } + /** + * Alteration to entity view pages. + * + * @param \Drupal\core_event_dispatcher\Event\Entity\EntityViewAlterEvent $event + * The entity view alter service. + */ + public function entityViewAlter(EntityViewAlterEvent $event):void { + $this->appendServiceDescriptionToVetCenterFacilityService($event); + } + + /** + * Appends Vet Center facility service description to its title on node:view. + * + * @param \Drupal\core_event_dispatcher\Event\Entity\EntityViewAlterEvent $event + * The entity view alter service. + */ + public function appendServiceDescriptionToVetCenterFacilityService(EntityViewAlterEvent $event):void { + if (($event->getDisplay()->getTargetBundle() !== 'vet_center_facility_health_servi') + || ($event->getDisplay()->getOriginalMode() !== 'full')) { + return; + } + $build = &$event->getBuild(); + $service_node = $event->getEntity(); + $referenced_terms = $service_node->get('field_service_name_and_descripti')->referencedEntities(); + // Render the national service term description (if available). + if (!empty($referenced_terms)) { + $description = ""; + $referenced_term = reset($referenced_terms); + if ($referenced_term) { + $view_builder = $this->entityTypeManager->getViewBuilder('taxonomy_term'); + $referenced_term_content = $view_builder->view($referenced_term, 'vet_center_service'); + $description = $this->renderer->renderRoot($referenced_term_content); + } + } + else { + $description = new FormattableMarkup( + '