function ResourceBase::routes

Same name in other branches
  1. 9 core/modules/rest/src/Plugin/ResourceBase.php \Drupal\rest\Plugin\ResourceBase::routes()
  2. 10 core/modules/rest/src/Plugin/ResourceBase.php \Drupal\rest\Plugin\ResourceBase::routes()
  3. 11.x core/modules/rest/src/Plugin/ResourceBase.php \Drupal\rest\Plugin\ResourceBase::routes()

Overrides ResourceInterface::routes

File

core/modules/rest/src/Plugin/ResourceBase.php, line 99

Class

ResourceBase
Common base class for resource plugins.

Namespace

Drupal\rest\Plugin

Code

public function routes() {
    $collection = new RouteCollection();
    $definition = $this->getPluginDefinition();
    $canonical_path = isset($definition['uri_paths']['canonical']) ? $definition['uri_paths']['canonical'] : '/' . strtr($this->pluginId, ':', '/') . '/{id}';
    $create_path = isset($definition['uri_paths']['create']) ? $definition['uri_paths']['create'] : '/' . strtr($this->pluginId, ':', '/');
    // BC: the REST module originally created the POST URL for a resource by
    // reading the 'https://www.drupal.org/link-relations/create' URI path from
    // the plugin annotation. For consistency with entity type definitions, that
    // then changed to reading the 'create' URI path. For any REST Resource
    // plugins that were using the old mechanism, we continue to support that.
    if (!isset($definition['uri_paths']['create']) && isset($definition['uri_paths']['https://www.drupal.org/link-relations/create'])) {
        @trigger_error('The "https://www.drupal.org/link-relations/create" string as a RestResource plugin annotation URI path key is deprecated in Drupal 8.4.0, now a valid link relation type name must be specified, so "create" must be specified instead before Drupal 9.0.0. See https://www.drupal.org/node/2737401.', E_USER_DEPRECATED);
        $create_path = $definition['uri_paths']['https://www.drupal.org/link-relations/create'];
    }
    $route_name = strtr($this->pluginId, ':', '.');
    $methods = $this->availableMethods();
    foreach ($methods as $method) {
        $path = $method === 'POST' ? $create_path : $canonical_path;
        $route = $this->getBaseRoute($path, $method);
        // Note that '_format' and '_content_type_format' route requirements are
        // added in ResourceRoutes::getRoutesForResourceConfig().
        $collection->add("{$route_name}.{$method}", $route);
        // BC: the REST module originally created per-format GET routes, instead
        // of a single route. To minimize the surface of this BC layer, this uses
        // route definitions that are as empty as possible, plus an outbound route
        // processor.
        // @see \Drupal\rest\RouteProcessor\RestResourceGetRouteProcessorBC
        if ($method === 'GET' || $method === 'HEAD') {
            foreach ($this->serializerFormats as $format_name) {
                $collection->add("{$route_name}.{$method}.{$format_name}", (new BcRoute())->setRequirement('_format', $format_name));
            }
        }
    }
    return $collection;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.