You are here

function site_settings_update_8004 in Site Settings and Labels 8

Create the entity data_table and migrate existing entities.

1 call to site_settings_update_8004()
site_settings_update_8003 in ./site_settings.install
Delete orphaned site settings.

File

./site_settings.install, line 104
Contains install and update hooks.

Code

function site_settings_update_8004() {
  $data_table_exists = \Drupal::database()
    ->schema()
    ->tableExists('site_setting_entity_field_data');
  if (!$data_table_exists) {

    // Create the missing data table.
    $entity_type_id = 'site_setting_entity';
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type_manager
      ->clearCachedDefinitions();
    $entity_type = $entity_type_manager
      ->getDefinition($entity_type_id);
    $entity_update_manager = \Drupal::entityDefinitionUpdateManager();
    $entity_update_manager
      ->installEntityType($entity_type);

    // Copy the relevant values from base table to data table.
    $query = \Drupal::database()
      ->select('site_setting_entity', 'sse');
    $query
      ->fields('sse');
    $sse_items = $query
      ->execute()
      ->fetchAll();
    foreach ($sse_items as $item) {
      $insert_values = [
        'id' => $item->id,
        'type' => $item->type,
        'langcode' => $item->langcode,
        'user_id' => $item->user_id,
        'name' => $item->name,
        'fieldset' => $item->fieldset,
        'status' => $item->status,
        'created' => $item->created,
        'changed' => $item->changed,
        'default_langcode' => '1',
      ];
      \Drupal::database()
        ->insert('site_setting_entity_field_data')
        ->fields($insert_values)
        ->execute();
    }

    // Remove base table fields that are no longer needed.
    $schema = \Drupal::database()
      ->schema();
    $drop_fields = [
      'name',
      'fieldset',
      'status',
      'created',
      'changed',
    ];
    foreach ($drop_fields as $drop_field) {
      $schema
        ->dropField('site_setting_entity', $drop_field);
    }
  }
}