5 * Update hooks for the Redirect module.
8 use Drupal\redirect\Entity\Redirect;
9 use Drupal\Core\Database\Database;
10 use Drupal\system\Entity\Action;
11 use Drupal\views\Entity\View;
12 use Symfony\Component\Yaml\Yaml;
13 use Drupal\Core\Config\InstallStorage;
14 use Drupal\Core\Config\FileStorage;
15 use Drupal\user\Entity\Role;
18 * Rehash redirects to account for case insensitivity.
20 function redirect_update_8100(&$sandbox) {
21 // Loop through 100 redirects at a time.
22 if (!isset($sandbox['progress'])) {
23 $sandbox['progress'] = 0;
24 $sandbox['current_rid'] = 0;
25 // Note, because MySQL can treat `foo = LOWER(foo)`, all records must be checked.
26 $sandbox['max'] = db_query('SELECT COUNT(1) FROM {redirect}')->fetchField();
29 $result = \Drupal::database()->select('redirect', 'r')
30 ->fields('r', ['rid', 'redirect_source__path', 'redirect_source__query', 'language', 'hash'])
31 ->condition('rid', $sandbox['current_rid'], '>')
33 ->orderBy('rid', 'ASC')
36 foreach ($result as $row) {
37 $query = !empty($row->redirect_source__query) ? unserialize($row->redirect_source__query): [];
38 $new_hash = Redirect::generateHash($row->redirect_source__path, (array) $query, $row->language);
39 if ($row->hash != $new_hash) {
40 // Do a direct query to speed things up.
41 $query = \Drupal::database();
42 $query->update('redirect')
43 ->fields(['hash' => $new_hash])
44 ->condition('rid', $row->rid)
47 $sandbox['progress']++;
48 $sandbox['current_rid'] = $row->rid;
51 $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
56 * Update the {redirect} table.
58 function redirect_update_8101() {
59 // Get the current schema, change the length.
60 $key_value_store_schema = \Drupal::keyValue('entity.storage_schema.sql');
61 $schema = $key_value_store_schema->get('redirect.field_schema_data.hash');
62 $schema['redirect']['fields']['hash']['length'] = 64;
64 // Set the max_length of the hash column to 64 characters.
65 Database::getConnection()
67 ->changeField('redirect', 'hash', 'hash', $schema['redirect']['fields']['hash']);
69 // Update the last installed field definition and field schema.
70 /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value_store */
71 \Drupal::entityManager()->clearCachedFieldDefinitions();
72 $key_value_store_definition = \Drupal::keyValue('entity.definitions.installed');
73 $storage_definitions = $key_value_store_definition->get('redirect.field_storage_definitions');
74 $storage_definitions['hash'] = $storage_definition = \Drupal::entityManager()
75 ->getFieldStorageDefinitions('redirect')['hash'];
76 $key_value_store_definition->set('redirect.field_storage_definitions', $storage_definitions);
78 // Update the stored schema.
79 $key_value_store_schema->set('redirect.field_schema_data.hash', $schema);
83 * Update settings based on existing settings and Globalredirect settings.
85 function redirect_update_8102() {
86 // Load default configuration.
87 $redirect_settings = \Drupal::config('redirect.settings');
88 $globalredirect_settings = \Drupal::config('globalredirect.settings');
89 $config_factory = \Drupal::configFactory();
90 $redirect = $config_factory->getEditable('redirect.settings');
91 $nonclean_to_clean = $redirect_settings->get('global_clean');
92 $admin_path = $redirect_settings->get('global_admin_paths');
93 $frontpage_redirect = $redirect_settings->get('global_home');
94 $deslash = $redirect_settings->get('global_deslash');
98 // If Globalredirect configuration exists, use those settings.
99 if ((!$globalredirect_settings->isNew())) {
100 $access_check = $globalredirect_settings->get('access_check');
101 $normalize_aliases = $globalredirect_settings->get('normalize_aliases');
102 $content_location_header = $globalredirect_settings->get('content_location_header');
103 $term_path_handler = $globalredirect_settings->get('term_path_handler');
104 $deslash = $globalredirect_settings->get('deslash');
105 $frontpage_redirect = $globalredirect_settings->get('frontpage_redirect');
106 $nonclean_to_clean = $globalredirect_settings->get('nonclean_to_clean');
108 $redirect->set('access_check', $access_check);
109 $redirect->set('normalize_aliases', $normalize_aliases);
110 $redirect->set('content_location_header', $content_location_header);
111 $redirect->set('term_path_handler', $term_path_handler);
113 $message = 'The Globalredirect module functionality has been merged into redirect, it should be uninstalled now.';
116 $redirect->set('access_check', FALSE);
117 $redirect->set('normalize_aliases', TRUE);
118 $redirect->set('content_location_header', FALSE);
119 $redirect->set('term_path_handler', TRUE);
122 // Update new redirect settings names.
123 $redirect->set('nonclean_to_clean', $nonclean_to_clean);
124 $redirect->set('admin_path', $admin_path);
125 $redirect->set('frontpage_redirect', $frontpage_redirect);
126 $redirect->set('deslash', $deslash);
128 // Remove old names of redirect settings.
129 $redirect->clear('global_clean');
130 $redirect->clear('global_admin_paths');
131 $redirect->clear('global_home');
132 $redirect->clear('global_deslash');
140 * Creates the new default redirect view.
142 function redirect_update_8103() {
145 // Only create if the redirect view doesn't exist and views is enabled.
146 if (!View::load('redirect') && \Drupal::moduleHandler()->moduleExists('views')) {
147 $config_path = drupal_get_path('module', 'redirect') . '/config/install/views.view.redirect.yml';
148 $data = Yaml::parse($config_path);
149 \Drupal::configFactory()->getEditable('views.view.redirect')->setData($data)->save(TRUE);
150 $message = 'The new redirect view has been created.';
153 $message = 'Not creating a redirect view since it already exists.';
159 * Save the bulk delete action to config.
161 function redirect_update_8104() {
162 if (!Action::load('redirect_delete_action')) {
163 $entity_type_manager = \Drupal::entityTypeManager();
164 $module_handler = \Drupal::moduleHandler();
166 // Save the bulk delete action to config.
167 $config_install_path = $module_handler->getModule('redirect')->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
168 $storage = new FileStorage($config_install_path);
170 ->getStorage('action')
171 ->create($storage->read('system.action.redirect_delete_action'))
178 * Ensure to use the redirect_404 submodule.
180 function redirect_update_8105() {
181 \Drupal::service('module_installer')->install(['redirect_404']);
185 * Removes unnecessary settings from storage.
187 function redirect_update_8106() {
188 $config = \Drupal::configFactory()->getEditable('redirect.settings');
189 $config->set('route_normalizer_enabled', $config->get('normalize_aliases'));
190 $config->clear('term_path_handler');
191 $config->clear('normalize_aliases');
192 $config->clear('deslash');
193 $config->clear('frontpage_redirect');
194 $config->clear('nonclean_to_clean');
199 * Update permissions.
201 * When splitting the permission for settings from the permission to view
202 * the list of redirects, maintain the current permissions for sites which
203 * already have this module installed.
205 function redirect_update_8107() {
206 if ($roles = Role::loadMultiple()) {
207 foreach ($roles as $role) {
208 if ($role->hasPermission('administer redirects')) {
209 $role->grantPermission('administer redirect settings');