You are here

public function WebhooksSettingsForm::buildForm in Acquia Content Hub 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/WebhooksSettingsForm.php, line 77

Class

WebhooksSettingsForm
Defines the form to register the webhooks.

Namespace

Drupal\acquia_contenthub\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this->configFactory
    ->getEditable('acquia_contenthub.admin_settings');
  $form['webhook_settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Administer Webhooks'),
    '#collapsible' => TRUE,
    '#description' => $this
      ->t('Manage Acquia Content Hub Webhooks'),
  ];
  if ($config
    ->get('webhook_url')) {
    $webhook_url = $config
      ->get('webhook_url');
  }
  else {
    $webhook_url = Url::fromUri('internal:/acquia-contenthub/webhook', [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  $webhook_uuid = $config
    ->get('webhook_uuid');

  // Match $remote_uuid via $webhook_url from service response.
  $remote_uuid = NULL;
  $settings = $this->contentHubSubscription
    ->getSettings();
  if ($settings) {

    // Ask service about webhooks.
    $webhooks = $settings
      ->getWebhooks();
    foreach ($webhooks as $webhook) {
      if ($webhook['url'] === $webhook_url) {
        $remote_uuid = $webhook['uuid'];
        break;
      }
    }
  }

  // Fix local state if it does not match service state.
  if ($remote_uuid != $webhook_uuid) {
    $config
      ->set('webhook_uuid', $remote_uuid);
    $config
      ->set('webhook_url', $webhook_url);
    $config
      ->save();
    $webhook_uuid = $remote_uuid;
  }
  if ((bool) $webhook_uuid) {
    $title = $this
      ->t('Receive Webhooks (uuid = %uuid)', [
      '%uuid' => $webhook_uuid,
    ]);
  }
  else {
    $title = $this
      ->t('Receive Webhooks');
  }
  $form['webhook_settings']['webhook_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Acquia Content Hub URL'),
    '#description' => $this
      ->t('Please use a full URL (Ex. http://example.com/acquia-contenthub/webhook). This is the end-point where this site will receive webhooks from Acquia Content Hub.'),
    '#default_value' => $webhook_url,
    '#required' => TRUE,
  ];
  $form['webhook_settings']['webhook_uuid'] = [
    '#type' => 'checkbox',
    '#title' => $title,
    '#default_value' => (bool) $webhook_uuid,
    '#description' => $this
      ->t('Webhooks must be enabled to receive updates from Acquia Content Hub'),
  ];
  return parent::buildForm($form, $form_state);
}