Drupal 8 and REST API

Scroll Down

Written by Chris Wright

July 5, 2017

Drupal 8 and REST API

Starting a project in Drupal 8 can be somewhat intimidating if you’ve never used it. Several months ago there was not much documentation online for it. It’s much better now as the docs are updated regularly. However, there was a struggle when learning it and when Drupal Console / kint was buggy between versions.

Drupal Console is basically a better version of Drush but it is much more powerful. You can generate custom modules and tests straight from the terminal. You can also clear the cache, migrate databases, create nodes, users and even debug, enable and disable REST endpoints. There is a lot more that you can do with Drupal Console, but those are some basic things you can use it for. More documentation here:

http://drupalconsole.com/cheatsheet/

And you can download it here: https://drupalconsole.com/

If you are looking to create a REST API, you’re in luck because REST comes in core in Drupal 8. Enable the RESTful Web Services module which comes in core Drupal 8. You will also need to download and install the Rest UI module if you wish to view your endpoints in the GUI rather than just operating in Drupal Console. The module REST API Doc is a good resource for building and debugging an api. It will create a running documentation of your api and endpoints, what they do etc based on the endpoints you have created and enable. More information here: https://www.drupal.org/project/rest_api_doc

If you are using Drupal’s default REST resources, you will only be able to use GET, POST, PATCH and DELETE http methods, however you can create your own endpoints and configure them in such a way to use GET, PUT, POST, DELETE, PATCH, HEAD and OPTIONS.

To create a Rest Resource through Drupal Console, you will first need your own custom module. You can do this in the terminal by running drupal generate:module. It will ask you for your module name, machine name, module path, description, package name, Drupal core version, if you want a .module file generated, is the module a feature, does it need a composer.json, does it have any dependencies, should it generate a unit test class, a themeable template and lastly if you are ready to generate the module.

Next you will create your rest resource by running the command drupal generate:plugin:rest:resource [options] or the shorthand is drupal gprr. It will ask you which module to generate this in, you can add the name of the custom module you just created, enter your rest resource name, this should be an UpperCamelCase name such as MyCustomEndpoint, the machine name for your endpoint, your rest resource label (this is how it will appear on the sites’ GUI, the plugin rest resource url, which HTTP method(s) you would like to be available in your endpoint and if you are ready to confirm generation.

In the past, trying to enable an endpoint through the GUI didn’t actually enable it even though it said it did (this is probably fixed now), however you can enable, disable and debug your endpoints successfully through Drupal Console using these commands:

drupal rest:enable [arguments] or the shorthand is drupal ree

drupal rest:disable [arguments] or the shorthand is drupal redi

drupal rest:debug [arguments] [options] or the shorthand is drupal rede

Again, there is further documentation for all of this on https://drupalconsole.com/cheatsheet/

Also, this page has been very helpful: https://docs.drupalconsole.com/en/commands/available-commands.html

You will need to do some configuring in the your modules’ src/Plugin/rest/resource/MyCustomEndpoint.php to actually gather/create/delete content, but that is how you make a boilerplate endpoint using Drupal Console. Another great feature of using the REST API with Drupal 8 is how easy it is to use Views to create an Endpoint view. Instead of just gathering your content on a page or on a block, you can create an endpoint url that allows you to grab that content externally through the API. To do this, you will need these module enabled: HAL, HTTP Basic Authentication, RESTful Web Services and Serialization. These modules are available in core, just make sure they are enabled.

Create a new view and select REST Export Settings: Provide a REST export > REST export path: “your-endpoint-url-goes-here”. In the Format: Serialization > Settings, you can change your accepted request formats for what you need it to be. It can be hal_json, json, or xml. Side-note, if you have the JSONAPI module enabled, I believe you can also use that as a format in your view. By default, the content is exposed as a generic entity, but you can change it to show fields so you can have more control on what content is being output through your view.