1

I have a site https://www.huytonweb.com/ the front end is in Next.js and the back end is Drupal 10 CMS. The front end will access the data from the back end using ?_format=json (like https://cms.huytonweb.com/services/ecommerce?_format=json (may need to run in Firefox)) and views and blocks are accessed using the jsonapi mod.

I'm writing the content and think it would be useful for content to reference other content in an <aside>. So, I edit my content type (locally) and add a reference field, I edit my content and add some references and all is great, until I see the json, no title, just ID, UUID and URL! No matter what I do I cannot get a title to appear in the json data for referenced content.

My fallback was to just go through the DOM and get all the links and automatically create an <aside>, but the URL inerHTML may not be the title, so I need a Drupal way to do this.

So, my question is; how do I get a title into the reference field for content?

Any ideas or thoughts would be appreciated

Edit: The title was edited and became misleading! The content is using ?_format=json not the JSON:API

7
  • What is your use case? Usually explaining how you want to do things technically just confuses those who know how to use Drupal, because the person explaining it that way is trying to do it their own way, rather than the Drupal way, so we won't often have experience in your specific methodology. If you give your use case, often there is a Drupally-way to do it, with more people who will be able to provide their method of achieving that goal.
    – Jaypan
    Commented May 31 at 15:49
  • Didn't they explain the use-case in the second paragraph? It seems so to me.
    – mona lisa
    Commented May 31 at 15:53
  • Sorry if the use case was not explained as well as it could have been, I want to add a bunch of references from content to content using field reference in an aside. If you have any ideas @Jaypan please let me know
    – Dave_____1
    Commented May 31 at 16:25
  • Oh, I'm a new contributor, but I've been using Drupal since v5 and have wrote a few mods and themes in 5, 6 and 7 if the only choice is override the mod. I did have a similar problem in D7 but that was the URL not the title
    – Dave_____1
    Commented May 31 at 16:28
  • Does drupal.org/project/jsonapi_field_formatter help? Commented Jun 1 at 21:47

2 Answers 2

1

JSON:API returns ID, UUID, and URL because that's the spec.

If you want more properties, like the title or the content body, you have to include those. See the documentation on includes.

Also two useful modules:

  • JSON:API Extras allows you to set the default includes per resource. So you can specify which entity references should be auto-included for the base endpoint.
  • JSON:API includes can be hard to parse; it can be much easier if the response is flattened. This is what the JSON:API Include module does.
4
  • Thanks for this. I use the JSON:API for the blocks and views, but for the content I use ?_format=json like: http://localhost:8080/knowledge-base/ecommerce?_format=json Vs https://cms.huytonweb.com/jsonapi/block_content/basic?include=field_main_image&fields[file--file]=uri,url&filter[published-filter][condition][path]=field_published&filter[published-filter][condition][operator]=%3D&filter[published-filter][condition][value]=1&sort=field_weight This means I can't use the include for content FYI I'm not using the JSON:API for content because it allows my clients to use the Drupal menu
    – Dave_____1
    Commented Jun 3 at 13:21
  • Title was changed to say I'm using the JSON:API. This was incorrect and I've changed it to show the correct ?_format=json
    – Dave_____1
    Commented Jun 3 at 13:47
  • It's unfortunate the title was edited incorrectly, making this answer invalid. Given the UUID, you could fetch the title with JSON:API. This will create another request, but I'm assuming the endpoint you are using already would require a second request anyway because it's not returning the entity reference content, only the raw ids. It would also be helpful if you clarified in the question which decoupled menu implementation you're using; there are a couple, and core's has been through a few iterations, so it's useful to clarify which. Commented Jun 3 at 14:27
  • Thanks @patrick Kenny. Yeah, it was unfortunate, I do already use Extras for blocks and views. I'm on D10 and using the core menu module. A new request is one way to do it, just send all the referenced UUIDs back up and stich the returning content in, but this sounds like overkill to just add a title or teaser to the reference which I'm sure it can get in the original DB request. I was hoping for a simple mod or yaml change. I did a similar thing years ago on D6 or D7 but it was for the URL not the title and I think it was a hack :(
    – Dave_____1
    Commented Jun 4 at 16:03
0

There are multiple approaches to achieve it depends upon How API's are configured & implemented (using Rest API):

API's are provided by module:

if you are using module's API:

for eg: /node/{nid}?format=json

Approach 1: In this override these specific API's and add title field of reference content field

Approach 2: You need to hit the API with provided ID of reference field, then from response of API, can get the title of reference field.

API's created by rest export view: can use following approach:

Add the relationship of content reference field in the view & add then add title field of reference content field

2
  • Thanks for this, it's similar to other suggestions but not what I'm looking for. I'm using jsonapi for the blocks and views, but not for content. I use ?_format=json for content because it allows my clients to use the Drupal Menu system e.g. https://cms.huytonweb.com/services/ecommerce?_format=json I'm looking for a way to add a title to the reference field. Later, I'll want the teaser, images, etc. too so I can add full cards for the referenced content
    – Dave_____1
    Commented Jun 3 at 13:34
  • Title was changed to say I'm using the JSON:API. This was incorrect and I've changed it to show the correct ?_format=json
    – Dave_____1
    Commented Jun 3 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.