diff core/modules/rest/rest.post_update.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/modules/rest/rest.post_update.php	Thu Jul 05 14:24:15 2018 +0000
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * @file
+ * Post update functions for Rest.
+ */
+
+use Drupal\rest\Entity\RestResourceConfig;
+use Drupal\rest\RestResourceConfigInterface;
+
+/**
+ * Create REST resource configuration entities.
+ *
+ * @see rest_update_8201()
+ * @see https://www.drupal.org/node/2308745
+ */
+function rest_post_update_create_rest_resource_config_entities() {
+  $resources = \Drupal::state()->get('rest_update_8201_resources', []);
+  foreach ($resources as $key => $resource) {
+    $resource = RestResourceConfig::create([
+      'id' => str_replace(':', '.', $key),
+      'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
+      'configuration' => $resource,
+    ]);
+    $resource->save();
+  }
+}
+
+/**
+ * Simplify method-granularity REST resource config to resource-granularity.
+ *
+ * @see https://www.drupal.org/node/2721595
+ */
+function rest_post_update_resource_granularity() {
+  /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_config_entities */
+  $resource_config_entities = RestResourceConfig::loadMultiple();
+
+  foreach ($resource_config_entities as $resource_config_entity) {
+    if ($resource_config_entity->get('granularity') === RestResourceConfigInterface::METHOD_GRANULARITY) {
+      $configuration = $resource_config_entity->get('configuration');
+
+      $format_and_auth_configuration = [];
+      foreach (array_keys($configuration) as $method) {
+        $format_and_auth_configuration['format'][$method] = implode(',', $configuration[$method]['supported_formats']);
+        $format_and_auth_configuration['auth'][$method] = implode(',', $configuration[$method]['supported_auth']);
+      }
+
+      // If each method has the same formats and the same authentication
+      // providers configured, convert it to 'granularity: resource', which has
+      // a simpler/less verbose configuration.
+      if (count(array_unique($format_and_auth_configuration['format'])) === 1 && count(array_unique($format_and_auth_configuration['auth'])) === 1) {
+        $first_method = array_keys($configuration)[0];
+        $resource_config_entity->set('configuration', [
+          'methods' => array_keys($configuration),
+          'formats' => $configuration[$first_method]['supported_formats'],
+          'authentication' => $configuration[$first_method]['supported_auth']
+        ]);
+        $resource_config_entity->set('granularity', RestResourceConfigInterface::RESOURCE_GRANULARITY);
+        $resource_config_entity->save();
+      }
+    }
+  }
+}