1<?php 2 3/** 4 * @file 5 * Install, update and uninstall functions for the rest module. 6 */ 7 8use Drupal\Core\Config\Entity\ConfigEntityType; 9use Drupal\Core\StringTranslation\TranslatableMarkup; 10 11/** 12 * Install the REST config entity type and fix old settings-based config. 13 * 14 * @see rest_post_update_create_rest_resource_config_entities() 15 */ 16function rest_update_8201() { 17 \Drupal::entityDefinitionUpdateManager()->installEntityType(new ConfigEntityType([ 18 'id' => 'rest_resource_config', 19 'label' => new TranslatableMarkup('REST resource configuration'), 20 'config_prefix' => 'resource', 21 'admin_permission' => 'administer rest resources', 22 'label_callback' => 'getLabelFromPlugin', 23 'entity_keys' => ['id' => 'id'], 24 'config_export' => [ 25 'id', 26 'plugin_id', 27 'granularity', 28 'configuration', 29 ], 30 ])); 31 \Drupal::state()->set('rest_update_8201_resources', \Drupal::config('rest.settings')->get('resources')); 32 \Drupal::configFactory()->getEditable('rest.settings') 33 ->clear('resources') 34 ->save(); 35} 36 37/** 38 * Re-save all views with a REST display to add new auth defaults. 39 */ 40function rest_update_8202() { 41 $config_factory = \Drupal::configFactory(); 42 foreach ($config_factory->listAll('views.view.') as $view_config_name) { 43 $save = FALSE; 44 $view = $config_factory->getEditable($view_config_name); 45 $displays = $view->get('display'); 46 foreach ($displays as $display_name => &$display) { 47 if ($display['display_plugin'] == 'rest_export') { 48 if (!isset($display['display_options']['auth'])) { 49 $display['display_options']['auth'] = []; 50 $save = TRUE; 51 } 52 } 53 } 54 if ($save) { 55 $view->set('display', $displays); 56 $view->save(TRUE); 57 } 58 } 59} 60 61/** 62 * Enable BC for EntityResource: continue to use permissions. 63 */ 64function rest_update_8203() { 65 $config_factory = \Drupal::configFactory(); 66 $rest_settings = $config_factory->getEditable('rest.settings'); 67 $rest_settings->set('bc_entity_resource_permissions', TRUE) 68 ->save(TRUE); 69} 70 71/** 72 * Ensure the right REST authentication method is used. 73 * 74 * This fixes the bug in https://www.drupal.org/node/2825204. 75 */ 76function rest_update_8401() { 77 $config_factory = \Drupal::configFactory(); 78 $auth_providers = \Drupal::service('authentication_collector')->getSortedProviders(); 79 $process_auth = function ($auth_option) use ($auth_providers) { 80 foreach ($auth_providers as $provider_id => $provider_data) { 81 // The provider belongs to the module that declares it as a service. 82 if (strtok($provider_data->_serviceId, '.') === $auth_option) { 83 return $provider_id; 84 } 85 } 86 87 return $auth_option; 88 }; 89 90 foreach ($config_factory->listAll('views.view.') as $view_config_name) { 91 $save = FALSE; 92 $view = $config_factory->getEditable($view_config_name); 93 $displays = $view->get('display'); 94 foreach ($displays as $display_name => $display) { 95 if ('rest_export' === $display['display_plugin'] && !empty($display['display_options']['auth'])) { 96 $displays[$display_name]['display_options']['auth'] = array_map($process_auth, $display['display_options']['auth']); 97 $save = TRUE; 98 } 99 } 100 if ($save) { 101 $view->set('display', $displays); 102 $view->save(TRUE); 103 } 104 } 105} 106