Problem/Motivation
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.
Steps to reproduce
Proposed resolution
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.
Remaining tasks
* Implementation
** Consider entity reference fields with multiple values here.
* Docs
* Tests
User interface changes
API changes
Data model changes
Issue fork api_client-3425819
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments