e2a620d958cb0d1a6ef12e02b60bcde38ae9c191
[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\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;
19 {% endblock %}
20
21 {% block class_declaration %}
22 /**
23  * Provides a resource to get view modes by entity and bundle.
24  *
25  * @RestResource(
26  *   id = "{{ plugin_id }}",
27  *   label = @Translation("{{ plugin_label }}"),
28  *   uri_paths = {
29  *     "canonical" = "/{{ plugin_url }}"
30  *   }
31  * )
32  */
33 class {{ class_name }} extends ResourceBase {% endblock %}
34
35 {% block class_variables %}
36   /**
37    * A current user instance.
38    *
39    * @var \Drupal\Core\Session\AccountProxyInterface
40    */
41   protected $currentUser;
42 {% endblock %}
43
44 {% block class_construct %}
45
46   /**
47    * Constructs a new {{ class_name }} object.
48    *
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
58    *   A logger instance.
59    * @param \Drupal\Core\Session\AccountProxyInterface $current_user
60    *   A current user instance.
61    */
62   public function __construct(
63     array $configuration,
64     $plugin_id,
65     $plugin_definition,
66     array $serializer_formats,
67     LoggerInterface $logger,
68     AccountProxyInterface $current_user) {
69     parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
70
71     $this->currentUser = $current_user;
72   }
73 {% endblock %}
74
75 {% block class_create %}
76
77   /**
78    * {@inheritdoc}
79    */
80   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
81     return new static(
82       $configuration,
83       $plugin_id,
84       $plugin_definition,
85       $container->getParameter('serializer.formats'),
86       $container->get('logger.factory')->get('{{module_name}}'),
87       $container->get('current_user')
88     );
89   }
90 {% endblock %}
91
92 {% block class_methods %}
93 {% for state, state_settings in plugin_states %}
94
95   /**
96    * Responds to {{ state }} requests.
97    *
98    * @param \Drupal\Core\Entity\EntityInterface $entity
99    *   The entity object.
100    *
101    * @return \Drupal\rest\{{ state_settings.response_class }}
102    *   The HTTP response object.
103    *
104    * @throws \Symfony\Component\HttpKernel\Exception\HttpException
105    *   Throws exception expected.
106    */
107   public function {{ state|lower }}(EntityInterface $entity) {
108
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();
113     }
114
115     return new {{ state_settings.response_class }}({{ (state == 'DELETE') ? 'NULL' : '$entity' }}, {{ state_settings.http_code }});
116   }
117 {% endfor %}
118 {% endblock %}