Week 7
Drupal as a backend
Headless, Decoupled
Drupal CMS provides an administrative back-end capable of making changes that display on the front-end of your website in HTML.
This is essentially what all CMS platforms do: provide an administrative suite to non-technical website managers.
The idea of a headless Drupal CMS is
you keep using the administrative back-end,
but the front-end and presentation can take many forms.
For example, a javascript framework like React might be used instead to present the front-end.
Content might be displayed on a desktop, smartphone, smartwatch, a tv set, ...
A Headless system only serves raw content through our API.
No markup is provided. CMS templates are not used.
Markup (html) will have to be added through the Front-end framework.
The CMS is only used for content management.
A Decoupled system serves formatted content (that already includes markup/html).
CMS templates are still used.
The CMS provides basic markup.
The CMS stores both the content
and parts of the presentation.
Decoupled is often considered Best of both worlds.
Headless vs Decoupled?
Drupal supports both.
Web Services make it possible for external applications to interact with our application (in this case our Drupal site).
The most common interactions are reading, creating, updating, and deleting resources. (= CRUD)
REST is one of the most popular ways of making Web Services work.
There are other formats such as SOAP or XML-RPC, but we are only going to focus on REST because it is the Drupal standard.
REST utilizes HTTP methods, such as GET, POST, and DELETE.
As an example, a popular usage of a REST interface is a mobile application that needs to read and write data from your site's database.
REpresentational State Transfer
Guiding Principles of REST
Web Services have been implemented in Drupal 8 Core with the following modules:
Exposes entities and other resources via a RESTful web API. It depends on the Serialization module for the serialization of data that is sent to and from the API.
Provides a service for serialization of data to and from formats such as JSON and XML.
Extends the Serialization module to provide the HAL hypermedia format. This is what is used as the primary format in Drupal 8 Core. It only adds two reserved keywords, ‘_links’ for link relations (also used by Github's Pull Request API) and ‘_embedded’ for embedded resources. The HAL hypermedia format can be encoded in both JSON and XML. For more details see the initial HAL proposal.
This module implements basic user authentication using the HTTP Basic authentication provider. It facilitates the use of an username and password for authentication when making calls to the REST API. It is required for the examples shown in this blog post, and I would advise configuring SSL if you use it in production. For anyone looking for a more secure option, check out the contributed OAuth module which already has a Drupal 8 release.
Optionally enable
These are all CORE modules and are always included with Drupal.
The Drupal framework supports many ways to expose data. Let's look at the most basic version.
First we will create a new content-type: portfolio.
Then we will use the Views module, to create a simple RESTful export of data in the JSON format.
We can then consume this JSON in an external application.
Enable the following modules:
Create a new content-type: Portfolio
Add fields:
Create a few nodes (at least 3) using dummy content.
Create a new view: Portfolio API
Choose "Provide REST export"
Set REST export path: "api/portfolio"
Make the following config:
Make sure to check your result.
Visit: yoursitedomain:port/api/portfolio
f.e.: http://drupalimd.dd:8083/api/portfolio
Drupal has a strict security policy. By default it does not allow for external services to communicate with itself.
For now we will disable the CORS security measures.
This is not best practice.
For a production site you should set up proper CORS & implement proper authentication.
# Configure Cross-Site HTTP requests (CORS).
# Read https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
# for more information about the topic in general.
# Note: By default the configuration is disabled.
cors.config:
enabled: true
Your data is now ready for consumption.
For example: use a bit of jquery to see if we can retrieve and show data.
(function($) {
'use strict';
$( document ).ready(function() {
$.ajax('http://drupalimd.dd:8083/api/portfolio', // request url
{
dataType: 'json', // type of response data
// success callback Function
success: function (data, status, xhr) {
console.log(data);
},
}
);
});
})(jQuery);
Another example: Test via console
jQuery.ajax('http://drupalimd.dd:8083/api/portfolio', // request url
{
dataType: 'json', // type of response data
// success callback Function
success: function (data, status, xhr) {
console.log(data);
},
}
);
Follow Step by step guide, see previous slides
In the next class Mobile Development,
you will be required to use this API.