1/13/2024 0 Comments Drupal core json apiNavigate to a URL like and confirm that JSON is being output at that URL. If using basic authentication and the Basic Auth module isn’t already enabled, enable it. You could also switch out the authentication to something like OAuth, but for this article we’ll stick with the built-in basic and cookie authentication methods. If you wanted to update or post content you’d add POST or PATCH information. That will support GET, or read-only access. To expose users or taxonomy terms or comments, copy the above file, and change the name and id as necessary, like this: filename: /MODULE/config/optional/_term.yml: id: entity.taxonomy_term For instance, if you created a module called custom_jsonapi, a file that would expose node data might look like: filename: /MODULE/config/optional/.yml: id: entity.node This could be done by creating a simple module that has YAML file(s) in /MODULE/config/optional. Then, create YAML configuration to “turn on” the JSON API. My example is a simple Drupal 8 site with an article content type that has a body and field_image image field, the kind of thing core provides out of the box.įirst, download and install the JSON API module. There is not much preparation needed for the source because of JSON API’s flexibility. There’s a series of short videos on YouTube that demonstrate many of the configuration options and parameters that are available in Drupal’s JSON API. (The Drupal community plans to add JSON API to Core in the future.) Because of all that flexibility, that is the solution I decided to use for my example. For example, you can sort and filter the articles, limit the fields that are returned to a subset, and bring along any or all related entities in the same query. From that one path, the consumer can select exactly what they want to retrieve just by altering the URL. You don’t need to know the path of every individual entity, just the general path for a entity type, and bundle. It puts the power in the hands of the data consumer. To further complicate things, there's an outstanding bug using GET with Views REST endpoints. And you’d have to create a View for every possible collection of data that you want to make available. You could also use Views to create a JSON endpoint, but it might be difficult to configure it to include all the required data, especially all the data from related content, like images, authors, and related nodes. You can access data from any entity by appending ?_format=json to its path, but that means you have to know the path ahead of time, and you’d be pulling in one entity at a time, which is not efficient. Why JSON API?ĭrupal 8 Core ships with two ways to export JSON data. On the target site, I used Migrate from Drupal Core 8.2.3 along with Migrate Plus and Migrate Tools. I ended up using the JSON API module, along with the REST modules in Drupal Core on the source site. Hopefully, this will save someone a lot of time in the future. Nevertheless, it took me several days to get everything working, so I thought I’d write up an article to explain how I solved the problem. Much of what I wanted to do was undocumented and confusing, but it worked well, once I figured it out. I wanted to find a way to pull data from one Drupal 8 site to another, using JSON API to expose data on one site, and Drupal’s Migrate with a JSON source on another site to consume it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |