You are here

function social_core_update_8021 in Open Social 10.3.x

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

Update social_post existing content.

File

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

Code

function social_core_update_8021(&$sandbox) {
  $database = \Drupal::database();
  $table_post = 'post';
  $table_data = 'post_field_data';

  // Get the old data.
  $existing_data_post = $database
    ->select($table_post)
    ->fields($table_post)
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);

  // Wipe it.
  $database
    ->truncate($table_post)
    ->execute();
  $existing_data_data = $database
    ->select($table_data)
    ->fields($table_data)
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);

  // Wipe it.
  $database
    ->truncate($table_data)
    ->execute();

  // Add new field to tables.
  $spec = [
    'type' => 'varchar',
    'length' => 32,
    'not null' => TRUE,
    'default' => 'post',
    'description' => 'The ID of the target entity.',
  ];
  $schema = Database::getConnection()
    ->schema();
  if ($schema
    ->fieldExists($table_post, 'type')) {
    $schema
      ->changeField($table_post, 'type', 'type', $spec);
  }
  else {
    $schema
      ->addField($table_post, 'type', $spec);
  }
  if ($schema
    ->fieldExists($table_data, 'type')) {
    $schema
      ->changeField($table_data, 'type', 'type', $spec);
  }
  else {
    $schema
      ->addField($table_data, 'type', $spec);
  }

  // Update definitions and schema.
  $list = \Drupal::entityDefinitionUpdateManager()
    ->getChangeList();
  if (!empty($list['post'])) {
    foreach ($list['post'] as $post) {
      \Drupal::entityDefinitionUpdateManager()
        ->updateEntityType($post);
    }
  }

  // Update config post_type.
  $path = drupal_get_path('module', 'social_post') . '/config/install';
  $config_factory = \Drupal::configFactory();
  $config_name = "social_post.post_type.post";
  $filepath = "{$path}/{$config_name}.yml";
  $data = Yaml::parse($filepath);
  if (is_array($data)) {
    $config_factory
      ->getEditable($config_name)
      ->setData($data)
      ->save();
  }
  if (!empty($existing_data_post)) {

    // Set the old data.
    $insert_query_post = $database
      ->insert($table_post)
      ->fields(array_keys(end($existing_data_post)));
    foreach ($existing_data_post as $row) {
      $insert_query_post
        ->values(array_values($row));
    }
    $insert_query_post
      ->execute();
  }
  if (!empty($existing_data_data)) {
    $insert_query_data = $database
      ->insert($table_data)
      ->fields(array_keys(end($existing_data_data)));
    foreach ($existing_data_data as $row) {
      $insert_query_data
        ->values(array_values($row));
    }
    $insert_query_data
      ->execute();
  }

  // Unset default value for post entity.
  $schema
    ->changeField($table_data, 'type', 'type', $spec);
}