You are here

function brightcove_update_8109 in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 brightcove.install \brightcove_update_8109()
  2. 3.x brightcove.install \brightcove_update_8109()

Migrate config entity subscriptions.

File

./brightcove.install, line 414
Brightcove install file.

Code

function brightcove_update_8109(&$sandbox) {
  $container = \Drupal::getContainer();

  /** @var \Drupal\Core\Database\Connection $connection */
  $connection = $container
    ->get('database');

  // Get existing brightcove subscription entities. At this point the config
  // entity does not exist, so it must be acquired through the database.
  $results = $connection
    ->select('config', 'c')
    ->fields('c')
    ->condition('name', 'brightcove.brightcove_subscription.%', 'LIKE')
    ->execute()
    ->fetchAll();

  // Migrate subscription entities.
  if (!empty($results)) {
    foreach ($results as $result) {

      // Get data and map to the new fields.
      $data = unserialize($result->data);
      $data['bcsid'] = $data['id'];
      unset($data['id']);
      $data['is_default'] = $data['default'];
      unset($data['default']);

      // Save new subscription entity.
      $brightcove_subscription = BrightcoveSubscription::createFromArray($data);
      $brightcove_subscription
        ->save();
    }

    // Remove old subscription config entities.
    $connection
      ->delete('config')
      ->condition('name', 'brightcove.brightcove\\_subscription.%', 'LIKE')
      ->execute();
  }
  else {
    $clients = BrightcoveAPIClient::loadMultiple();
    foreach ($clients as $client) {
      $default = BrightcoveSubscription::loadDefault($client);
      if (empty($default)) {
        $subscription = new BrightcoveSubscription(TRUE);
        $subscription
          ->setStatus(FALSE)
          ->setApiClient($client)
          ->setEndpoint(BrightcoveUtil::getDefaultSubscriptionUrl())
          ->setEvents([
          'video-change',
        ])
          ->save(FALSE);
      }
    }
  }

  // Clear cached definitions.

  /** @var \Drupal\Core\Config\TypedConfigManager $typed_config_manager */
  $typed_config_manager = $container
    ->get('config.typed');
  $typed_config_manager
    ->clearCachedDefinitions();
}