You are here

public function ContentHubSettingsForm::buildForm in Acquia Content Hub 8.2

Same name and namespace in other branches
  1. 8 src/Form/ContentHubSettingsForm.php \Drupal\acquia_contenthub\Form\ContentHubSettingsForm::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/ContentHubSettingsForm.php, line 115

Class

ContentHubSettingsForm
Defines the form to configure the Content Hub connection settings.

Namespace

Drupal\acquia_contenthub\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $provider = $this->clientFactory
    ->getProvider();
  $this->settings = $this->clientFactory
    ->getSettings();
  $this->client = $this->clientFactory
    ->getClient();
  $readonly = $provider !== 'core_config';
  $connected = !empty($this->client);
  $client_name = '';
  $is_suppressed = $this
    ->config('acquia_contenthub.admin_settings')
    ->get('is_suppressed');
  $form['actions']['env_var'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Register with environment variable'),
    '#description' => $this
      ->t('Credentials are set in environment variables.'),
    '#open' => TRUE,
    '#access' => $provider === 'environment_variable',
  ];
  $form['actions']['env_var']['remove_suppression'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Register site'),
    '#button_type' => 'primary',
    '#weight' => 100,
    '#limit_validation_errors' => [],
    '#access' => $is_suppressed,
    '#submit' => [
      [
        $this,
        'removeWebhookSuppression',
      ],
    ],
  ];
  $form['actions']['env_var']['suppress_webhook'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Unregister site'),
    '#button_type' => 'primary',
    '#weight' => 100,
    '#limit_validation_errors' => [],
    '#access' => !$is_suppressed,
    '#submit' => [
      [
        $this,
        'suppressWebhook',
      ],
    ],
  ];
  if ($connected) {
    $client_name = $this
      ->ensureClientNameIsSynced();
  }
  if ($readonly && $connected && $provider !== 'environment_variable') {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Settings are currently read-only, provided by %provider. Values shown for informational purposes only.', [
      '%provider' => $provider,
    ]));
  }
  if ($readonly && !$connected) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Warning, you are not connected to Content Hub, but your settings provider (%provider) does not allow settings changes. Values shown for informational purposes only.', [
      '%provider' => $provider,
    ]), 'warning');
    $connected = TRUE;
  }
  $form['settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Connection Settings'),
    '#collapsible' => TRUE,
    '#description' => $this
      ->t('Settings for connection to Acquia Content Hub'),
    '#disabled' => $connected,
    '#open' => !$connected,
    '#access' => $provider !== 'environment_variable',
  ];
  $form['settings']['send_contenthub_updates'] = [
    '#type' => 'checkbox',
    '#title' => 'Send updates to Content Hub Service.',
    '#description' => 'Disable this flag with drush in case of Service degradation.',
    '#disabled' => TRUE,
    '#default_value' => $this
      ->config('acquia_contenthub.admin_settings')
      ->get('send_contenthub_updates') ?? TRUE,
  ];
  $form['settings']['hostname'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Acquia Content Hub Hostname'),
    '#description' => $this
      ->t('The hostname of the Acquia Content Hub API without trailing slash at end of URL, e.g. http://localhost:5000'),
    '#default_value' => $this->settings
      ->getUrl(),
    '#required' => TRUE,
  ];
  $form['settings']['api_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('API Key'),
    '#default_value' => $this->settings
      ->getApiKey(),
    '#required' => TRUE,
  ];
  $form['settings']['secret_key'] = [
    '#type' => 'password',
    '#title' => $this
      ->t('Secret Key'),
    '#default_value' => $this->settings
      ->getSecretKey(),
    '#required' => TRUE,
  ];
  $form['settings']['client_name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Client Name'),
    '#default_value' => $client_name,
    '#required' => TRUE,
    '#description' => $this
      ->t('A unique client name by which Acquia Content Hub will identify this site. The name of this client site cannot be changed once set.'),
  ];
  $webhook_is_disabled = FALSE;
  if ($this->client) {
    $webhooks = $this->client
      ->getWebHooks();
    $webhook_uuid = $this->settings
      ->getWebhook('uuid');

    /** @var \Acquia\ContentHubClient\Webhook $webhook */
    $webhook = current(array_filter($webhooks, function (Webhook $webhook) use ($webhook_uuid) {
      return $webhook
        ->getUuid() === $webhook_uuid;
    }));
    $webhook_is_disabled = $webhook ? !$webhook
      ->isEnabled() : FALSE;
  }
  $webhook_description = $this
    ->t('This should be the domain (and drupal subpath if applicable). Do not include trailing slash.');
  if ($webhook_is_disabled) {
    $webhook_description .= ' <b>' . $this
      ->t('To re-enable the webhook please click "Update Public URL" button.') . '</b>';
  }
  $form['settings']['webhook'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Publicly Accessible URL'),
    '#required' => TRUE,
    '#default_value' => $this->settings
      ->getWebhook() ?: $GLOBALS['base_url'],
    '#description' => $webhook_description,
    '#disabled' => $webhook_is_disabled,
  ];
  $form['settings']['origin'] = [
    '#type' => 'item',
    '#title' => $this
      ->t("Site's Origin UUID"),
    '#markup' => $this->settings
      ->getUuid(),
  ];
  if ($readonly) {
    return $form;
  }
  $form = parent::buildForm($form, $form_state);
  if (!$connected) {
    $form['actions']['submit']['#value'] = $this
      ->t('Register Site');
    $form['actions']['submit']['#name'] = 'register_site';
    return $form;
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t('Site successfully connected to Content Hub. To change connection settings, unregister the site first.'));
  $form['webhook_details'] = [
    '#type' => 'fieldset',
    '#title' => 'Webhook',
    '#collapsible' => FALSE,
    '#open' => TRUE,
  ];
  $form['webhook_details']['webhook'] = $form['settings']['webhook'];
  unset($form['settings']['webhook']);
  $form['actions']['updatewebhook'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Update Public URL'),
    '#button_type' => 'primary',
    '#weight' => 100,
    '#submit' => [
      [
        $this,
        'updateWebhook',
      ],
    ],
    '#validate' => [
      [
        $this,
        'validateWebhook',
      ],
    ],
    '#name' => 'update_webhook',
  ];
  $form['actions']['unregister'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Unregister Site'),
    '#url' => Url::fromRoute('acquia_contenthub.delete_client_confirm'),
    '#weight' => 999,
    '#limit_validation_errors' => [],
    '#attributes' => [
      'class' => [
        'use-ajax',
        'button',
      ],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => json_encode([
        'width' => '50%',
      ]),
    ],
    // No validation at all is required in the equivocate case, so
    // we include this here to make it skip the form-level validator.
    '#validate' => [],
    '#name' => 'unregister_site',
  ];
  unset($form['actions']['submit']);
  return $form;
}