You are here

public function Settings::buildForm in Google API PHP Client 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/Settings.php, line 66

Class

Settings
Google API Settings.

Namespace

Drupal\google_api_client\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('google_api_client.settings');
  $tokenConf = $this
    ->config('google_api_client.tokens');
  $options = [
    'attributes' => [
      'target' => '_blank',
    ],
  ];
  $form['client'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Client Settings'),
  ];
  $form['client']['help'] = [
    '#type' => '#markup',
    '#markup' => $this
      ->t('To get your Client Credentials, you need to register your application. See details on @link.', [
      '@link' => Link::fromTextAndUrl('https://developers.google.com/api-client-library/php/auth/web-app', Url::fromUri('https://developers.google.com/api-client-library/php/auth/web-app'))
        ->toString(),
    ]),
  ];
  $form['client']['credentials'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Client Credentials'),
    '#default_value' => $config
      ->get('credentials'),
    '#required' => TRUE,
    '#placeholder' => $this
      ->t('Example: {"web":{"client_id":"example.apps.googleusercontent.com","project_id":"example","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"example","redirect_uris":["http://example.com/google_api_client/callback"],"javascript_origins":["http://example.com"]}}'),
  ];
  $linkScopes = Link::fromTextAndUrl('click here to learn more', Url::fromUri('https://developers.google.com/identity/protocols/googlescopes', $options))
    ->toString();
  $form['client']['scopes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Client Scopes'),
    '#default_value' => $config
      ->get('scopes'),
    '#required' => TRUE,
    '#placeholder' => $this
      ->t('Example: https://www.googleapis.com/auth/cse'),
    '#description' => $this
      ->t('Add one scope per line. OAuth 2.0 Scopes for Google APIs, @link. After changing scopes, you have to request new tokens.', [
      '@link' => $linkScopes,
    ]),
  ];
  if ($config
    ->get('credentials') != '') {

    // Failure to get URL might be because of invalid client credentials.
    try {
      $url = $this
        ->accessUrl();
    } catch (\Exception $e) {
      $msg = $this
        ->t('Failure to use provided client credentials. Error message: <q>@error</q>', [
        '@error' => $e
          ->getMessage(),
      ]);
      $this
        ->messenger()
        ->addError($msg);
    }
  }
  if (isset($url)) {
    $link = Link::fromTextAndUrl('click here', Url::fromUri($url, $options))
      ->toString();

    // Just check if any of the tokens are set, if not set a message.
    if ($tokenConf
      ->get('google_access_token') == NULL && $tokenConf
      ->get('google_refresh_token') == NULL) {
      $msg = $this
        ->t('Access and Refresh Tokens are not set, to get your Tokens, @link.', [
        '@link' => $link,
      ]);
      $this
        ->messenger()
        ->addError($msg);
    }

    // TODO Figure out a nicer way to display the link. Maybe a button?
    $form['client']['tokens'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Access and Refresh Tokens'),
      '#description' => $this
        ->t('To get your Tokens, @link.', [
        '@link' => $link,
      ]),
      '#open' => TRUE,
      '#access' => TRUE,
    ];
  }
  return parent::buildForm($form, $form_state);
}