5 * Install, update and uninstall functions for the rest module.
8 use Drupal\Core\Config\Entity\ConfigEntityType;
9 use Drupal\Core\StringTranslation\TranslatableMarkup;
12 * Implements hook_requirements().
14 function rest_requirements($phase) {
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.'),
29 * Install the REST config entity type and fix old settings-based config.
31 * @see rest_post_update_create_rest_resource_config_entities()
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'],
48 \Drupal::state()->set('rest_update_8201_resources', \Drupal::config('rest.settings')->get('resources'));
49 \Drupal::configFactory()->getEditable('rest.settings')
55 * Re-save all views with a REST display to add new auth defaults.
57 function rest_update_8202() {
58 $config_factory = \Drupal::configFactory();
59 foreach ($config_factory->listAll('views.view.') as $view_config_name) {
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'] = [];
72 $view->set('display', $displays);
79 * Enable BC for EntityResource: continue to use permissions.
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)
89 * Ensure the right REST authentication method is used.
91 * This fixes the bug in https://www.drupal.org/node/2825204.
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) {
107 foreach ($config_factory->listAll('views.view.') as $view_config_name) {
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']);
118 $view->set('display', $displays);