3

I've created a rest Plugin that returns some data at /api/custom/{nid}.

Drupal core provides four modules that together can handle RESTful API, these are: Serialization, RESTful Web Services, HTTP Basic Authentication, HAL. I can't find any information how do I enable custom (or even build-in) API endpoint without restui module.

So the question is:

  • Is there a way to enable custom rest resource without restui module? If so, how?
  • Is there a way to enable build-in rest resources without restui module? If so, how?
3

Ok, I finally found out how to enable an endpoint to be accessible without RESTUI module. This gives more flexibility to keep everything in code and one custom module.

This instructions applies to Drupal > 8.2 only.

First you need to know how to create a custom rest resource, which can be found here or here.

So, let's say we have a custom module named 'custom_rest' that contains REST plugin resource. Module returns only a single node json data. The structure of the module looks like this:

|custom_rest
|--|config
|  --|optional
|      --rest.resource.custom_rest_resource.yml
|--|src
|  |--Plugin
|     |--rest
|        |--resource
|           --CustomRestResource.php
|--custom_rest.info.yml
|--custom_rest.module

The most important files for us are:

 -  CustomRestResource.php
 -  rest.resource.custom_rest_resource.yml

CustomRestResource.php is a plugin with the custom rest resource. My get method looks like this:

  public function get($nid) {
    if (!$this->currentUser->hasPermission('access content')) {
      throw new AccessDeniedHttpException();
    }
    $data = node_load($nid);
    return new ResourceResponse($data);
  }

This method returns a json node structure for an existing node. That's it, it's just for testing purposes only. It can do anything you want.

As for the enabling an endpoint it needs an extra yml file that should be located in:

/custom_rest/config/optional/rest.resource.custom_rest_resource.yml

The contents of the rest.resource.custom_rest_resource.yml file for this particular endpoint looks like this:

langcode: en
status: true
dependencies:
  module:
    - custom_rest
    - hal
    - serialization
    - user
id: custom_rest_resource
plugin_id: custom_rest_resource
granularity: method
configuration:
  GET:
    supported_formats:
      - hal_json
      - json
      - xml
    supported_auth:
      - cookie

Of course, the name of the file, dependencies, id, plugin_id, configuration should be adjusted to your needs.

If you have all of this and you will enable the module this endpoint should be accessible in browser or any app that handles http requests (postman, etc.).

Notice that this yml file won't be triggered if you already enabled the module. If you've added this file after module enable, you should uninstall the module and enable it again.

0

The other option is to use Views. It gives you a lot more options.

Below are few links where you can see some basic examples:

  • I've already seen those links :) Still it doesn't explain in details how to enable an endpoint. What I know I have to have a rest resource yml file with the configuration, but I have no idea where to put it or how to enable it during module enable. Views does not allow to POST and DELETE, so it's fine for getting data only (from what I know). – David Jan 26 '17 at 9:35
  • @David Ya that's true view don't work other than GET. Well in that case probably you need a custom plugin and in your plugin class in RestResource annotation you can give your custom end points as far as i know. For reference you can see REST core OR this article valuebound.com/resources/blog/… – Mudassar Ali Jan 26 '17 at 10:00
  • that's what I did, I used this tutorial to generate a module with rest plugin, but this question refers to enabling plugin without using restui module. Within this tutorial we can find usage of restui and I'm looking for drupal build-in way to do that. It has to be done some way, without it core rest modules would be useless. – David Jan 26 '17 at 10:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.