You are here

function paragraphs_update_8006 in Paragraphs 8

Remove revision_timestamp, changed fields, add content_translation_changed.

File

./paragraphs.install, line 104
Installation hooks for Paragraphs module.

Code

function paragraphs_update_8006() {
  $tables_fields = [
    'paragraphs_item_revision' => 'revision_timestamp',
    'paragraphs_item_field_data' => 'changed',
    'paragraphs_item_revision_field_data' => 'changed',
  ];
  $database = Drupal::database();
  $entity_definition_update_manager = Drupal::entityDefinitionUpdateManager();

  // Ensure that the data from the content translation status field is deleted
  // so that the field can safely be deleted.
  foreach ($tables_fields as $table => $field) {
    if ($database
      ->schema()
      ->fieldExists($table, $field)) {
      $database
        ->update($table)
        ->fields([
        $field => NULL,
      ])
        ->execute();
    }
  }
  foreach ($tables_fields as $table => $field) {

    // Delete the storage definition if it was defined before.
    $storage_definition = $entity_definition_update_manager
      ->getFieldStorageDefinition($field, 'paragraph');
    if ($storage_definition) {
      $entity_definition_update_manager
        ->uninstallFieldStorageDefinition($storage_definition);
    }
  }

  // Add content_translation_changed field.
  $field_storage_definitions = \Drupal::service('entity_field.manager')
    ->getFieldStorageDefinitions('paragraph');
  if (isset($field_storage_definitions['content_translation_changed'])) {
    $storage_definition = BaseFieldDefinition::create('changed')
      ->setLabel(t('Translation changed time'))
      ->setDescription(t('The Unix timestamp when the translation was most recently saved.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
    \Drupal::entityDefinitionUpdateManager()
      ->installFieldStorageDefinition('content_translation_changed', 'paragraph', 'paragraph', $storage_definition);
  }
}