I'm developing a decoupled system where the end user can add additional textual information to images displayed by Drupal 8 in a lightbox. When the user opens an image, my custom module launches a Javascript script that puts up the UI to enter the text (subject to permissions, of course). The text is displayed as an overlay on the image - the image itself is not modified.

Each image is a standard Drupal image - i.e. a file, such as an image associated with an article.

I'd like to store and retrieve an image's text as a JSON string somehow "associated" with the image in Drupal's database. My initial plan was to use REST calls (JSON:API, Drupal 8's core REST, or maybe GraphQL) to transmit the text between script and Drupal. However, it appears that these REST implementations will only allow storage of Drupal entities. My JSON strings, not being entities, thus could not be stored using REST.

Is this even possible? How can a script store a JSON string into the Drupal database and link it to a given image?

Please note: I have looked at the existing answers for this issue in drupal.stackexchange and it appears none of them apply to my case. If you believe that an existing answer applies to my case, please point out which answer.

  • Isn't the image a media entity? Otherwise its just a string pointing to a location on disk. – Kevin Jan 10 at 1:29
  • Right so you have a unique use case .. howare you storing the images ... do they have reference in the database ? aka stored images attached to an entity or are they images you uploaded and are calling direct ? – Taggart Jensen Jan 10 at 1:30
  • Edited to describe the image in the second paragraph. – John B Jan 10 at 1:37
  • Yes, it's a pretty severe limitation in my opinion that json_api module doesn't support anything but entities, but that's the scope of that module. You'd need to implement your own RestResource and then you can do whatever code you want there. GraphQL module was slated for a complete typed data implementation back a year or two ago, but it's really hard to do that. – mradcliffe Jan 10 at 3:28
  • Alternatively, use entities and the json_field module would allow direct storage of json in your database in a sql database such as postgresql or limited storage in mysql/mariadb. – mradcliffe Jan 10 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.