You are here

function multiversion_update_8101 in Multiversion 8

Make langcode primary key in base data table and base revision table.

File

./multiversion.install, line 412

Code

function multiversion_update_8101() {
  $schema = Database::getConnection()
    ->schema();
  $entity_types = \Drupal::service('multiversion.manager')
    ->getSupportedEntityTypes();

  /** @var ContentEntityTypeInterface $entity_type */
  foreach ($entity_types as $entity_type) {
    if ($entity_type
      ->id() === 'file' || $entity_type
      ->get('local') == TRUE || empty($entity_type
      ->getKey('langcode'))) {
      continue;
    }

    // Get the tables name used for base table and revision table.
    $table_base = $entity_type
      ->isTranslatable() ? $entity_type
      ->getDataTable() : $entity_type
      ->getBaseTable();
    $table_revision = $entity_type
      ->isTranslatable() ? $entity_type
      ->getRevisionDataTable() : $entity_type
      ->getRevisionTable();
    if ($table_base) {
      $schema
        ->dropPrimaryKey($table_base);
      $schema
        ->addPrimaryKey($table_base, [
        $entity_type
          ->getKey('id'),
        'langcode',
      ]);
    }
    if ($table_revision) {
      $schema
        ->dropPrimaryKey($table_revision);
      $schema
        ->addPrimaryKey($table_revision, [
        $entity_type
          ->getKey('revision'),
        'langcode',
      ]);
    }
  }
}