1 {% extends "base/class.php.twig" %}
4 \Drupal\{{module_name}}\Plugin\rest\resource\{{class_name}}.
7 {% block namespace_class %}
8 namespace Drupal\{{module_name}}\Plugin\rest\resource;
12 use Drupal\Core\Session\AccountProxyInterface;
13 use Drupal\rest\ModifiedResourceResponse;
14 use Drupal\rest\Plugin\ResourceBase;
15 use Drupal\rest\ResourceResponse;
16 use Psr\Log\LoggerInterface;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21 {% block class_declaration %}
23 * Provides a resource to get view modes by entity and bundle.
26 * id = "{{ plugin_id }}",
27 * label = @Translation("{{ plugin_label }}"),
29 * "canonical" = "/{{ plugin_url }}"
33 class {{ class_name }} extends ResourceBase {% endblock %}
35 {% block class_variables %}
37 * A current user instance.
39 * @var \Drupal\Core\Session\AccountProxyInterface
41 protected $currentUser;
44 {% block class_construct %}
47 * Constructs a new {{ class_name }} object.
49 * @param array $configuration
50 * A configuration array containing information about the plugin instance.
51 * @param string $plugin_id
52 * The plugin_id for the plugin instance.
53 * @param mixed $plugin_definition
54 * The plugin implementation definition.
55 * @param array $serializer_formats
56 * The available serialization formats.
57 * @param \Psr\Log\LoggerInterface $logger
59 * @param \Drupal\Core\Session\AccountProxyInterface $current_user
60 * A current user instance.
62 public function __construct(
66 array $serializer_formats,
67 LoggerInterface $logger,
68 AccountProxyInterface $current_user) {
69 parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
71 $this->currentUser = $current_user;
75 {% block class_create %}
80 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
85 $container->getParameter('serializer.formats'),
86 $container->get('logger.factory')->get('{{module_name}}'),
87 $container->get('current_user')
92 {% block class_methods %}
93 {% for state, state_settings in plugin_states %}
96 * Responds to {{ state }} requests.
98 * @param \Drupal\Core\Entity\EntityInterface $entity
101 * @return \Drupal\rest\{{ state_settings.response_class }}
102 * The HTTP response object.
104 * @throws \Symfony\Component\HttpKernel\Exception\HttpException
105 * Throws exception expected.
107 public function {{ state|lower }}(EntityInterface $entity) {
109 // You must to implement the logic of your REST Resource here.
110 // Use current user after pass authentication to validate access.
111 if (!$this->currentUser->hasPermission('access content')) {
112 throw new AccessDeniedHttpException();
115 return new {{ state_settings.response_class }}({{ (state == 'DELETE') ? 'NULL' : '$entity' }}, {{ state_settings.http_code }});