function system_update_8804
Convert path aliases to entities.
File
-
core/
modules/ system/ system.install, line 2676
Code
function system_update_8804(&$sandbox = NULL) {
// Bail out early if the entity type is not using the default storage class.
$storage = \Drupal::entityTypeManager()->getStorage('path_alias');
if (!$storage instanceof PathAliasStorage) {
return;
}
if (!isset($sandbox['current_id'])) {
// This must be the first run. Initialize the sandbox.
$sandbox['progress'] = 0;
$sandbox['current_id'] = 0;
}
$database = \Drupal::database();
$step_size = 200;
$url_aliases = $database->select('url_alias', 't')
->condition('t.pid', $sandbox['current_id'], '>')
->fields('t')
->orderBy('pid', 'ASC')
->range(0, $step_size)
->execute()
->fetchAll();
if ($url_aliases) {
/** @var \Drupal\Component\Uuid\UuidInterface $uuid */
$uuid = \Drupal::service('uuid');
$base_table_insert = $database->insert('path_alias');
$base_table_insert->fields([
'id',
'revision_id',
'uuid',
'path',
'alias',
'langcode',
'status',
]);
$revision_table_insert = $database->insert('path_alias_revision');
$revision_table_insert->fields([
'id',
'revision_id',
'path',
'alias',
'langcode',
'status',
'revision_default',
]);
foreach ($url_aliases as $url_alias) {
$values = [
'id' => $url_alias->pid,
'revision_id' => $url_alias->pid,
'uuid' => $uuid->generate(),
'path' => $url_alias->source,
'alias' => $url_alias->alias,
'langcode' => $url_alias->langcode,
'status' => 1,
];
$base_table_insert->values($values);
unset($values['uuid']);
$values['revision_default'] = 1;
$revision_table_insert->values($values);
}
$base_table_insert->execute();
$revision_table_insert->execute();
$sandbox['progress'] += count($url_aliases);
$last_url_alias = end($url_aliases);
$sandbox['current_id'] = $last_url_alias->pid;
// If we're not in maintenance mode, the number of path aliases could change
// at any time so make sure that we always use the latest record count.
$missing = $database->select('url_alias', 't')
->condition('t.pid', $sandbox['current_id'], '>')
->orderBy('pid', 'ASC')
->countQuery()
->execute()
->fetchField();
$sandbox['#finished'] = $missing ? $sandbox['progress'] / ($sandbox['progress'] + (int) $missing) : 1;
}
else {
$sandbox['#finished'] = 1;
}
if ($sandbox['#finished'] >= 1) {
// Keep a backup of the old 'url_alias' table if requested.
if (Settings::get('entity_update_backup', TRUE)) {
$old_table_name = 'old_' . substr(uniqid(), 0, 6) . '_url_alias';
if (!$database->schema()
->tableExists($old_table_name)) {
$database->schema()
->renameTable('url_alias', $old_table_name);
}
}
else {
$database->schema()
->dropTable('url_alias');
}
return t('Path aliases have been converted to entities.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.