diff --git a/src/Entity/BrightcoveAPIClient.php b/src/Entity/BrightcoveAPIClient.php index 7f75d5d..c76c4aa 100644 --- a/src/Entity/BrightcoveAPIClient.php +++ b/src/Entity/BrightcoveAPIClient.php @@ -8,6 +8,7 @@ use Brightcove\API\Exception\AuthenticationException; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\brightcove\BrightcoveAPIClientInterface; use Brightcove\API\Client; +use Drupal\key\Entity\Key; /** * Defines the Brightcove API Client entity. @@ -158,6 +159,14 @@ class BrightcoveAPIClient extends ConfigEntityBase implements BrightcoveAPIClien * @inheritdoc */ public function getSecretKey() { + if (\Drupal::moduleHandler()->moduleExists('key')) { + // Ensure key is loaded, to catch the case where key module is loaded + // after the key is saved. + $key_entity = Key::load($this->secret_key); + if ($key_entity) { + return $key_entity->getKeyValue(); + } + } return $this->secret_key; } @@ -292,7 +301,7 @@ class BrightcoveAPIClient extends ConfigEntityBase implements BrightcoveAPIClien } // Otherwise get a new access token. else { - $client = Client::authorize($this->client_id, $this->secret_key); + $client = Client::authorize($this->client_id, $this->getSecretKey()); // Update access information. This will ensure that in the current // session we will get the correct access data. diff --git a/src/Form/BrightcoveAPIClientForm.php b/src/Form/BrightcoveAPIClientForm.php index e5fee08..d4ac676 100644 --- a/src/Form/BrightcoveAPIClientForm.php +++ b/src/Form/BrightcoveAPIClientForm.php @@ -17,6 +17,7 @@ use Drupal\Core\Queue\QueueInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\brightcove\Entity\BrightcoveAPIClient; +use Drupal\key\Entity\Key; use Brightcove\API\Exception\AuthenticationException; use Brightcove\API\Client; use Brightcove\API\CMS; @@ -180,14 +181,34 @@ class BrightcoveAPIClientForm extends EntityForm { '#required' => TRUE, ]; - $form['secret_key'] = [ - '#type' => 'textfield', - '#title' => $this->t('Brightcove API Secret Key'), - '#description' => $this->t('The Secret Key associated with the Client ID above, only visible once when Registering New Application.'), - '#maxlength' => 255, - '#default_value' => $brightcove_api_client->getSecretKey(), - '#required' => TRUE, - ]; + if (\Drupal::moduleHandler()->moduleExists('key')) { + // Get the Pfizer keys. + $keys = Key::loadMultiple(); + $key_options = [ + '' => $this->t('-- Select one --'), + ]; + foreach ($keys as $entity) { + $key_options[$entity->id()] = $entity->label(); + } + $form['secret_key'] = [ + '#type' => 'select', + '#options' => $key_options, + '#title' => $this->t('Brightcove API Secret Key'), + '#description' => $this->t('The Secret Key associated with the Client ID above, only visible once when Registering New Application.'), + '#default_value' => $brightcove_api_client->getDrupalSecretKey(), + '#required' => TRUE, + ]; + } + else { + $form['secret_key'] = [ + '#type' => 'textfield', + '#title' => $this->t('Brightcove API Secret Key'), + '#description' => $this->t('The Secret Key associated with the Client ID above, only visible once when Registering New Application.'), + '#maxlength' => 255, + '#default_value' => $brightcove_api_client->getSecretKey(), + '#required' => TRUE, + ]; + } $form['account_id'] = [ '#type' => 'textfield', @@ -227,9 +248,16 @@ class BrightcoveAPIClientForm extends EntityForm { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { + if (\Drupal::moduleHandler()->moduleExists('key')) { + $key_entity = Key::load($form_state->getValue('secret_key')); + $secret_key = $key_entity->getKeyValue(); + } + else { + $secret_key = $form_state->getValue('secret_key'); + } try { // Try to authorize client and save values on success. - $client = Client::authorize($form_state->getValue('client_id'), $form_state->getValue('secret_key')); + $client = Client::authorize($form_state->getValue('client_id'), $secret_key); // Test account ID. $cms = new CMS($client, $form_state->getValue('account_id'));