|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @file |
| 4 | + * Brightcove install file. |
| 5 | + */ |
| 6 | +use Drupal\brightcove\Entity\BrightcoveAPIClient; |
| 7 | +use Drupal\brightcove\Entity\BrightcoveSubscription; |
| 8 | +use Drupal\Core\Field\BaseFieldDefinition; |
| 9 | +use Drupal\Core\Url; |
| 10 | + |
| 11 | +/** |
| 12 | + * Implements hook_requirements(). |
| 13 | + */ |
| 14 | +function brightcove_requirements($phase) { |
| 15 | + $requirements = []; |
| 16 | + |
| 17 | + $requirements['brightcove'] = [ |
| 18 | + 'title' => 'Brightcove API', |
| 19 | + ]; |
| 20 | + |
| 21 | + // Check for API availability. |
| 22 | + if (!class_exists('Brightcove\API\API')) { |
| 23 | + $requirements['brightcove'] += [ |
| 24 | + 'description' => t('Missing Brightcove API from the vendor folder.'), |
| 25 | + 'severity' => REQUIREMENT_ERROR, |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + // If everything passes show that the Brightcove API is installed |
| 30 | + // successfully. |
| 31 | + if (!isset($requirements['severity']) || $requirements['severity'] == REQUIREMENT_OK) { |
| 32 | + $requirements['brightcove'] += [ |
| 33 | + 'description' => t('Installed'), |
| 34 | + 'severity' => REQUIREMENT_OK, |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + return $requirements; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Copy values from brightcove_callback table to a expirable key value store. |
| 43 | + */ |
| 44 | +function brightcove_update_8100(&$sandbox) { |
| 45 | + // Get database connection. |
| 46 | + $database = \Drupal::database(); |
| 47 | + |
| 48 | + if (!isset($sandbox['progress'])) { |
| 49 | + $sandbox['progress'] = 0; |
| 50 | + $sandbox['max'] = $database->select('brightcove_callback', 'bc') |
| 51 | + ->fields('bc', ['id']) |
| 52 | + ->countQuery() |
| 53 | + ->execute() |
| 54 | + ->fetchField(); |
| 55 | + } |
| 56 | + |
| 57 | + $key_value_expirable = \Drupal::keyValueExpirable('brightcove_callback'); |
| 58 | + $entries = $database->select('brightcove_callback') |
| 59 | + ->fields('brightcove_callback') |
| 60 | + ->orderBy('id', 'ASC') |
| 61 | + ->range($sandbox['progress'], 50) |
| 62 | + ->execute() |
| 63 | + ->fetchAll(); |
| 64 | + foreach ($entries as $entry) { |
| 65 | + $sandbox['progress']++; |
| 66 | + |
| 67 | + if (REQUEST_TIME < $entry->expires) { |
| 68 | + $key_value_expirable->setWithExpire($entry->token, $entry->video_id, $entry->expires - REQUEST_TIME); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Drop brightcove_callback table. |
| 77 | + */ |
| 78 | +function brightcove_update_8101() { |
| 79 | + \Drupal::database() |
| 80 | + ->schema() |
| 81 | + ->dropTable('brightcove_callback'); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Update BrightcoveAPIClient config entity's schema. |
| 86 | + */ |
| 87 | +function brightcove_update_8102() { |
| 88 | + $config_factory = \Drupal::configFactory(); |
| 89 | + foreach ($config_factory->listAll('brightcove.brightcove_api_client.') as $brightcove_api_client_config) { |
| 90 | + $config = $config_factory->getEditable($brightcove_api_client_config); |
| 91 | + $config->clear('access_token'); |
| 92 | + $config->clear('access_token_expire_date'); |
| 93 | + $config->save(TRUE); |
| 94 | + } |
| 95 | + drupal_flush_all_caches(); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Sets a default callback for the reference_id. |
| 100 | + */ |
| 101 | +function brightcove_update_8103() { |
| 102 | + /** @var \Drupal\Core\Field\BaseFieldDefinition $installed_field_definition */ |
| 103 | + $installed_field_definition = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('reference_id', 'brightcove_video'); |
| 104 | + $installed_field_definition->setDefaultValueCallback('\Drupal\brightcove\Entity\BrightcoveVideo::getDefaultReferenceId'); |
| 105 | + \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition($installed_field_definition); |
| 106 | + |
| 107 | + /** @var \Drupal\Core\Field\BaseFieldDefinition $installed_field_definition */ |
| 108 | + $installed_field_definition = \Drupal::entityDefinitionUpdateManager()->getFieldStorageDefinition('reference_id', 'brightcove_playlist'); |
| 109 | + $installed_field_definition->setDefaultValueCallback('\Drupal\brightcove\Entity\BrightcovePlaylist::getDefaultReferenceId'); |
| 110 | + \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition($installed_field_definition); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Updates the entity with the video_url base field. |
| 115 | + */ |
| 116 | +function brightcove_update_8104() { |
| 117 | + $storage_definition = BaseFieldDefinition::create('uri') |
| 118 | + ->setLabel(t('Video source URL')) |
| 119 | + ->setDisplayOptions('form', [ |
| 120 | + 'type' => 'uri', |
| 121 | + ]) |
| 122 | + ->setDisplayOptions('view', [ |
| 123 | + 'type' => 'uri_link', |
| 124 | + 'label' => 'inline', |
| 125 | + 'settings' => [ |
| 126 | + 'trim_length' => 150, |
| 127 | + 'target' => '_blank', |
| 128 | + ], |
| 129 | + ]) |
| 130 | + ->setDisplayConfigurable('form', TRUE) |
| 131 | + ->setDisplayConfigurable('view', TRUE); |
| 132 | + |
| 133 | + \Drupal::entityDefinitionUpdateManager() |
| 134 | + ->installFieldStorageDefinition('video_url', 'brightcove_video', 'brightcove_video', $storage_definition); |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +/** |
| 139 | + * Update entity definitions. |
| 140 | + */ |
| 141 | +function brightcove_update_8105() { |
| 142 | + \Drupal::entityDefinitionUpdateManager()->applyUpdates(); |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Create default subscription for each available client. |
| 147 | + */ |
| 148 | +function brightcove_update_8106() { |
| 149 | + /** @var BrightcoveAPIClient[] $clients */ |
| 150 | + $clients = BrightcoveAPIClient::loadMultiple(); |
| 151 | + $messages = []; |
| 152 | + foreach ($clients as $client) { |
| 153 | + // Create new default subscription if not exist yet. |
| 154 | + $id = "default_{$client->id()}"; |
| 155 | + $subscription = BrightcoveSubscription::load($id); |
| 156 | + |
| 157 | + if (empty($subscription)) { |
| 158 | + BrightcoveSubscription::create([ |
| 159 | + 'id' => $id, |
| 160 | + 'status' => FALSE, |
| 161 | + 'default' => TRUE, |
| 162 | + 'api_client_id' => $client->id(), |
| 163 | + 'endpoint' => Url::fromRoute('brightcove_notification_callback', [], ['absolute' => TRUE])->toString(), |
| 164 | + 'events' => ['video-change'], |
| 165 | + ])->save(FALSE); |
| 166 | + } |
| 167 | + else { |
| 168 | + $messages[] = t('Default subscription for "@client" client already exist, skipping...', ['@client' => $client->label()]) . PHP_EOL; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return implode(PHP_EOL, $messages); |
| 173 | +} |
0 commit comments