Skip to content

Commit

Permalink
VACMS-17177: Shows descriptions for vet center services on node view/…
Browse files Browse the repository at this point in the history
…edit (#17267)

* VACMS-17177: Adds functionality for descriptions to be shown on vet center services

* VACMS-17177: Remove extraneous apostrophe

Co-authored-by: Christian Burk <omahane@gmail.com>

---------

Co-authored-by: Christian Burk <omahane@gmail.com>
  • Loading branch information
Becapa and omahane authored Feb 27, 2024
1 parent 19ff302 commit 924c3ee
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -84,21 +94,25 @@ 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,
EntityFieldManager $entity_field_manager,
EntityTypeManager $entityTypeManager,
MessengerInterface $messenger,
TranslationInterface $string_translation,
UserPermsService $user_perms_service
UserPermsService $user_perms_service,
RendererInterface $renderer
) {
$this->currentUser = $currentUser;
$this->entityTypeManager = $entityTypeManager;
$this->entityFieldManager = $entity_field_manager;
$this->messenger = $messenger;
$this->stringTranslation = $string_translation;
$this->userPermsService = $user_perms_service;
$this->renderer = $renderer;
}

/**
Expand All @@ -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(
'<div><strong>Notice: The national service description was not found.</strong></div>',
[]);
}
$formatted_markup = new FormattableMarkup($description, []);
$build['field_service_name_and_descripti']['#suffix'] = $formatted_markup;
}

/**
* Creates custom render array from field on referenced entity.
*
Expand Down Expand Up @@ -444,6 +503,16 @@ public function alterVbaFacilityServiceNodeForm(FormIdAlterEvent $event): void {
$this->buildHealthServicesDescriptionArrayAddToSettings($event);
}

/**
* Alterations to the Vet Center facility service node form.
*
* @param \Drupal\core_event_dispatcher\Event\Form\FormIdAlterEvent $event
* The event.
*/
public function alterVetCenterServiceNodeForm(FormIdAlterEvent $event): void {
$this->buildHealthServicesDescriptionArrayAddToSettings($event);
}

/**
* Builds an array of descriptions from health services available on form.
*
Expand Down Expand Up @@ -552,6 +621,7 @@ public function getVaServicesTaxonomyFieldNames(string $node_type) : array {
];
break;

case 'vet_center_facility_health_servi':
case 'vet_center':
$vaServicesFields = [
'type' => 'field_vet_center_type_of_care',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ services:
- '@messenger'
- '@string_translation'
- '@va_gov_user.user_perms'
- '@renderer'
tags:
- { name: event_subscriber }

0 comments on commit 924c3ee

Please sign in to comment.