You are here

protected function MultiversionStorageSchemaConverter::updateFieldStorageDefinitionsToRevisionable in Multiversion 8.2

1 call to MultiversionStorageSchemaConverter::updateFieldStorageDefinitionsToRevisionable()
MultiversionStorageSchemaConverter::convertToMultiversionable in src/Entity/Storage/Sql/MultiversionStorageSchemaConverter.php

File

src/Entity/Storage/Sql/MultiversionStorageSchemaConverter.php, line 341

Class

MultiversionStorageSchemaConverter

Namespace

Drupal\multiversion\Entity\Storage\Sql

Code

protected function updateFieldStorageDefinitionsToRevisionable(ContentEntityTypeInterface $entity_type, array $storage_definitions, array $fields_to_update = [], $update_cached_definitions = TRUE) {
  $updated_storage_definitions = array_map(function ($storage_definition) {
    return clone $storage_definition;
  }, $storage_definitions);

  // Update the 'langcode' field manually, as it is configured in the base
  // content entity field definitions.
  if ($entity_type
    ->hasKey('langcode')) {
    $fields_to_update = array_merge([
      $entity_type
        ->getKey('langcode'),
    ], $fields_to_update);
  }
  foreach ($fields_to_update as $field_name) {
    if (!empty($updated_storage_definitions[$field_name]) && !$updated_storage_definitions[$field_name]
      ->isRevisionable()) {
      $updated_storage_definitions[$field_name]
        ->setRevisionable(TRUE);
      if ($update_cached_definitions) {
        $this->entityDefinitionUpdateManager
          ->updateFieldStorageDefinition($updated_storage_definitions[$field_name]);
      }
    }
  }

  // Add the revision ID field.
  $revision_field = BaseFieldDefinition::create('integer')
    ->setName($entity_type
    ->getKey('revision'))
    ->setTargetEntityTypeId($entity_type
    ->id())
    ->setTargetBundle(NULL)
    ->setLabel(new TranslatableMarkup('Revision ID'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  if ($update_cached_definitions) {
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition($revision_field
      ->getName(), $entity_type
      ->id(), $entity_type
      ->getProvider(), $revision_field);
  }
  $updated_storage_definitions[$entity_type
    ->getKey('revision')] = $revision_field;

  // Add the default revision flag field.
  $field_name = $entity_type
    ->getRevisionMetadataKey('revision_default');
  $storage_definition = BaseFieldDefinition::create('boolean')
    ->setName($field_name)
    ->setTargetEntityTypeId($entity_type
    ->id())
    ->setTargetBundle(NULL)
    ->setLabel(t('Default revision'))
    ->setDescription(t('A flag indicating whether this was a default revision when it was saved.'))
    ->setStorageRequired(TRUE)
    ->setTranslatable(FALSE)
    ->setRevisionable(TRUE);
  if ($update_cached_definitions) {
    $this->entityDefinitionUpdateManager
      ->installFieldStorageDefinition($field_name, $entity_type
      ->id(), $entity_type
      ->getProvider(), $storage_definition);
  }
  $updated_storage_definitions[$field_name] = $storage_definition;

  // Add the 'revision_translation_affected' field if needed.
  if ($entity_type
    ->isTranslatable()) {
    $revision_translation_affected_field = BaseFieldDefinition::create('boolean')
      ->setName($entity_type
      ->getKey('revision_translation_affected'))
      ->setTargetEntityTypeId($entity_type
      ->id())
      ->setTargetBundle(NULL)
      ->setLabel(new TranslatableMarkup('Revision translation affected'))
      ->setDescription(new TranslatableMarkup('Indicates if the last edit of a translation belongs to current revision.'))
      ->setReadOnly(TRUE)
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
    if ($update_cached_definitions) {
      $this->entityDefinitionUpdateManager
        ->installFieldStorageDefinition($revision_translation_affected_field
        ->getName(), $entity_type
        ->id(), $entity_type
        ->getProvider(), $revision_translation_affected_field);
    }
    $updated_storage_definitions[$entity_type
      ->getKey('revision_translation_affected')] = $revision_translation_affected_field;
  }
  return $updated_storage_definitions;
}