Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Created February 15, 2016 18:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmolivas/a213464321034c7df3c7 to your computer and use it in GitHub Desktop.
Save jmolivas/a213464321034c7df3c7 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\my_module\Plugin\rest\resource\MyRestResource.
*/
namespace Drupal\my_module\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.
*
* @RestResource(
* id = "my_rest",
* label = @Translation("My REST end-point"),
* uri_paths = {
* "canonical" = "/api/{resource}"
* }
* )
*/
class MyRestResource 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('rest'),
$container->get('current_user')
);
}
/**
* Responds to PUT requests.
*
* Returns a list of bundles for specified entity.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function put($resource) {
return new ResourceResponse("Implement REST State PUT : " . $resource);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment