Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Post update functions for Rest.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\rest\Entity\RestResourceConfig;
|
Chris@0
|
9 use Drupal\rest\RestResourceConfigInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Create REST resource configuration entities.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @see rest_update_8201()
|
Chris@0
|
15 * @see https://www.drupal.org/node/2308745
|
Chris@0
|
16 */
|
Chris@0
|
17 function rest_post_update_create_rest_resource_config_entities() {
|
Chris@0
|
18 $resources = \Drupal::state()->get('rest_update_8201_resources', []);
|
Chris@0
|
19 foreach ($resources as $key => $resource) {
|
Chris@0
|
20 $resource = RestResourceConfig::create([
|
Chris@0
|
21 'id' => str_replace(':', '.', $key),
|
Chris@0
|
22 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
|
Chris@0
|
23 'configuration' => $resource,
|
Chris@0
|
24 ]);
|
Chris@0
|
25 $resource->save();
|
Chris@0
|
26 }
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Simplify method-granularity REST resource config to resource-granularity.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @see https://www.drupal.org/node/2721595
|
Chris@0
|
33 */
|
Chris@0
|
34 function rest_post_update_resource_granularity() {
|
Chris@0
|
35 /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_config_entities */
|
Chris@0
|
36 $resource_config_entities = RestResourceConfig::loadMultiple();
|
Chris@0
|
37
|
Chris@0
|
38 foreach ($resource_config_entities as $resource_config_entity) {
|
Chris@0
|
39 if ($resource_config_entity->get('granularity') === RestResourceConfigInterface::METHOD_GRANULARITY) {
|
Chris@0
|
40 $configuration = $resource_config_entity->get('configuration');
|
Chris@0
|
41
|
Chris@0
|
42 $format_and_auth_configuration = [];
|
Chris@0
|
43 foreach (array_keys($configuration) as $method) {
|
Chris@0
|
44 $format_and_auth_configuration['format'][$method] = implode(',', $configuration[$method]['supported_formats']);
|
Chris@0
|
45 $format_and_auth_configuration['auth'][$method] = implode(',', $configuration[$method]['supported_auth']);
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 // If each method has the same formats and the same authentication
|
Chris@0
|
49 // providers configured, convert it to 'granularity: resource', which has
|
Chris@0
|
50 // a simpler/less verbose configuration.
|
Chris@0
|
51 if (count(array_unique($format_and_auth_configuration['format'])) === 1 && count(array_unique($format_and_auth_configuration['auth'])) === 1) {
|
Chris@0
|
52 $first_method = array_keys($configuration)[0];
|
Chris@0
|
53 $resource_config_entity->set('configuration', [
|
Chris@0
|
54 'methods' => array_keys($configuration),
|
Chris@0
|
55 'formats' => $configuration[$first_method]['supported_formats'],
|
Chris@17
|
56 'authentication' => $configuration[$first_method]['supported_auth'],
|
Chris@0
|
57 ]);
|
Chris@0
|
58 $resource_config_entity->set('granularity', RestResourceConfigInterface::RESOURCE_GRANULARITY);
|
Chris@0
|
59 $resource_config_entity->save();
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@17
|
64
|
Chris@17
|
65 /**
|
Chris@17
|
66 * Clear caches due to changes in route definitions.
|
Chris@17
|
67 */
|
Chris@17
|
68 function rest_post_update_161923() {
|
Chris@17
|
69 // Empty post-update hook.
|
Chris@17
|
70 }
|