2

A couple months back I successfully setup a development site to learn about the Drupal 8 REST API with version 8.0.x using the REST UI module for configuration. When I heard about the improvements to the REST Services in Drupal 8.2 I was pretty excited (from my understanding it is now possible to access configuration data such as content types and taxonomy terms) so I setup a new install using version 8.2.1. However, understanding the configuration for 8.2 is pretty daunting, and since it's so new there are very little resources around the setup and configuration.

I was able to locate this resource and I imported the example configuration provided, but was still unsure on how to configure other REST Endpoints.

I used the default configuration provided in the above resource. It looks like this:

langcode: en
status: true
dependencies:
 module:
   - basic_auth
   - hal
   - node
id: entity.node
plugin_id: 'entity:node'
granularity: resource
configuration:
 methods:
   - GET
   - POST
   - PATCH
   - DELETE
 formats:
   - hal_json
 authentication:
   - basic_auth
  • How do I edit the configuration once it's imported via the configuration manager?
  • Where can I find the available plugin_id's so that I can configure other resources? Where can I learn more about plugins?
  • How would I begin to implement my own REST endpoint?

I know this is a lot to ask, but at the time of writing there is little to no information covering these topics so I figure other people might be confused as well. I would like to create a guide on this, but that would first require that I understand it first.

  • There is no user interface for Restful interfaces in core (other than Views Rest exports). However there is a contrib module, restui, that adds one, but unfortunately as of 10/13/2016 it is not up-to-date with 8.2.x changes. There is an issue and a patch for it though - drupal.org/node/2758563. At the moment you will need to import configuration i.e. sync configuration changes via YAML. – mradcliffe Oct 13 '16 at 18:47
  • A lot of the current documentation pages for developers are in flux as we make improvements to drupal.org's Documentation section. One of those sections was on plugins and is now missing from the Develop for Drupal page, but that is where I would go prior to this. – mradcliffe Oct 13 '16 at 18:49
  • Found it, it's now at drupal.org/docs/8/api – mradcliffe Oct 13 '16 at 18:50
  • @mradcliffe So I just need to create a rest-config directory in my drupal installation and put all of my .yml config files in there and then copy / paste them into the single import when I want to update them? I'm still a bit confused on how I would discover the rest plugins available by default. Maybe you could post a resource config of another resource then I might begin to see the pattern? – Dan Lucas Oct 13 '16 at 19:22
1

I search a lot too after 8.2x and this is how i got it working :

Example for a custom user ressource :

myModule/config/install

rest.resource.user.rest_ressource.yml

langcode: en
status: true
dependencies:
  module:
    - basic_auth
id: user.rest_ressource
plugin_id: 'user_rest_ressource'
granularity: resource
configuration:
  methods:
    - GET
  formats:
    - json
  authentication:
    - basic_auth

In myModule/src/Plugin/rest/resource

UserRestRessource.php

<?php

namespace Drupal\myModule\Plugin\rest\resource;

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;


/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "user_rest_ressource",
 *   label = @Translation("User Rest"),
 *   uri_paths = {
 *     "canonical" = "/api/user/getInfo"
 *   }
 * )
 */
class UserRestRessource extends ResourceBase {

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Constructs a Drupal\rest\Plugin\ResourceBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param array $serializer_formats
   *   The available serialization formats.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   A current user instance.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    array $serializer_formats,
    LoggerInterface $logger,
    AccountProxyInterface $current_user) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);

    $this->currentUser = $current_user;

  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->getParameter('serializer.formats'),
      $container->get('logger.factory')->get('restimadiff'),
      $container->get('current_user')
    );
  }

  /**
   * Responds to GET requests.
   *
   * 
   *
   * @throws \Symfony\Component\HttpKernel\Exception\HttpException
   *   Throws exception expected.
   */
  public function get() {

      $responseResource=new ResourceResponse(
          array()

      );  
    return $responseResource;
  }

}
  • Do you have to add rest via a module? – dibs Apr 30 '17 at 23:52
-2

Since posting this, the documentation on drupal.org/docs/8/ has been updated appropriately.

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.