From 315f645fce911e89d8b5481d32ea56c8aa1d2a77 Mon Sep 17 00:00:00 2001 From: Alex Finnarn Date: Thu, 21 Dec 2023 13:25:25 -0500 Subject: [PATCH 1/5] convert hook to event subscriber --- composer.json | 4 +- .../js/json-schemas-table-filter.js | 27 ++++---- .../EventSubscriber/AddJsEventSubscriber.php | 63 +++++++++++++++++++ .../custom/va_gov_api/va_gov_api.module | 18 ------ .../custom/va_gov_api/va_gov_api.services.yml | 6 ++ 5 files changed, 84 insertions(+), 34 deletions(-) create mode 100644 docroot/modules/custom/va_gov_api/src/EventSubscriber/AddJsEventSubscriber.php delete mode 100644 docroot/modules/custom/va_gov_api/va_gov_api.module diff --git a/composer.json b/composer.json index 124eba3b14..3fa2887e10 100644 --- a/composer.json +++ b/composer.json @@ -227,7 +227,9 @@ "webmozart/path-util": "^2.3", "webonyx/graphql-php": "^14.11", "weitzman/drupal-test-traits": "^2.0.1", - "zaporylie/composer-drupal-optimizations": "^1.2" + "zaporylie/composer-drupal-optimizations": "^1.2", + "ext-dom": "*", + "ext-libxml": "*" }, "autoload": { "psr-4": { diff --git a/docroot/modules/custom/va_gov_api/js/json-schemas-table-filter.js b/docroot/modules/custom/va_gov_api/js/json-schemas-table-filter.js index 8086755b3b..995f4d6905 100644 --- a/docroot/modules/custom/va_gov_api/js/json-schemas-table-filter.js +++ b/docroot/modules/custom/va_gov_api/js/json-schemas-table-filter.js @@ -1,21 +1,18 @@ /** * Attach a behavior to filter out unwanted schemas for OpenAPI UI viewing. */ -(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'); +document.addEventListener('DOMContentLoaded', function () { + // 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 firstCell = 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 (firstCell.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); + }); +}); + diff --git a/docroot/modules/custom/va_gov_api/src/EventSubscriber/AddJsEventSubscriber.php b/docroot/modules/custom/va_gov_api/src/EventSubscriber/AddJsEventSubscriber.php new file mode 100644 index 0000000000..7141dc79b7 --- /dev/null +++ b/docroot/modules/custom/va_gov_api/src/EventSubscriber/AddJsEventSubscriber.php @@ -0,0 +1,63 @@ +moduleHandler = $moduleHandler; + } + + public static function getSubscribedEvents(): array { + return [ + KernelEvents::RESPONSE => ['onResponse'], + ]; + } + + /** + * Modify response to add JS script. + * + * @throws \DOMException + */ + 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