-
Notifications
You must be signed in to change notification settings - Fork 70
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
Facility banner endpoints needs additional data to fully render #16781
Comments
I'm going to start with the "cheap" solution since that might be good enough. It isn't technically sexy, but it certainly works and requires no code changes...well, a small code change it turns out. It is true that trying to To test:
I thought this would be a breeze, but after adding the situation updates field to the Turns out some code was moving the revision log text area to the right-hand sidebar where "Section Settings" is. I can tell in browser dev tools that the error CSS class was being applied but it didn't show up to me...so it took me a minute to figure out that was the issue. I was able to find code already altering the banner forms here: https://github.com/department-of-veterans-affairs/va.gov-cms/blob/main/docroot/modules/custom/va_gov_banner/src/EventSubscriber/EntityEventSubscriber.php#L40 And it was simple to Proposed changes:
Questions:
Other attempts:
|
Since attaching Attempts:
So, I decided to try the nuclear option of modifying the response at the last minute, which always works. In this case, I don't think the code is much better or worse than modifying how JSON:API loads resources, normalizes, and then serializes them for responses. Plus, the data can be in whatever format is requested since the response doesn't go through any validation afterward. Beautiful ;P Something like: public function onKernelResponse(ResponseEvent $event): void {
// Only modify the "/jsonapi/banner-alerts" route.
$request = $event->getRequest();
if ($request->get('_route') !== 'va_gov_api.banner_alerts') {
return;
}
// Get paragraph data and put into the includes section...
$response = $event->getResponse();
$response->setContent($this->setParagraphData($response->getContent()));
} The main caveat is updating the OpenAPI schema generator for the VA.gov JSON:API to reflect how the data is added for discoverability. I think this can be done, but I paused to make this comment and will go look into that afterward. An alternative is to split up the queries so that the |
I did try looking at what the computed breadcrumbs module does for a computed field on all node types. While I did find another hook that defines bundle field definitions, but it doesn't have an event subscriber, which was a problem before...I'm fine with adding hooks that have no deprecation notices. Three solutions so far:
I added a draft PR to #16883 . However, there appears to be a graphQL check error I need to review. Not sure if it's because of the added field definition... It's beyond the scope of this issue, but I'm not sure why the banner alerts endpoint has to conform to the JSON:API spec since the collection is not a standard collection. It's also not in the JSON:API schema that I could see. With a custom REST endpoint, the data could be shaped however is wanted. |
After commenting out the |
I was initially thinking maybe it would happen in this loop in // Add the banners to the response.
foreach ($facility_banners as $entity) {
$this->addEntityToResponse($resource_type, $entity);
} If it isn't feasible to add the paragraph data we want as a field on the entity for our particular endpoint there, I think I found something that may help. This issue seems to describe a similar problem as ours ( It may be worth pulling on that thread a little farther... it seems potentially simpler than some of the other approaches. I don't want to add "unused" hidden fields to content types that don't need them. Duplicating data on our endpoint doesn't bother me (as you note, it is a custom REST endpoint, we can shape it however we want) as long as it isn't duplicating the data on the actual JSON:API endpoint for a banner node (ie |
I checked out that patch to test, but now I am seeing some weird behavior locally that I've replicated. Even with no code changes to the main branch, I still get different behavior than what I saw on the branch in the PR.
I'll try checking out the branch I made a PR on, but I'm not sure what's going on with my local right now... As far as the patch issue, I made a comment on that about the JSON:API spec and how that type of change would affect Drupal's interpretation of it. I think most devs want more information included, but I could see the issue linger for a years...maybe not. However, adding a patch to how relationships work, in general, just for this issue seems riskier than other approaches. What about making the banner alerts code run at an endpoint like It should be rather trivial to take what's in the |
i am not tied to the
this is exactly it, yes. |
Okay, I added a custom controller to the example code in the attached draft PR so that a request to "api/v1/banner-alerts?path=" // Add the banners to the response.
$full_width_banner_alert_data = [];
foreach ($facility_banners as $entity) {
$full_width_banner_alert_data[] = $this->serializer->normalize($entity);
// Override field_situation_updates with referenced paragraph data.
$situation_updates = $entity->get('field_situation_updates')->referencedEntities();
$situation_update_data = [];
foreach ($situation_updates as $situation_update) {
$situation_update_data[] = $this->serializer->normalize($situation_update);
}
$full_width_banner_alert_data['field_situation_updates'] = $situation_update_data;
} includes the paragraph data in the response. I mainly copy/pasted the code in And what is the preferred shape of data the frontend wants to receive? It might be possible to normalize like JSON:API and then override the field, but the default output for entity normalization isn't similar to the current banner alerts response. |
I am comfortable with this approach. |
Requirements
'Facility banners' (
full_width_banner_alert
nodes) can be returned by the Banner Alerts endpoint, but the data returned is missing some needed information which is required to fully render the banner.Acceptance criteria
Background & implementation details
The endpoint is reached at the following URL structure:
where the item-path value should contain both leading and trailing slashes.
That endpoint is largely defined in this class: https://github.com/department-of-veterans-affairs/va.gov-cms/blob/main/docroot/modules/custom/va_gov_api/src/Resources/BannerAlerts.php
The
full_width_banner_alert
entity contains a fieldfield_situation_updates
that is a Paragraph field and therefore is not fully included, since entity references are not expanded normally.We can't use the normal
?include=field_situation_update
mechanism in JSON:API requests because multiple resource types are returned. jsonapi module (core) DOES contain mechanisms for resolving includes and we might want to take advantage of that here.The text was updated successfully, but these errors were encountered: