{% extends "base/class.php.twig" %} {% block file_path %} \Drupal\{{module_name}}\Plugin\rest\resource\{{class_name}}. {% endblock %} {% block namespace_class %} namespace Drupal\{{module_name}}\Plugin\rest\resource; {% endblock %} {% block use_class %} use Drupal\rest\ModifiedResourceResponse; use Drupal\rest\Plugin\ResourceBase; use Drupal\rest\ResourceResponse; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; {% endblock %} {% block class_declaration %} /** * Provides a resource to get view modes by entity and bundle. * * @RestResource( * id = "{{ plugin_id }}", * label = @Translation("{{ plugin_label }}"), * uri_paths = { {% for state_settings in plugin_states %} * "{{ state_settings.uri_paths }}" = "{{ plugin_url }}" {% endfor %} * } * ) */ class {{ class_name }} extends ResourceBase {% endblock %} {% block class_variables %} /** * A current user instance. * * @var \Drupal\Core\Session\AccountProxyInterface */ protected $currentUser; {% endblock %} {% block class_create %} /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); $instance->logger = $container->get('logger.factory')->get('{{module_name}}'); $instance->currentUser = $container->get('current_user'); return $instance; } {% endblock %} {% block class_methods %} {% for state, state_settings in plugin_states %} /** * Responds to {{ state }} requests. * * @param string $payload * * @return \Drupal\rest\{{ state_settings.response_class }} * The HTTP response object. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * Throws exception expected. */ public function {{ state|lower }}($payload) { // You must to implement the logic of your REST Resource here. // Use current user after pass authentication to validate access. if (!$this->currentUser->hasPermission('access content')) { throw new AccessDeniedHttpException(); } return new {{ state_settings.response_class }}({{ (state == 'DELETE') ? 'NULL' : '$payload' }}, {{ state_settings.http_code }}); } {% endfor %} {% endblock %}