You are here

public function SettingsForm::validateForm in Video Embed Brightcove 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/SettingsForm.php, line 104

Class

SettingsForm
Configure Video Embed Brightcove module.

Namespace

Drupal\video_embed_brightcove\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $client_id = $form_state
    ->getValue('client_id');
  $client_secret = $form_state
    ->getValue('client_secret');

  // Check authentication if keys are provided.
  if (!empty($client_id) || !empty($client_secret)) {
    $auth_uri = 'https://oauth.brightcove.com/v4/access_token';
    $auth_string = base64_encode($client_id . ':' . $client_secret);
    $auth_options = [
      'headers' => [
        'Authorization' => 'Basic ' . $auth_string,
        'Content-Type' => 'application/x-www-form-urlencoded',
      ],
      'body' => 'grant_type=client_credentials',
    ];
    try {
      $this->httpClient
        ->request('POST', $auth_uri, $auth_options);
    } catch (\Exception $e) {

      // Set error if authentication was not successful.
      $form_state
        ->setErrorByName('client_id', 'Brightcove API authentication failed.');
      $form_state
        ->setErrorByName('client_secret', 'Please check client ID and secret key.');
    }
  }
}