Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uw_cfg_common
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WCMS
uw_cfg_common
Commits
1646debe
Commit
1646debe
authored
2 years ago
by
Eric Bremner
Committed by
Kevin Paxman
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
ISTWCMS-5954: fixing api response alter
parent
c85075b6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!358
ISTWCMS-5954: add fieldable path
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/EventSubscriber/UwResponseSubscriber.php
+56
-16
56 additions, 16 deletions
src/EventSubscriber/UwResponseSubscriber.php
with
56 additions
and
16 deletions
src/EventSubscriber/UwResponseSubscriber.php
+
56
−
16
View file @
1646debe
...
...
@@ -155,17 +155,35 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
return
;
}
// Reset the order of the json response.
// We do this because by default jsonapi puts the data
// into three categories, attributes, relationships and
// meta. We want the endpoint to just have the straight
// values without the categories.
$jsonapi_response
[
'data'
]
=
$this
->
setUwJsonResponseData
(
$jsonapi_response
);
// If there is a listing image, set it to the
// info we need.
if
(
isset
(
$jsonapi_response
[
'data'
][
0
][
'listing_image'
]))
{
$this
->
setUwListingImage
(
$jsonapi_response
);
// If there is data in the json response, process it.
if
(
isset
(
$jsonapi_response
[
'data'
]))
{
// Reset the order of the json response.
// We do this because by default jsonapi puts the data
// into three categories, attributes, relationships and
// meta. We want the endpoint to just have the straight
// values without the categories.
$jsonapi_response
[
'data'
]
=
$this
->
setUwJsonResponseData
(
$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.
...
...
@@ -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.
*
...
...
@@ -215,22 +253,24 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
*
* @param array $jsonapi_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\PluginNotFoundException|\Drupal\Core\Entity\EntityStorageException
*/
private
function
setUw
Listing
Image
(
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.
foreach
(
$jsonapi_response
[
'data'
]
as
$i
=>
$iValue
)
{
// 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.
$entity
=
$this
->
entityRepository
->
loadEntityByUuid
(
'media'
,
$iValue
[
'listing_image'
][
'data'
][
'id'
]
$iValue
[
$image_name
][
'data'
][
'id'
]
);
// Get the image field from the media entity.
...
...
@@ -240,7 +280,7 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
$file
=
$image
->
entity
;
// 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.
$picture
[
'sources'
]
=
$picture
[
'responsive_sources'
];
...
...
@@ -249,7 +289,7 @@ class UwResponseSubscriber implements EventSubscriberInterface, ContainerInjecti
unset
(
$picture
[
'responsive_sources'
]);
// Set the values for the listing image.
$jsonapi_response
[
'data'
][
$i
][
'listing_image'
]
=
[
$jsonapi_response
[
'data'
][
$i
][
$image_name
]
=
[
'fid'
=>
$file
->
id
(),
'filename'
=>
$file
->
getFilename
(),
'uri'
=>
$file
->
getFileUri
(),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment