You are here

function token_custom_update_8001 in Custom Tokens 8

Update entity definition and database tables.

File

./token_custom.install, line 13
Install, update and uninstall functions for the Custom Tokens module.

Code

function token_custom_update_8001() {
  $connection = Database::getConnection();
  $entityTypeManager = Drupal::entityTypeManager();
  $entityTypeDefinition = $entityTypeManager
    ->getDefinition('token_custom');
  $base_table = $entityTypeDefinition
    ->getBaseTable();
  $field_data_table = $base_table . '_field_data';
  $base_temp_table = $base_table . '_temp';
  $field_data_temp_table = $field_data_table . '_temp';

  // If there is no data then there is nothing to do here.
  // Just run `drush entity-updates`.
  if (!$connection
    ->query("SELECT machine_name FROM {{$base_table}}")
    ->fetchAll()) {
    return;
  }

  /* @var string */
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();

  /* @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface */
  $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  /* @var \Drupal\Core\Database\Schema */
  $schema = $connection
    ->schema();

  /* @var boolean */
  $field_data_exists = $schema
    ->tableExists($field_data_table);

  // Copy tables and then truncate them so the entity definition can be updated.
  $connection
    ->query("CREATE TABLE {{$base_temp_table}} SELECT * FROM {{$base_table}};");
  if (!$field_data_exists) {
    $records = $connection
      ->query("SELECT * FROM {{$base_temp_table}}");
  }
  else {
    $connection
      ->query("CREATE TABLE {{$field_data_temp_table}}  SELECT * FROM {{$field_data_table}};");
    $records = $connection
      ->query("SELECT * FROM {{$base_temp_table}} b INNER JOIN {{$field_data_temp_table}} fd on b.machine_name = fd.machine_name;");
    $connection
      ->truncate($field_data_table)
      ->execute();
  }
  $connection
    ->truncate($base_table)
    ->execute();

  // Apply changes to the entity definition.
  $entity_definition_update_manager
    ->applyUpdates();

  // Populate entity tables from data in temporary tables.
  foreach ($records
    ->fetchAll() as $record) {
    $connection
      ->insert($base_table)
      ->fields([
      'machine_name' => $record->machine_name,
      'type' => $record->type,
      'langcode' => $langcode,
    ])
      ->execute();
    $connection
      ->insert($field_data_table)
      ->fields([
      'machine_name' => $record->machine_name,
      'type' => $record->type,
      'langcode' => $langcode,
      'name' => $record->name,
      'description' => $record->description,
      'content__value' => $record->content,
      'content__format' => $record->format,
      'is_new' => $record->is_new,
      'default_langcode' => TRUE,
    ])
      ->execute();
  }

  // Tidy up.
  $connection
    ->delete($base_temp_table)
    ->execute();
  if ($field_data_exists) {
    $connection
      ->delete($field_data_temp_table)
      ->execute();
  }
}