You are here

public function GoogleAnalyticsCounterAuthForm::buildForm in Google Analytics Counter 8.3

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/GoogleAnalyticsCounterAuthForm.php, line 111

Class

GoogleAnalyticsCounterAuthForm
Class GoogleAnalyticsCounterAuthForm.

Namespace

Drupal\google_analytics_counter\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  global $base_url;
  $config = $this
    ->config('google_analytics_counter.settings');
  $form['#tree'] = TRUE;

  // Initialize the feed to trigger the fetching of the tokens.
  $this->authManager
    ->newGaFeed();
  $web_properties = key($this->authManager
    ->getWebPropertiesOptions());
  if ($web_properties === 'unauthenticated') {
    $form['authenticate'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Authenticate with Google Analytics'),
      '#description' => $this
        ->t("This action will redirect you to Google. Login with the account you'd like to use."),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $form['authenticate']['authenticate_submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Authenticate'),
    ];
  }
  else {
    if ($config
      ->get('general_settings.client_id') !== '') {
      $form['revoke'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Revoke authentication'),
        '#description' => $this
          ->t('This action will revoke authentication from Google Analytics.'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => 5,
      ];
      $form['revoke']['revoke_submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Revoke authentication'),
      ];
    }
  }
  $markup_description = $this->messageManager
    ->authenticationInstructions($web_properties);
  $form['setup'] = [
    '#type' => 'markup',
    '#markup' => '<h4>' . $this
      ->t('Google Analytics Setup') . '</h4>' . $markup_description,
    '#weight' => 10,
  ];
  $t_args = [
    ':href' => Url::fromUri('http://code.google.com/apis/console')
      ->toString(),
    '@href' => 'Google API Console',
  ];
  $form['client_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Client ID'),
    '#default_value' => $config
      ->get('general_settings.client_id'),
    '#size' => 90,
    '#description' => $this
      ->t('Create the Client ID in the access tab of the <a href=:href target="_blank">@href</a>.', $t_args),
    '#weight' => 11,
  ];
  $form['client_secret'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Client Secret'),
    '#default_value' => $config
      ->get('general_settings.client_secret'),
    '#size' => 90,
    '#description' => $this
      ->t('Create the Client secret in the <a href=:href target="_blank">@href</a>.', $t_args),
    '#weight' => 12,
  ];
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $uri = \Drupal::service('path.alias_manager')
    ->getAliasByPath($current_path);
  $description = $this
    ->t('The path that users are redirected to after they have authenticated with Google.<br /> Default: <strong>@default_uri</strong>', [
    '@default_uri' => $base_url . $uri,
  ]);
  $form['redirect_uri'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Authorized Redirect URI'),
    '#default_value' => $config
      ->get('general_settings.redirect_uri'),
    '#size' => 90,
    '#description' => $description,
    '#weight' => 13,
  ];
  $t_args = [
    ':href' => $this->messageManager
      ->googleProjectName(),
    '@href' => 'Analytics API',
  ];
  $form['project_name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Google Project Name'),
    '#default_value' => $config
      ->get('general_settings.project_name'),
    '#description' => $this
      ->t("Optionally add your Google Project's machine name. Machine names are written like <em>project-name</em>. This field helps to take you directly to your <a href=:href>@href</a> page to view quotas.", $t_args),
    '#weight' => 14,
  ];
  $options = $this->authManager
    ->getWebPropertiesOptions();
  $form['profile_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t("Google View"),
    '#options' => $options,
    '#default_value' => $config
      ->get('general_settings.profile_id'),
    '#description' => $this
      ->t("Choose a Google Analytics view. If you are not authenticated or if the project you are authenticating to does not have Analytics, no options are available."),
    '#weight' => 15,
  ];
  return parent::buildForm($form, $form_state);
}