Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / templates / module / src / Plugin / Rest / Resource / rest.php.twig
1 {% extends "base/class.php.twig" %}
2
3 {% block file_path %}
4 \Drupal\{{module_name}}\Plugin\rest\resource\{{class_name}}.
5 {% endblock %}
6
7 {% block namespace_class %}
8 namespace Drupal\{{module_name}}\Plugin\rest\resource;
9 {% endblock %}
10
11 {% block use_class %}
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;
18 {% endblock %}
19
20 {% block class_declaration %}
21 /**
22  * Provides a resource to get view modes by entity and bundle.
23  *
24  * @RestResource(
25  *   id = "{{ plugin_id }}",
26  *   label = @Translation("{{ plugin_label }}"),
27  *   uri_paths = {
28  *     "canonical" = "/{{ plugin_url }}"
29  *   }
30  * )
31  */
32 class {{ class_name }} extends ResourceBase {% endblock %}
33
34 {% block class_variables %}
35   /**
36    * A current user instance.
37    *
38    * @var \Drupal\Core\Session\AccountProxyInterface
39    */
40   protected $currentUser;
41 {% endblock %}
42
43 {% block class_construct %}
44
45   /**
46    * Constructs a new {{ class_name }} object.
47    *
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
57    *   A logger instance.
58    * @param \Drupal\Core\Session\AccountProxyInterface $current_user
59    *   A current user instance.
60    */
61   public function __construct(
62     array $configuration,
63     $plugin_id,
64     $plugin_definition,
65     array $serializer_formats,
66     LoggerInterface $logger,
67     AccountProxyInterface $current_user) {
68     parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
69
70     $this->currentUser = $current_user;
71   }
72 {% endblock %}
73
74 {% block class_create %}
75
76   /**
77    * {@inheritdoc}
78    */
79   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
80     return new static(
81       $configuration,
82       $plugin_id,
83       $plugin_definition,
84       $container->getParameter('serializer.formats'),
85       $container->get('logger.factory')->get('{{module_name}}'),
86       $container->get('current_user')
87     );
88   }
89 {% endblock %}
90 {% block class_methods %}
91 {% for state in plugin_states %}
92
93   /**
94    * Responds to {{ state }} requests.
95    *
96    * Returns a list of bundles for specified entity.
97    *
98    * @throws \Symfony\Component\HttpKernel\Exception\HttpException
99    *   Throws exception expected.
100    */
101   public function {{ state|lower }}() {
102
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();
107     }
108
109     return new ResourceResponse("Implement REST State {{ state }}!");
110   }
111 {% endfor %}
112 {% endblock %}