A concept that is recently gaining a lot of attention in Drupal community is what is referred as “headless” Drupal or decoupled Drupal. The idea is to combine the strengths of Drupal as a content management system (CMS) with strengths of other technologies and frameworks like AngularJS or integrate with ecosystem of choice. It is possible that one may want to integrate content from a Drupal website into other Java based applications/ecosystem (say, accessing Drupal content from Java server-side). To know more about exciting developments on decoupled CMS, there is a hub of information at https://pantheon.io/decoupled-cms.
In this post, we will see a basic example of a Drupal website exposing content as JSON REST API and a Java client to access the content. In Part 1, we will create a REST API from a Drupal website. In Part 2, we will create a Java client to access the content externally. Drupal 8 has an in-built ability to expose REST API out of the box.
Installing and Creating Drupal Site
Lets get started with creating a Drupal website. The fastest and simplest way I know of, is to use Pantheon. I am using a free account, using which, two sites. Once account is created, click ‘Create your first site’. I am naming the site as “My Tech Courses”.
Click ‘Create Site’. Select ‘Start from Scratch’ and click ‘Install Drupal 8’.
Site gets created.
Click ‘Visit your Pantheon Dashboard’.
Click ‘Visit Development site. It starts regular Drupal installation.
Select ‘English’ for language and ‘Save and continue’. In Choose profile screen, select ‘Standard’ and ‘Save and continue’. Fill details in ‘Configure site’ screen and ’Save and continue’. Make sure to remember the username/password (super user).
Creating REST API
Now, to create REST API, we will use the approach of using Views module. To enable the required modules, click on ‘Extend’ from the top menu (login as Admin if not already). Scroll down to Web Services section and enable and ‘Install’.
Go to Structure –> Views. Click ‘Add new view’. For simplicity, we will use the already available Article type for courses.
Fill the following. View name: Courses. Optionally, give a description. Show ‘Content’ of type ‘Article’ sorted by ‘Newest first’. Check ‘Provide a REST export’ and give path api/v1/courses .
Click ‘Save and Edit’ and click Save in next page. Our REST API is already ready and can be access using the API endpoint.
API Fields Customization
The API that is created fetches all the data from nodes. To customize the fields, go back to Structure –> Views and click Edit.
Click on the link ‘Entity’ next to Show
Change selection to Fields.
Click ‘Apply(this display)’. Apply again. Click on the Add button next to “FIELDS’. Select fields ‘Body’ and ‘Image’.
Click Apply multiple times to close the popup screens. Once back to the views screen, click on settings link next to show. Check Raw Output (to ensure no additional tags are added by Drupal) and give Alias as below and Apply:
Click the title field under FIELDS and uncheck ‘Link to the content’ (so that title does not come as html markup).
In the screen to configure field title, uncheck the ‘Link to Content’ and Apply.
Make sure to Save the view.
Add Content
Now, let us go to website home and add some content. Login as Admin (if not already). Click on ‘Add content’, click on ‘Article’ and give title, body and upload an image.
Test the Drupal Content API
Open any REST client tool (or even just browser) and access the Drupal REST API endpoint url http://dev-my-tech-courses.pantheon.io/api/v1/courses. Using Postman Chrome extension:
We have successfully tested a Drupal site exposing its content as REST API. The image/photo content is coming as html markup, which is a problem, but we will deal with that later. In part 2, we will create a Java client to fetch and display content from this API.