function RESTTestBase::enableService

Enables the REST service interface for a specific entity type.

Parameters

string|false $resource_type: The resource type that should get REST API enabled or FALSE to disable all resource types.

string $method: The HTTP method to enable, e.g. GET, POST etc.

string|array $format: (Optional) The serialization format, e.g. hal_json, or a list of formats.

array $auth: (Optional) The list of valid authentication methods.

File

core/modules/rest/src/Tests/RESTTestBase.php, line 381

Class

RESTTestBase
Test helper class that provides a REST client method to send HTTP requests.

Namespace

Drupal\rest\Tests

Code

protected function enableService($resource_type, $method = 'GET', $format = NULL, array $auth = []) {
    if ($resource_type) {
        // Enable REST API for this entity type.
        $resource_config_id = str_replace(':', '.', $resource_type);
        // get entity by id
        
        /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
        $resource_config = $this->resourceConfigStorage
            ->load($resource_config_id);
        if (!$resource_config) {
            $resource_config = $this->resourceConfigStorage
                ->create([
                'id' => $resource_config_id,
                'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
                'configuration' => [],
            ]);
        }
        $configuration = $resource_config->get('configuration');
        if (is_array($format)) {
            for ($i = 0; $i < count($format); $i++) {
                $configuration[$method]['supported_formats'][] = $format[$i];
            }
        }
        else {
            if ($format == NULL) {
                $format = $this->defaultFormat;
            }
            $configuration[$method]['supported_formats'][] = $format;
        }
        if (!is_array($auth) || empty($auth)) {
            $auth = $this->defaultAuth;
        }
        foreach ($auth as $auth_provider) {
            $configuration[$method]['supported_auth'][] = $auth_provider;
        }
        $resource_config->set('configuration', $configuration);
        $resource_config->save();
    }
    else {
        foreach ($this->resourceConfigStorage
            ->loadMultiple() as $resource_config) {
            $resource_config->delete();
        }
    }
    $this->rebuildCache();
}

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