Skip to content

Commit

Permalink
OpenAPI UI Hook to Event Subscriber (#16552)
Browse files Browse the repository at this point in the history
* convert hook to event subscriber

* correct doc block

* remove libraries definition

* convert one event subscriber to another

* remove composer requirements no longer needed

---------

Co-authored-by: Tanner Heffner <[email protected]>
  • Loading branch information
alexfinnarn and tjheffner authored Jan 2, 2024
1 parent d2d3982 commit 73a8a63
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

rows.forEach(function(row) {
// Get the first cell (td) of the row, which contains the schema label.
const firstCell = row.cells[0];
const schemaLabel = row.cells[0];

// Remove all but 'VA.gov JSON:API'.
if (firstCell.textContent.trim() !== 'VA.gov JSON:API') {
if (schemaLabel.textContent.trim() !== 'VA.gov JSON:API') {
row.remove();
}
});
}
}
})(jQuery, Drupal, drupalSettings);

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

namespace Drupal\va_gov_api\EventSubscriber;

use Drupal\core_event_dispatcher\Event\Theme\PageAttachmentsEvent;
use Drupal\core_event_dispatcher\PageHookEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Add JS to the OpenAPI UI selector page so only one schema is displayed.
*/
class AddJsEventSubscriber implements EventSubscriberInterface {

/**
* The request.
*/
protected RequestStack $requestStack;

/**
* Constructor.
*/
public function __construct(RequestStack $requestStack) {
$this->requestStack = $requestStack;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
PageHookEvents::PAGE_ATTACHMENTS => ['onPageAttachments'],
];
}

/**
* Modify response to add JS script to a route.
*/
public function onPageAttachments(PageAttachmentsEvent $event): void {
if ($this->requestStack->getCurrentRequest()->get('_route') === 'openapi.downloads') {
$attachments = &$event->getAttachments();
$attachments['#attached']['library'][] = 'va_gov_api/json_schemas_table_filter';
}
}

}
18 changes: 0 additions & 18 deletions docroot/modules/custom/va_gov_api/va_gov_api.module

This file was deleted.

6 changes: 6 additions & 0 deletions docroot/modules/custom/va_gov_api/va_gov_api.services.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Increases the # of items in the response for any given jsonapi request.
parameters:
next_jsonapi.size_max: 2000
services:
va_gov_api.add_js_to_ui:
class: Drupal\va_gov_api\EventSubscriber\AddJsEventSubscriber
arguments: ['@request_stack']
tags:
- { name: event_subscriber }

0 comments on commit 73a8a63

Please sign in to comment.