Skip to content

Commit

Permalink
convert one event subscriber to another
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfinnarn committed Dec 22, 2023
1 parent 8c96385 commit a60b120
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 51 deletions.
26 changes: 15 additions & 11 deletions docroot/modules/custom/va_gov_api/js/json-schemas-table-filter.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
/**
* Attach a behavior to filter out unwanted schemas for OpenAPI UI viewing.
*/
document.addEventListener('DOMContentLoaded', function () {
// Get all the rows in the table body.
const rows = document.querySelectorAll('#block-vagovclaro-content table tbody tr');
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.vaGovJsonSchemasTableFilter = {
attach: function (context, settings) {
// Get all the rows in the table body.
const rows = document.querySelectorAll('#block-vagovclaro-content table tbody tr');

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

// Remove all but 'VA.gov JSON:API'.
if (schemaLabel.textContent.trim() !== 'VA.gov JSON:API') {
row.remove();
// Remove all but '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
Expand Up @@ -2,70 +2,44 @@

namespace Drupal\va_gov_api\EventSubscriber;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\core_event_dispatcher\Event\Theme\PageAttachmentsEvent;
use Drupal\core_event_dispatcher\PageHookEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RequestStack;

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

/**
* The module handler.
* The request.
*/
private ModuleHandlerInterface $moduleHandler;
protected RequestStack $requestStack;

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

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

/**
* Modify response to add JS script.
*
* @throws \DOMException
* Modify response to add JS script to a route.
*/
public function onResponse(ResponseEvent $event): void {
// Only run this code on the OpenAPI UI selector page.
$request = $event->getRequest();
$path = $request->getPathInfo();
if ($event->getRequestType() == HttpKernelInterface::MAIN_REQUEST && $path === '/admin/config/services/openapi') {

$script = file_get_contents(
DRUPAL_ROOT . '/'
. $this->moduleHandler->getModule('va_gov_api')->getPath()
. '/js/json-schemas-table-filter.js');

// Create a DOMDocument object and load the response content into it.
$responseContent = $event->getResponse()->getContent();
$dom = new \DOMDocument();
// Need to suppress errors due to <nav> tags.
libxml_use_internal_errors(TRUE);
$dom->loadHTML($responseContent);
libxml_clear_errors();

// Add the script to the DOMDocument object before the closing body tag.
$dom->getElementsByTagName('body')
->item(0)
->appendChild($dom->createElement('script', $script));

// Save the DOMDocument object back into the response content.
$responseContent = $dom->saveHTML();
$event->getResponse()->setContent($responseContent);
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';
}
}

Expand Down
8 changes: 8 additions & 0 deletions docroot/modules/custom/va_gov_api/va_gov_api.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
json_schemas_table_filter:
version: 1.x
js:
js/json-schemas-table-filter.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
2 changes: 1 addition & 1 deletion docroot/modules/custom/va_gov_api/va_gov_api.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ parameters:
services:
va_gov_api.add_js_to_ui:
class: Drupal\va_gov_api\EventSubscriber\AddJsEventSubscriber
arguments: ['@module_handler']
arguments: ['@request_stack']
tags:
- { name: event_subscriber }

0 comments on commit a60b120

Please sign in to comment.