You are here

public function SettingsForm::buildForm in Acquia Connector 8

Same name and namespace in other branches
  1. 8.2 src/Form/SettingsForm.php \Drupal\acquia_connector\Form\SettingsForm::buildForm()
  2. 3.x src/Form/SettingsForm.php \Drupal\acquia_connector\Form\SettingsForm::buildForm()

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/SettingsForm.php, line 125

Class

SettingsForm
Acquia Connector Settings.

Namespace

Drupal\acquia_connector\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('acquia_connector.settings');
  $storage = new Storage();
  $identifier = $storage
    ->getIdentifier();
  $key = $storage
    ->getKey();
  $subscription = $config
    ->get('subscription_name');
  if (empty($identifier) && empty($key)) {
    return new RedirectResponse((string) \Drupal::service('url_generator')
      ->generateFromRoute('acquia_connector.start'));
  }

  // Check our connection to the Acquia and validate credentials.
  try {
    $this->client
      ->getSubscription($identifier, $key);
  } catch (ConnectorException $e) {
    $error_message = acquia_connector_connection_error_message($e
      ->getCustomMessage('code', FALSE));
    $ssl_available = in_array('ssl', stream_get_transports(), TRUE) && !defined('ACQUIA_CONNECTOR_TEST_ACQUIA_DEVELOPMENT_NOSSL') && $config
      ->get('spi.ssl_verify');
    if (empty($error_message) && $ssl_available) {
      $error_message = $this
        ->t('There was an error in validating your subscription credentials. You may want to try disabling SSL peer verification by setting the variable acquia_connector.settings:spi.ssl_verify to false.');
    }
    $this
      ->messenger()
      ->addError($error_message);
  }
  $form['connected'] = [
    '#markup' => $this
      ->t('<h3>Connected to Acquia</h3>'),
  ];
  if (!empty($subscription)) {
    $form['subscription'] = [
      '#markup' => $this
        ->t('Subscription: @sub <a href=":url">change</a>', [
        '@sub' => $subscription,
        ':url' => (string) \Drupal::service('url_generator')
          ->generateFromRoute('acquia_connector.setup'),
      ]),
    ];
  }
  $form['identification'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Site Identification'),
    '#collapsible' => FALSE,
  ];
  $form['identification']['description']['#markup'] = $this
    ->t('Provide a name for this site to uniquely identify it on Acquia Cloud.');
  $form['identification']['description']['#weight'] = -2;
  $form['identification']['site'] = [
    '#prefix' => '<div class="acquia-identification">',
    '#suffix' => '</div>',
    '#weight' => -1,
  ];
  $form['identification']['site']['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $this->state
      ->get('spi.site_name') ?? $this->spiController
      ->getAcquiaHostedName(),
  ];
  if (!empty($form['identification']['site']['name']['#default_value']) && $this->spiController
    ->checkAcquiaHosted()) {
    $form['identification']['site']['name']['#disabled'] = TRUE;
  }
  if ($this->spiController
    ->checkAcquiaHosted()) {
    $form['identification']['#description'] = $this
      ->t('Acquia hosted sites are automatically provided with a machine name.');
  }
  $form['identification']['site']['machine_name'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
      'source' => [
        'identification',
        'site',
        'name',
      ],
    ],
    '#default_value' => $this->state
      ->get('spi.site_machine_name'),
  ];
  if ($this->spiController
    ->checkAcquiaHosted()) {
    $form['identification']['site']['machine_name']['#default_value'] = $this->state
      ->get('spi.site_machine_name') ?: $this->spiController
      ->getAcquiaHostedMachineName();
    $form['identification']['site']['machine_name']['#disabled'] = TRUE;
  }
  $form['connection'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Acquia Subscription Settings'),
    '#collapsible' => FALSE,
  ];

  // Help documentation is local unless the Help module is disabled.
  if ($this->moduleHandler
    ->moduleExists('help')) {
    $help_url = Url::fromRoute('help.page', [
      'name' => 'acquia_connector',
    ])
      ->toString();
  }
  else {
    $help_url = Url::fromUri('https://docs.acquia.com/acquia-cloud/insight/install/')
      ->getUri();
  }
  if (!empty($identifier) && !empty($key)) {
    $form['connection']['spi'] = [
      '#prefix' => '<div class="acquia-spi">',
      '#suffix' => '</div>',
      '#weight' => 0,
    ];
    $form['connection']['description']['#markup'] = $this
      ->t('Allow collection and examination of the following items. <a href=":url">Learn more</a>.', [
      ':url' => $help_url,
    ]);
    $form['connection']['description']['#weight'] = '-1';
    $form['connection']['spi']['admin_priv'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Admin privileges'),
      '#default_value' => $config
        ->get('spi.admin_priv'),
    ];
    $form['connection']['spi']['send_node_user'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Nodes and users'),
      '#default_value' => $config
        ->get('spi.send_node_user'),
    ];
    $form['connection']['spi']['send_watchdog'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Watchdog logs'),
      '#default_value' => $config
        ->get('spi.send_watchdog'),
    ];
    $form['connection']['acquia_dynamic_banner'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Receive updates from Acquia Subscription'),
      '#default_value' => $config
        ->get('spi.dynamic_banner'),
    ];
    $form['connection']['alter_variables'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow Insight to update list of approved variables.'),
      '#default_value' => (int) $config
        ->get('spi.set_variables_override'),
      '#description' => $this
        ->t('Insight can set variables on your site to recommended values at your approval, but only from a specific list of variables. Check this box to allow Insight to update the list of approved variables. <a href=":url">Learn more</a>.', [
        ':url' => $help_url,
      ]),
    ];
    $use_cron = $config
      ->get('spi.use_cron');
    $form['connection']['use_cron'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Send via Drupal cron'),
      '#default_value' => $use_cron,
    ];
    $form['#attached']['library'][] = 'acquia_connector/acquia_connector.form';
    $key = sha1($this->privateKey
      ->get());

    // phpcs:ignore
    $url = Url::fromRoute('acquia_connector.send', [], [
      'query' => [
        'key' => $key,
      ],
      'absolute' => TRUE,
    ])
      ->toString();
    $form['connection']['use_cron_url'] = [
      '#type' => 'container',
      '#children' => $this
        ->t("Enter the following URL in your server's crontab to send SPI data:<br /><em>:url</em>", [
        ':url' => $url,
      ]),
      '#states' => [
        'visible' => [
          ':input[name="use_cron"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
  }
  return parent::buildForm($form, $form_state);
}