rest.install 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 (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. }