You are here

function system_update_8001 in Drupal 8

Change two fields on the default menu link storage to be serialized data.

File

core/modules/system/system.install, line 1542
Install, update and uninstall functions for the system module.

Code

function system_update_8001(&$sandbox = NULL) {
  $database = \Drupal::database();
  $schema = $database
    ->schema();
  if ($schema
    ->tableExists('menu_tree')) {
    if (!isset($sandbox['current'])) {

      // Converting directly to blob can cause problems with reading out and
      // serializing the string data later on postgres, so rename the existing
      // columns and create replacement ones to hold the serialized objects.
      $old_fields = [
        'title' => [
          'description' => 'The text displayed for the link.',
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'default' => '',
        ],
        'description' => [
          'description' => 'The description of this link - used for admin pages and title attribute.',
          'type' => 'text',
          'not null' => FALSE,
        ],
      ];
      foreach ($old_fields as $name => $spec) {
        $schema
          ->changeField('menu_tree', $name, 'system_update_8001_' . $name, $spec);
      }
      $spec = [
        'description' => 'The title for the link. May be a serialized TranslatableMarkup.',
        'type' => 'blob',
        'size' => 'big',
        'not null' => FALSE,
        'serialize' => TRUE,
      ];
      $schema
        ->addField('menu_tree', 'title', $spec);
      $spec = [
        'description' => 'The description of this link - used for admin pages and title attribute.',
        'type' => 'blob',
        'size' => 'big',
        'not null' => FALSE,
        'serialize' => TRUE,
      ];
      $schema
        ->addField('menu_tree', 'description', $spec);
      $sandbox['current'] = 0;
      $sandbox['max'] = $database
        ->query('SELECT COUNT(mlid) FROM {menu_tree}')
        ->fetchField();
    }
    $menu_links = $database
      ->queryRange('SELECT mlid, system_update_8001_title AS title, system_update_8001_description AS description FROM {menu_tree} ORDER BY mlid ASC', $sandbox['current'], $sandbox['current'] + 50)
      ->fetchAllAssoc('mlid');
    foreach ($menu_links as $menu_link) {
      $menu_link = (array) $menu_link;

      // Convert title and description to serialized strings.
      $menu_link['title'] = serialize($menu_link['title']);
      $menu_link['description'] = serialize($menu_link['description']);
      $database
        ->update('menu_tree')
        ->fields($menu_link)
        ->condition('mlid', $menu_link['mlid'])
        ->execute();
      $sandbox['current']++;
    }
    $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['current'] / $sandbox['max'];
    if ($sandbox['#finished'] >= 1) {

      // Drop unnecessary fields from {menu_tree}.
      $schema
        ->dropField('menu_tree', 'system_update_8001_title');
      $schema
        ->dropField('menu_tree', 'title_arguments');
      $schema
        ->dropField('menu_tree', 'title_context');
      $schema
        ->dropField('menu_tree', 'system_update_8001_description');
    }
    return t('Menu links converted');
  }
  else {
    return t('Menu link conversion skipped, because the {menu_tree} table did not exist yet.');
  }
}