- Issue created by @brianperry
Traversing relationships without deserializing data is kind of gross.
Take an image on an Umami article - you end up having to do something like:
const mediaId = article.relationships.field_media_image.data.id;
const mediaEntity = included.find((obj) => obj.id === mediaId);
const fileId = mediaEntity.relationships.field_media_image.data.id;
const fileEntity = included.find((obj) => obj.id === fileId);
const fileUrl = fileEntity.attributes.uri.url;
We could provide a small utility to make this easier.
A quick example could be something like:
const getRelationship = (entity, includes, relationship) => {
const id = entity.relationships[relationship].data.id;
return included.find((obj) => obj.id === id);
};
const mediaEntity = getRelationship(article, included, "field_media_image");
const fileEntity = getRelationship(mediaEntity, included, "field_media_image");
const fileUrl = fileEntity.attributes.uri.url;
Would be nice if relationship was an array so we could do something like:
getRelationship(article, included, ["field_media_image", "field_media_image"]);
And just return the file entity rather than having to do a two step process.
* Implementation
** Consider entity reference fields with multiple values here.
* Docs
* Tests
Active
Code