.../rest/config/optional/rest.resource.entity__node.yml | 2 +- core/modules/rest/config/schema/rest.schema.yml | 15 +++++---------- core/modules/rest/rest.install | 3 ++- core/modules/rest/src/Entity/ConfigDependencies.php | 2 +- core/modules/rest/src/Entity/RestResourceConfig.php | 14 +++++++++++--- core/modules/rest/src/Tests/RESTTestBase.php | 2 +- core/modules/rest/src/Tests/ResourceTest.php | 4 ++-- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/core/modules/rest/config/optional/rest.resource.entity__node.yml b/core/modules/rest/config/optional/rest.resource.entity__node.yml index 8125fa3..c7da93f 100644 --- a/core/modules/rest/config/optional/rest.resource.entity__node.yml +++ b/core/modules/rest/config/optional/rest.resource.entity__node.yml @@ -1,7 +1,7 @@ id: entity__node plugin_id: 'entity:node' +granularity: method configuration: - granularity: method GET: supported_formats: - hal_json diff --git a/core/modules/rest/config/schema/rest.schema.yml b/core/modules/rest/config/schema/rest.schema.yml index a7cf07d..41bc2bf 100644 --- a/core/modules/rest/config/schema/rest.schema.yml +++ b/core/modules/rest/config/schema/rest.schema.yml @@ -7,18 +7,10 @@ rest.settings: type: string label: 'Domain of the relation' -# The base type for REST resource configuration. -rest_resource_base: - type: mapping - mapping: - granularity: - type: string - label: 'Resource configuration granularity' - # Method-level granularity of REST resource configuration. # @todo Add resource-level granularity in https://www.drupal.org/node/2721595. rest_resource.method: - type: rest_resource_base + type: mapping mapping: GET: type: rest_request @@ -59,6 +51,9 @@ rest.resource.*: plugin_id: type: string label: 'REST resource plugin id' + granularity: + type: string + label: 'REST resource configuration granularity' configuration: - type: rest_resource.[granularity] + type: rest_resource.[%parent.granularity] label: 'REST resource configuration' diff --git a/core/modules/rest/rest.install b/core/modules/rest/rest.install index e0597f9..ac4ce9c 100644 --- a/core/modules/rest/rest.install +++ b/core/modules/rest/rest.install @@ -34,7 +34,8 @@ function rest_update_8100() { foreach (\Drupal::config('rest.settings')->get('resources') as $key => $resource) { $resource = RestResourceConfig::create([ 'id' => str_replace(':', '__', $key), - 'configuration' => ['granularity' => 'method'] + $resource, + 'granularity' => 'method', + 'configuration' => $resource, ]); $resource->save(); } diff --git a/core/modules/rest/src/Entity/ConfigDependencies.php b/core/modules/rest/src/Entity/ConfigDependencies.php index ee9a2e5..cbdbd00 100644 --- a/core/modules/rest/src/Entity/ConfigDependencies.php +++ b/core/modules/rest/src/Entity/ConfigDependencies.php @@ -49,7 +49,7 @@ public function __construct(array $available_rest_formats, array $available_rest * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies() */ public function calculateDependencies(RestResourceConfigInterface $rest_config) { - $granularity = $rest_config->get('configuration')['granularity']; + $granularity = $rest_config->get('granularity'); if ($granularity === 'method') { return $this->calculateDependenciesForMethodGranularity($rest_config); } diff --git a/core/modules/rest/src/Entity/RestResourceConfig.php b/core/modules/rest/src/Entity/RestResourceConfig.php index 55ff040..795e2e8 100644 --- a/core/modules/rest/src/Entity/RestResourceConfig.php +++ b/core/modules/rest/src/Entity/RestResourceConfig.php @@ -22,6 +22,7 @@ * config_export = { * "id", * "plugin_id", + * "granularity", * "configuration" * } * ) @@ -43,6 +44,15 @@ class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigI protected $plugin_id; /** + * The REST resource configuration granularity. + * + * @todo Currently only 'method', but https://www.drupal.org/node/2721595 will add 'resource' + * + * @var string + */ + protected $granularity; + + /** * The REST resource configuration. * * @var array @@ -107,8 +117,7 @@ public function getResourcePlugin() { * {@inheritdoc} */ public function getMethods() { - $granularity = $this->configuration['granularity']; - if ($granularity === 'method') { + if ($this->granularity === 'method') { return $this->getMethodsForMethodGranularity(); } else { @@ -124,7 +133,6 @@ public function getMethods() { */ protected function getMethodsForMethodGranularity() { $methods = array_keys($this->configuration); - $methods = array_filter($methods, function($v) { return $v !== 'granularity'; }); return array_map([$this, 'normalizeRestMethod'], $methods); } diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index 8969a6a..41a4ea3 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -272,7 +272,7 @@ protected function enableService($resource_type, $method = 'GET', $format = NULL // get entity by id /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */ $resource_config = $this->resourceConfigStorage->load($resource_config_id); - $resource_config = $resource_config ?: $this->resourceConfigStorage->create(['id' => $resource_config_id, 'configuration' => ['granularity' => 'method']]); + $resource_config = $resource_config ?: $this->resourceConfigStorage->create(['id' => $resource_config_id, 'granularity' => 'method', 'configuration' => []]); if (is_array($format)) { for ($i = 0; $i < count($format); $i++) { diff --git a/core/modules/rest/src/Tests/ResourceTest.php b/core/modules/rest/src/Tests/ResourceTest.php index 1ab3970..c00035d 100644 --- a/core/modules/rest/src/Tests/ResourceTest.php +++ b/core/modules/rest/src/Tests/ResourceTest.php @@ -45,7 +45,7 @@ protected function setUp() { */ public function testFormats() { /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */ - $resource_config = $this->resourceConfigStorage->create(['id' => 'entity__entity_test', 'configuration' => ['granularity' => 'method']]); + $resource_config = $this->resourceConfigStorage->create(['id' => 'entity__entity_test', 'granularity' => 'method', 'configuration' => []]); // Attempt to enable the resource. $resource_config ->addSupportedAuthenticationProvider('GET', 'basic_auth') @@ -68,7 +68,7 @@ public function testFormats() { */ public function testAuthentication() { /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */ - $resource_config = $this->resourceConfigStorage->create(['id' => 'entity__entity_test', 'configuration' => ['granularity' => 'method']]); + $resource_config = $this->resourceConfigStorage->create(['id' => 'entity__entity_test', 'granularity' => 'method', 'configuration' => []]); // Attempt to enable the resource. $resource_config ->addSupportedFormat('GET', 'hal_json')