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

OpenAPI UI Hook to Event Subscriber #16552

Merged
merged 7 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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 }
Loading