You are here

function social_core_update_8020 in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.install \social_core_update_8020()
  2. 8.2 modules/social_features/social_core/social_core.install \social_core_update_8020()
  3. 8.3 modules/social_features/social_core/social_core.install \social_core_update_8020()
  4. 8.4 modules/social_features/social_core/social_core.install \social_core_update_8020()
  5. 8.5 modules/social_features/social_core/social_core.install \social_core_update_8020()
  6. 8.6 modules/social_features/social_core/social_core.install \social_core_update_8020()
  7. 8.7 modules/social_features/social_core/social_core.install \social_core_update_8020()
  8. 8.8 modules/social_features/social_core/social_core.install \social_core_update_8020()
  9. 10.3.x modules/social_features/social_core/social_core.install \social_core_update_8020()
  10. 10.0.x modules/social_features/social_core/social_core.install \social_core_update_8020()
  11. 10.1.x modules/social_features/social_core/social_core.install \social_core_update_8020()
  12. 10.2.x modules/social_features/social_core/social_core.install \social_core_update_8020()

Fix an schema issue caused by Flag module.

File

modules/social_features/social_core/social_core.install, line 617
Install, update and uninstall functions for the social_comment module.

Code

function social_core_update_8020() {
  $bundle_schema = [
    'description' => 'The Flag ID.',
    'type' => 'varchar_ascii',
    'length' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    'not null' => TRUE,
  ];

  /** @var \Drupal\Core\Database\Schema $schema */
  $schema = \Drupal::database()
    ->schema();
  $schema
    ->changeField('flagging', 'flag_id', 'flag_id', $bundle_schema);
  $schema
    ->dropIndex('flagging', 'flag_id');
  $schema
    ->dropIndex('flagging', 'flagging_field__flag_id__target_id');
  $schema
    ->addIndex('flagging', 'flagging_field__flag_id__target_id', [
    'flag_id',
  ], [
    'fields' => [
      'flag_id' => $bundle_schema,
    ],
  ]);

  // Update the field storage repository.

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $efm */
  $efm = \Drupal::service('entity_field.manager');

  /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $kv */
  $kv = \Drupal::service('keyvalue');

  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $repo */
  $repo = \Drupal::service('entity.last_installed_schema.repository');
  $efm
    ->clearCachedFieldDefinitions();
  $storage_definition = $efm
    ->getFieldStorageDefinitions('flagging')['flag_id'];
  $repo
    ->setLastInstalledFieldStorageDefinition($storage_definition);

  // Update the stored field schema.
  // @todo: There has to be a better way to do this.
  $kv_collection = 'entity.storage_schema.sql';
  $kv_name = 'flagging.field_schema_data.flag_id';
  $field_schema = $kv
    ->get($kv_collection)
    ->get($kv_name);
  $field_schema['flagging']['fields']['flag_id'] = $bundle_schema;
  $field_schema['flagging']['indexes']['flagging_field__flag_id__target_id'] = [
    'flag_id',
  ];
  $kv
    ->get($kv_collection)
    ->set($kv_name, $field_schema);
}