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