Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VACMS-17177: Shows descriptions for vet center services on node view/edit #17267

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 it's title on node:view.
Becapa marked this conversation as resolved.
Show resolved Hide resolved
*
* @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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question (for my own edification): Why does this need to be called via reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that the reason for this is because we are making changes to the build. If we didn't call it by reference, then we'd just be getting a copy of the build and any changes we make to it below (line 178) wouldn't be reflected in the actual build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

$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 }
Loading