rest.install 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * Implements hook_requirements().
  10. */
  11. function rest_requirements($phase) {
  12. $requirements = [];
  13. if ($phase == 'runtime' && PHP_SAPI !== 'cli' && version_compare(PHP_VERSION, '5.6.0', '>=') && version_compare(PHP_VERSION, '7', '<') && ini_get('always_populate_raw_post_data') != -1) {
  14. $requirements['always_populate_raw_post_data'] = [
  15. 'title' => t('always_populate_raw_post_data PHP setting'),
  16. 'value' => t('Not set to -1.'),
  17. 'severity' => REQUIREMENT_ERROR,
  18. 'description' => t('The always_populate_raw_post_data PHP setting should be set to -1 in PHP version 5.6. Please check the <a href="https://php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data">PHP manual</a> for information on how to correct this.'),
  19. ];
  20. }
  21. return $requirements;
  22. }
  23. /**
  24. * Install the REST config entity type and fix old settings-based config.
  25. *
  26. * @see rest_post_update_create_rest_resource_config_entities()
  27. */
  28. function rest_update_8201() {
  29. \Drupal::entityDefinitionUpdateManager()->installEntityType(new ConfigEntityType([
  30. 'id' => 'rest_resource_config',
  31. 'label' => new TranslatableMarkup('REST resource configuration'),
  32. 'config_prefix' => 'resource',
  33. 'admin_permission' => 'administer rest resources',
  34. 'label_callback' => 'getLabelFromPlugin',
  35. 'entity_keys' => ['id' => 'id'],
  36. 'config_export' => [
  37. 'id',
  38. 'plugin_id',
  39. 'granularity',
  40. 'configuration',
  41. ],
  42. ]));
  43. \Drupal::state()->set('rest_update_8201_resources', \Drupal::config('rest.settings')->get('resources'));
  44. \Drupal::configFactory()->getEditable('rest.settings')
  45. ->clear('resources')
  46. ->save();
  47. }
  48. /**
  49. * Re-save all views with a REST display to add new auth defaults.
  50. */
  51. function rest_update_8202() {
  52. $config_factory = \Drupal::configFactory();
  53. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  54. $save = FALSE;
  55. $view = $config_factory->getEditable($view_config_name);
  56. $displays = $view->get('display');
  57. foreach ($displays as $display_name => &$display) {
  58. if ($display['display_plugin'] == 'rest_export') {
  59. if (!isset($display['display_options']['auth'])) {
  60. $display['display_options']['auth'] = [];
  61. $save = TRUE;
  62. }
  63. }
  64. }
  65. if ($save) {
  66. $view->set('display', $displays);
  67. $view->save(TRUE);
  68. }
  69. }
  70. }
  71. /**
  72. * Enable BC for EntityResource: continue to use permissions.
  73. */
  74. function rest_update_8203() {
  75. $config_factory = \Drupal::configFactory();
  76. $rest_settings = $config_factory->getEditable('rest.settings');
  77. $rest_settings->set('bc_entity_resource_permissions', TRUE)
  78. ->save(TRUE);
  79. }
  80. /**
  81. * Ensure the right REST authentication method is used.
  82. *
  83. * This fixes the bug in https://www.drupal.org/node/2825204.
  84. */
  85. function rest_update_8401() {
  86. $config_factory = \Drupal::configFactory();
  87. $auth_providers = \Drupal::service('authentication_collector')->getSortedProviders();
  88. $process_auth = function ($auth_option) use ($auth_providers) {
  89. foreach ($auth_providers as $provider_id => $provider_data) {
  90. // The provider belongs to the module that declares it as a service.
  91. if (strtok($provider_data->_serviceId, '.') === $auth_option) {
  92. return $provider_id;
  93. }
  94. }
  95. return $auth_option;
  96. };
  97. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  98. $save = FALSE;
  99. $view = $config_factory->getEditable($view_config_name);
  100. $displays = $view->get('display');
  101. foreach ($displays as $display_name => $display) {
  102. if ('rest_export' === $display['display_plugin'] && !empty($display['display_options']['auth'])) {
  103. $displays[$display_name]['display_options']['auth'] = array_map($process_auth, $display['display_options']['auth']);
  104. $save = TRUE;
  105. }
  106. }
  107. if ($save) {
  108. $view->set('display', $displays);
  109. $view->save(TRUE);
  110. }
  111. }
  112. }