drupalorg-1.0.x-dev/src/Form/GitLabSettingsForm.php

src/Form/GitLabSettingsForm.php
<?php

namespace Drupal\drupalorg\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\drupalorg\Utilities\GitLabTokenRenew;

/**
 * Settings form for DrupalOrg GitLab integration.
 */
class GitLabSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'drupalorg.gitlab_settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'drupalorg_gitlab_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('drupalorg.gitlab_settings');
    $applied_config = \Drupal::config('drupalorg.gitlab_settings');
    try {
      $can_connect = TRUE;
      $days_to_expiry = (new GitLabTokenRenew())->daysToExpiry();
    }
    catch (\Throwable $e) {
      $can_connect = FALSE;
      $days_to_expiry = $this->t('-unknown-');
    }

    if ($config->get('token') === 'CHANGE-ME') {
      $this->messenger()->addError($this->t('Please change the default token for a valid one.'));
    }
    if (!$can_connect) {
      $this->messenger()->addError($this->t('The current host or token are not valid.'));
    }

    $extra_host = '';
    if ($applied_config->get('host') !== $config->get('host')) {
      $extra_host = '<br>' . $this->t('The actual value for this environment is %host', ['%host' => $applied_config->get('host')]);
    }
    $form['host'] = [
      '#type' => 'url',
      '#title' => $this->t('GitLab instance host'),
      '#default_value' => $config->get('host'),
      '#description' => $this->t('GitLab instance host URL. eg: https://git.drupalcode.org') . $extra_host,
      '#required' => TRUE,
    ];
    $form['token'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Token'),
      '#default_value' => '',
      '#placeholder' => $this->concealValue($config->get('token')),
      '#description' => $this->t('<b>Only populate it to change the existing value.</b><br>GitLab instance token. You can generate it in <a target="_blank" href="@url">here</a>.', [
        '@url' => $applied_config->get('host') . '/-/user_settings/personal_access_tokens',
      ]),
    ];
    $form['renew_token_on_cron'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Renew Token on cron'),
      '#default_value' => $config->get('renew_token_on_cron'),
      '#description' => $this->t('Current token expires in %days day(s).', ['%days' => $days_to_expiry]),
    ];
    $form['webhook_token'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Webhook token'),
      '#default_value' => '',
      '#placeholder' => $this->concealValue($config->get('webhook_token')),
      '#description' => $this->t('<b>Only populate it to change the existing value.</b><br>The token to validate webhook requests. Set to a random string that will be used when setting up webhooks in GitLab.'),
    ];

    $form['#theme'] = 'system_config_form';
    return parent::buildForm($form, $form_state);
  }

  /**
   * Displays the first and last character of a value and asterisks in between.
   *
   * @param string $value
   *   Value to conceal.
   *
   * @return string
   *   Concealed value.
   */
  protected function concealValue($value): string {
    $length = strlen($value);
    if ($length <= 2) {
      $value .= '**';
    }
    return $value[0] . str_repeat('*', $length - 2) . $value[$length - 1];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $config = $this->config('drupalorg.gitlab_settings');
    $values = $form_state->getValues();

    if (empty($values['token'])) {
      $values['token'] = $config->get('token');
    }
    if (empty($values['webhook_token'])) {
      $values['webhook_token'] = $config->get('webhook_token');
    }

    $config
      ->set('host', $values['host'])
      ->set('token', $values['token'])
      ->set('renew_token_on_cron', $values['renew_token_on_cron'])
      ->set('webhook_token', $values['webhook_token'])
      ->save();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc