You are here

function paragraphs_library_update_8004 in Paragraphs 8

Make the label revisionable.

File

modules/paragraphs_library/paragraphs_library.install, line 164
Install/Update hooks for paragraphs library.

Code

function paragraphs_library_update_8004() {
  $database = \Drupal::database();
  $column_schema = [
    'type' => 'varchar',
    'length' => 255,
    'binary' => FALSE,
    'not null' => FALSE,
  ];
  $database
    ->schema()
    ->addField('paragraphs_library_item_revision_field_data', 'label', $column_schema);
  $result = $database
    ->query('SELECT id, langcode, label FROM {paragraphs_library_item_field_data}');
  foreach ($result as $row) {
    $database
      ->update('paragraphs_library_item_revision_field_data')
      ->fields([
      'label' => $row->label,
    ])
      ->condition('id', $row->id)
      ->condition('langcode', $row->langcode)
      ->execute();
  }

  // Update the field storage repository.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $storage_definition = $definition_update_manager
    ->getFieldStorageDefinition('label', 'paragraphs_library_item');
  $storage_definition
    ->setRevisionable(TRUE);
  \Drupal::service('entity.last_installed_schema.repository')
    ->setLastInstalledFieldStorageDefinition($storage_definition);

  // Update the stored field schema.
  // @todo: There has to be a better way to do this.
  $key = 'paragraphs_library_item.field_schema_data.label';
  $field_schema = \Drupal::keyValue('entity.storage_schema.sql')
    ->get($key);
  $field_schema['paragraphs_library_item_revision_field_data']['fields']['label'] = $column_schema;
  \Drupal::keyValue('entity.storage_schema.sql')
    ->set($key, $field_schema);
}