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..c07b77a22f 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 @@ -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); + 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..c19705d593 --- /dev/null +++ b/docroot/modules/custom/va_gov_api/src/EventSubscriber/AddJsEventSubscriber.php @@ -0,0 +1,46 @@ +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'; + } + } + +} diff --git a/docroot/modules/custom/va_gov_api/va_gov_api.module b/docroot/modules/custom/va_gov_api/va_gov_api.module deleted file mode 100644 index 3251458992..0000000000 --- a/docroot/modules/custom/va_gov_api/va_gov_api.module +++ /dev/null @@ -1,18 +0,0 @@ -getPath(); - if ($current_path == '/admin/config/services/openapi') { - $attachments['#attached']['library'][] = 'va_gov_api/json_schemas_table_filter'; - } -} diff --git a/docroot/modules/custom/va_gov_api/va_gov_api.services.yml b/docroot/modules/custom/va_gov_api/va_gov_api.services.yml index bbdadb0350..5637378076 100644 --- a/docroot/modules/custom/va_gov_api/va_gov_api.services.yml +++ b/docroot/modules/custom/va_gov_api/va_gov_api.services.yml @@ -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 }