Created
April 21, 2016 06:18
-
-
Save malabya/809622e4b92c342e03ca3d5804a4d86c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains Drupal\custom_rest\Plugin\rest\resource\custom_rest. | |
*/ | |
namespace Drupal\custom_rest\Plugin\rest\resource; | |
use Drupal\Core\Session\AccountProxyInterface; | |
use Drupal\rest\Plugin\ResourceBase; | |
use Drupal\rest\ResourceResponse; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use Psr\Log\LoggerInterface; | |
/** | |
* Provides a resource to get view modes by entity and bundle. | |
* | |
* @RestResource( | |
* id = "custom_rest_resource", | |
* label = @Translation("Custom rest resource"), | |
* uri_paths = { | |
* "canonical" = "//api/custom" | |
* } | |
* ) | |
*/ | |
class CustomRestResource 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 POST requests. | |
* | |
* Returns a list of bundles for specified entity. | |
* | |
* @throws \Symfony\Component\HttpKernel\Exception\HttpException | |
* Throws exception expected. | |
*/ | |
public function post() { | |
// You must to implement the logic of your REST Resource here. | |
// Use current user after pass authentication to validate access. | |
/* | |
if(!$this->currentUser->hasPermission($permission)) { | |
throw new AccessDeniedHttpException(); | |
} | |
*/ | |
// Throw an exception if it is required. | |
// throw new HttpException(t('Throw an exception if it is required.')); | |
return new ResourceResponse("Implement REST State POST!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment