comparison core/modules/rest/rest.install @ 0:4c8ae668cc8c

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