{% 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\Core\Session\AccountProxyInterface; use Drupal\rest\Plugin\ResourceBase; use Drupal\rest\ResourceResponse; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Psr\Log\LoggerInterface; {% 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 = { * "canonical" = "/{{ plugin_url }}" * } * ) */ class {{ class_name }} extends ResourceBase {% endblock %} {% block class_variables %} /** * A current user instance. * * @var \Drupal\Core\Session\AccountProxyInterface */ protected $currentUser; {% endblock %} {% block class_construct %} /** * Constructs a new {{ class_name }} object. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param array $serializer_formats * The available serialization formats. * @param \Psr\Log\LoggerInterface $logger * A logger instance. * @param \Drupal\Core\Session\AccountProxyInterface $current_user * A current user instance. */ public function __construct( array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, AccountProxyInterface $current_user) { parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); $this->currentUser = $current_user; } {% endblock %} {% block class_create %} /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->getParameter('serializer.formats'), $container->get('logger.factory')->get('{{module_name}}'), $container->get('current_user') ); } {% endblock %} {% block class_methods %} {% for state in plugin_states %} /** * Responds to {{ state }} requests. * * Returns a list of bundles for specified entity. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException * Throws exception expected. */ public function {{ state|lower }}() { // 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 ResourceResponse("Implement REST State {{ state }}!"); } {% endfor %} {% endblock %}