Skip to content
Snippets Groups Projects
Commit 1646debe authored by Eric Bremner's avatar Eric Bremner Committed by Kevin Paxman
Browse files

ISTWCMS-5954: fixing api response alter

parent c85075b6
No related branches found
No related tags found
1 merge request!358ISTWCMS-5954: add fieldable path
...@@ -155,17 +155,35 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti ...@@ -155,17 +155,35 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
return; return;
} }
// Reset the order of the json response. // If there is data in the json response, process it.
// We do this because by default jsonapi puts the data if (isset($jsonapi_response['data'])) {
// into three categories, attributes, relationships and
// meta. We want the endpoint to just have the straight // Reset the order of the json response.
// values without the categories. // We do this because by default jsonapi puts the data
$jsonapi_response['data'] = $this->setUwJsonResponseData($jsonapi_response); // into three categories, attributes, relationships and
// meta. We want the endpoint to just have the straight
// If there is a listing image, set it to the // values without the categories.
// info we need. $jsonapi_response['data'] = $this->setUwJsonResponseData($jsonapi_response);
if (isset($jsonapi_response['data'][0]['listing_image'])) {
$this->setUwListingImage($jsonapi_response); // The image fields to check and maybe unset.
$image_fields = [
'listing_image',
'hero_image',
];
// Step through the image fields and if there is
// no field, unset it, if there is set the data
// to the info that we want.
foreach ($image_fields as $image_field) {
// If the image field is empty, then unset the field.
// If there is an image field, set the data.
if (empty($jsonapi_response['data'][0][$image_field]['data'])) {
$this->unsetUwField($jsonapi_response, $image_field);
} else {
$this->setUwImage($jsonapi_response, $image_field);
}
}
} }
// Now set the new json response. // Now set the new json response.
...@@ -173,6 +191,26 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti ...@@ -173,6 +191,26 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
} }
} }
/**
* @param array $json_response
* The json response.
* @param string $field_name
* The field name to unset.
*/
private function unsetUwField(array &$json_response, string $field_name): void {
// Step through each of the data values and reorder.
foreach ($json_response['data'] as $i => $iValue) {
// Step through each of the values in the data.
foreach ($iValue as $j => $jValue) {
if ($j == $field_name) {
unset($json_response['data'][$i][$j]);
}
}
}
}
/** /**
* Function to reorder the data of the json response. * Function to reorder the data of the json response.
* *
...@@ -215,22 +253,24 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti ...@@ -215,22 +253,24 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
* *
* @param array $jsonapi_response * @param array $jsonapi_response
* The json api response. * The json api response.
* @param string $image_name
* The name if the image to set.
* *
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException|\Drupal\Core\Entity\EntityStorageException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException|\Drupal\Core\Entity\EntityStorageException
*/ */
private function setUwListingImage(array &$jsonapi_response): void { private function setUwImage(array &$jsonapi_response, string $image_name): void {
// Step through each of the data and set the listing image. // Step through each of the data and set the listing image.
foreach ($jsonapi_response['data'] as $i => $iValue) { foreach ($jsonapi_response['data'] as $i => $iValue) {
// If there is an uuid for the listing image, then set the data. // If there is an uuid for the listing image, then set the data.
if (isset($iValue['listing_image']['data']['id'])) { if (isset($iValue[$image_name]['data']['id'])) {
// Load the media entity using the uuid. // Load the media entity using the uuid.
$entity = $this->entityRepository->loadEntityByUuid( $entity = $this->entityRepository->loadEntityByUuid(
'media', 'media',
$iValue['listing_image']['data']['id'] $iValue[$image_name]['data']['id']
); );
// Get the image field from the media entity. // Get the image field from the media entity.
...@@ -240,7 +280,7 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti ...@@ -240,7 +280,7 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
$file = $image->entity; $file = $image->entity;
// Get the picture element. // Get the picture element.
$picture = $this->uwService->prepareResponsiveImage($entity, 'uw_ris_media'); $picture = $this->uwService->prepareResponsiveImage($entity, 'uw_ris_media', TRUE);
// Set the sources to the formatted version. // Set the sources to the formatted version.
$picture['sources'] = $picture['responsive_sources']; $picture['sources'] = $picture['responsive_sources'];
...@@ -249,7 +289,7 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti ...@@ -249,7 +289,7 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
unset($picture['responsive_sources']); unset($picture['responsive_sources']);
// Set the values for the listing image. // Set the values for the listing image.
$jsonapi_response['data'][$i]['listing_image'] = [ $jsonapi_response['data'][$i][$image_name] = [
'fid' => $file->id(), 'fid' => $file->id(),
'filename' => $file->getFilename(), 'filename' => $file->getFilename(),
'uri' => $file->getFileUri(), 'uri' => $file->getFileUri(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment