-
-
Save vever001/4f71e8b8c38797f73d68db2f5410bc92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Post Update hooks for the mymodule module. | |
*/ | |
use Drupal\Core\Database\Database; | |
use Drupal\Core\Site\Settings; | |
use Drupal\redirect\Entity\Redirect; | |
/** | |
* Updates redirect hashes after patch #2879648. | |
* | |
* @see https://www.drupal.org/project/redirect/issues/2879648#comment-13345751 | |
*/ | |
function mymodule_post_update_update_redirect_hashes(&$sandbox) { | |
if (!isset($sandbox['progress'])) { | |
$redirect_count = Database::getConnection() | |
->query('SELECT COUNT(rid) FROM {redirect}') | |
->fetchField(); | |
if (empty($redirect_count)) { | |
return t('There were no redirects to update.'); | |
} | |
$sandbox['progress'] = 0; | |
$sandbox['updated'] = 0; | |
$sandbox['deleted'] = 0; | |
$sandbox['current_rid'] = 0; | |
$sandbox['limit'] = Settings::get('entity_update_batch_size', 50); | |
$sandbox['max'] = $redirect_count; | |
} | |
// Get redirects in groups keeps this process within resource limits. | |
$redirect_ids = Database::getConnection() | |
->select('redirect', 'r') | |
->fields('r', ['rid']) | |
->condition('rid', $sandbox['current_rid'], '>') | |
->range(0, $sandbox['limit']) | |
->orderBy('rid', 'ASC') | |
->execute() | |
->fetchCol(); | |
// Get the redirect storage controller so we can call preSave() below. | |
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ | |
$entity_storage = Drupal::service('entity_type.manager')->getStorage('redirect'); | |
// Use preSave() to determine whether a redirect's hash has changed and only | |
// update it if it has. | |
foreach (Redirect::loadMultiple($redirect_ids) as $redirect) { | |
$old_hash = $redirect->getHash(); | |
$redirect->preSave($entity_storage); | |
$new_hash = $redirect->getHash(); | |
if ($old_hash !== $new_hash) { | |
// Check if the hash already exists. | |
$existing = Database::getConnection() | |
->select('redirect', 'r') | |
->fields('r', ['rid']) | |
->condition('hash', $new_hash) | |
->execute() | |
->fetchField(); | |
if ($existing !== FALSE) { | |
// If so, delete this redirect. | |
$redirect->delete(); | |
$sandbox['deleted']++; | |
} | |
else { | |
// Otherwise, update it. | |
$redirect->save(); | |
$sandbox['updated']++; | |
} | |
} | |
$sandbox['progress']++; | |
$sandbox['current_rid'] = $redirect->id(); | |
} | |
// Update the batch progress. | |
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max']; | |
if ($sandbox['#finished'] >= 1) { | |
return t('Updated redirects: %updated, deleted redirects: %deleted.', [ | |
'%updated' => $sandbox['updated'], | |
'%deleted' => $sandbox['deleted'], | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment