Created
September 20, 2016 16:14
-
-
Save MKorostoff/2fb7ab77f08b053d1944c4591e3cbdd7 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
link_domain: null | |
_core: | |
default_config_hash: E9VXRiWZNet4YVBv8j9WQmTlgb-rOjo0MiCSdgV0Guw | |
bc_entity_resource_permissions: true | |
resources: | |
todo_rest_resource: | |
GET: | |
supported_formats: | |
- json | |
supported_auth: | |
- cookie | |
POST: | |
supported_formats: | |
- json | |
supported_auth: | |
- cookie | |
PUT: | |
supported_formats: | |
- json | |
supported_auth: | |
- cookie | |
PATCH: | |
supported_formats: | |
- json | |
supported_auth: | |
- cookie | |
'entity:node': | |
GET: | |
supported_formats: | |
- json | |
supported_auth: | |
- cookie |
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 | |
namespace Drupal\todo\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 Psr\Log\LoggerInterface; | |
use \Drupal\serialization\Normalizer\NormalizerBase; | |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | |
/** | |
* Provides a resource to get view modes by entity and bundle. | |
* | |
* @RestResource( | |
* id = "todo_rest_resource", | |
* label = @Translation("Todo rest resource"), | |
* serialization_class = "Drupal\todo\Plugin\rest\resource\TodoNormalizer", | |
* uri_paths = { | |
* "canonical" = "/todo" | |
* } | |
* ) | |
*/ | |
class TodoRestResource 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('todo'), | |
$container->get('current_user') | |
); | |
} | |
/** | |
* Responds to GET requests. | |
* | |
* Returns a list of bundles for specified entity. | |
* | |
* @throws \Symfony\Component\HttpKernel\Exception\HttpException | |
* Throws exception expected. | |
*/ | |
public function get() { | |
//Find all of the todo nodes created by the current users | |
$query = \Drupal::entityQuery('node'); | |
$query->condition('status', 1) | |
->condition('type', 'todo') | |
->condition('uid', $this->currentUser->getAccount()->id()) | |
->condition('status', 1); | |
$entity_ids = $query->execute(); | |
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($entity_ids); | |
$response = array(); | |
$nodes = array_values($nodes); | |
foreach ($nodes as $key => $node) { | |
$todo_items = $node->field_todo_item->getValue(); | |
foreach ($todo_items as &$item) { | |
$item = explode('|', $item['value']); | |
//@todo add sanity check incase one of these values is not set | |
$item['value'] = $item[0]; | |
$item['status'] = $item[1]; | |
} | |
$response[$key]['title'] = $node->title->value; | |
$response[$key]['created'] = array( | |
"unixTime" => (int) $node->created->value, | |
"mTime" => (int) $node->created->value * 1000, | |
); | |
$response[$key]['changed'] = array( | |
"unixTime" => (int) $node->changed->value, | |
"mTime" => (int) $node->changed->value * 1000, | |
); | |
$response[$key]['todoItems'] = $todo_items; | |
} | |
return new ResourceResponse($response); | |
} | |
public function post() { | |
$response = array( | |
"hello_world" => $data, | |
); | |
return new ResourceResponse($response); | |
} | |
public function put($arg) { | |
$response = array( | |
"hello_world" => $arg, | |
); | |
return new ResourceResponse($response); | |
} | |
public function patch($arg) { | |
return new ResourceResponse('hello patch'); | |
} | |
public function routes() { | |
return parent::routes(); | |
} | |
} | |
class TodoNormalizer extends NormalizerBase implements DenormalizerInterface { | |
protected $supportedInterfaceOrClass = '\stdClass'; | |
public function __construct() { | |
$this->supportedInterfaceOrClass = 'stdClass'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function denormalize($data, $class, $format = NULL, array $context = array()) { | |
$request_data = new \stdClass($data); | |
$request_data->body = $data; | |
return $request_data; | |
} | |
/** | |
* Normalizes an object into a set of arrays/scalars. | |
* | |
* @param object $object object to normalize | |
* @param string $format format the normalization result will be encoded as | |
* @param array $context Context options for the normalizer | |
* | |
* @return array|string|bool|int|float|null | |
*/ | |
public function normalize($object, $format = NULL, array $context = array()) { | |
return $object; | |
} | |
public function supportsNormalization($data, $format = NULL) { | |
return true; | |
} | |
public function supportsDenormalization($data, $type, $format = NULL) { | |
//Validate user inputs here | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment