You are here

function paragraphs_update_8019 in Paragraphs 8

Update the field storage definitions of the parent fields.

File

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

Code

function paragraphs_update_8019() {
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $last_installed_schema = \Drupal::service('entity.last_installed_schema.repository');
  foreach ([
    'parent_id',
    'parent_type',
    'parent_field_name',
  ] as $field_name) {
    $column_schema = [
      'type' => 'varchar_ascii',
      'length' => $field_name == 'parent_id' ? 255 : 32,
      'binary' => FALSE,
      'not null' => FALSE,
    ];

    // Update the field storage repository.
    $storage_definition = $definition_update_manager
      ->getFieldStorageDefinition($field_name, 'paragraph');
    $storage_definition
      ->setRevisionable(TRUE);
    $last_installed_schema
      ->setLastInstalledFieldStorageDefinition($storage_definition);

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