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\Plugin\ResourceBase;
14 use Drupal\rest\ResourceResponse;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
17 use Psr\Log\LoggerInterface;
20 {% block class_declaration %}
22 * Provides a resource to get view modes by entity and bundle.
25 * id = "{{ plugin_id }}",
26 * label = @Translation("{{ plugin_label }}"),
28 * "canonical" = "/{{ plugin_url }}"
32 class {{ class_name }} extends ResourceBase {% endblock %}
34 {% block class_variables %}
36 * A current user instance.
38 * @var \Drupal\Core\Session\AccountProxyInterface
40 protected $currentUser;
43 {% block class_construct %}
46 * Constructs a new {{ class_name }} object.
48 * @param array $configuration
49 * A configuration array containing information about the plugin instance.
50 * @param string $plugin_id
51 * The plugin_id for the plugin instance.
52 * @param mixed $plugin_definition
53 * The plugin implementation definition.
54 * @param array $serializer_formats
55 * The available serialization formats.
56 * @param \Psr\Log\LoggerInterface $logger
58 * @param \Drupal\Core\Session\AccountProxyInterface $current_user
59 * A current user instance.
61 public function __construct(
65 array $serializer_formats,
66 LoggerInterface $logger,
67 AccountProxyInterface $current_user) {
68 parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
70 $this->currentUser = $current_user;
74 {% block class_create %}
79 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
84 $container->getParameter('serializer.formats'),
85 $container->get('logger.factory')->get('{{module_name}}'),
86 $container->get('current_user')
90 {% block class_methods %}
91 {% for state in plugin_states %}
94 * Responds to {{ state }} requests.
96 * Returns a list of bundles for specified entity.
98 * @throws \Symfony\Component\HttpKernel\Exception\HttpException
99 * Throws exception expected.
101 public function {{ state|lower }}() {
103 // You must to implement the logic of your REST Resource here.
104 // Use current user after pass authentication to validate access.
105 if (!$this->currentUser->hasPermission('access content')) {
106 throw new AccessDeniedHttpException();
109 return new ResourceResponse("Implement REST State {{ state }}!");