rest.install 3.2 KB

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