social_auth_vipps-8.x-2.1/src/Form/VippsAuthSettingsForm.php

src/Form/VippsAuthSettingsForm.php
<?php

namespace Drupal\social_auth_vipps\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\social_auth\Form\SocialAuthSettingsForm;

/**
 * Settings form for Social Auth Vipps.
 */
class VippsAuthSettingsForm extends SocialAuthSettingsForm {

  const SETTINGS = 'social_auth_vipps.settings';

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'social_auth_vipps_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return array_merge(
      parent::getEditableConfigNames(),
      [self::SETTINGS]
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('social_auth_vipps.settings');

    $form['vipps_settings'] = [
      '#type' => 'details',
      '#title' => $this->t('Vipps Client settings'),
      '#open' => TRUE,
      '#weight' => 0,
      '#description' => $this->t('You need to first create a Vipps App at <a href="@github-dev">@github-dev</a>',
        ['@github-dev' => 'https://portal.vipps.no']),
    ];

    $form['vipps_settings']['client_id'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Client ID'),
      '#default_value' => $config->get('client_id'),
      '#description' => $this->t('Copy the Client ID here.'),
    ];

    $form['vipps_settings']['client_secret'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Client Secret'),
      '#default_value' => $config->get('client_secret'),
      '#description' => $this->t('Copy the Client Secret here.'),
    ];

    $form['vipps_settings']['partner_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Partner mode'),
      '#default_value' => $config->get('partner_mode'),
      '#description' => $this->t('If you are a partner making API requests on behalf of a merchant.'),
    ];

    $form['vipps_settings']['partner_mode_wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Partner settings'),
      '#states' => [
        'visible' => [
          ':input[name="partner_mode"]' => ['checked' => TRUE],
        ]
      ],
    ];
    $form['vipps_settings']['partner_mode_wrapper']['merchant_serial_number'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Merchant Serial Number'),
      '#default_value' => $config->get('merchant_serial_number'),
      '#description' => $this->t("Please note that provided MSN must be for the sales unit that will be used for Login, not the Partner/Supermerchant ID"),
      '#element_validate' => [
        [get_called_class(), 'validatePartnerCredentials'],
      ],
      '#states' => [
        'required' => [
          ':input[name="partner_mode"]' => ['checked' => TRUE],
        ]
      ],
    ];
    $form['vipps_settings']['partner_mode_wrapper']['subscription_key'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Subscription Key'),
      '#default_value' => $config->get('subscription_key'),
      '#element_validate' => [
        [get_called_class(), 'validatePartnerCredentials'],
      ],
      '#states' => [
        'required' => [
          ':input[name="partner_mode"]' => ['checked' => TRUE],
        ]
      ],
    ];

    $form['vipps_settings']['test_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Test mode'),
      '#default_value' => $config->get('test_mode'),
      '#description' => $this->t('Send requests to the test server'),
    ];

    $form['vipps_settings']['show_in_login_form'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Show in login form'),
      '#default_value' => $config->get('show_in_login_form'),
      '#description' => $this->t("Show button 'Logg inn med Vipps' in the login form"),
    ];

    $form['vipps_settings']['authorized_redirect_url'] = [
      '#type' => 'textfield',
      '#disabled' => TRUE,
      '#title' => $this->t('Authorized redirect URIs'),
      '#description' => $this->t('Copy this value to <em>Authorized redirect URIs</em> field of your Vipps App settings.'),
      '#default_value' => Url::fromRoute('social_auth_vipps.callback')->setAbsolute()->toString(),
    ];

    $form['vipps_settings']['advanced'] = [
      '#type' => 'details',
      '#title' => $this->t('Advanced settings'),
      '#open' => FALSE,
    ];

    $form['vipps_settings']['advanced']['scopes'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Scopes for API call'),
      '#default_value' => $config->get('scopes'),
      '#description' => $this->t('Define any additional scopes to be requested, separated by a comma (e.g.: public_repo,user:follow).<br>
                                  The scopes \'user\' and \'user:email\' are added by default and always requested.<br>
                                  You can see the full list of valid scopes and their description <a href="@scopes">here</a>.', ['@scopes' => 'https://github.com/vippsas/vipps-login-api/blob/master/vipps-login-api.md#scopes']),
    ];

    $form['vipps_settings']['advanced']['endpoints'] = [
      '#type' => 'textarea',
      '#title' => $this->t('API calls to be made to collect data'),
      '#default_value' => $config->get('endpoints'),
      '#description' => $this->t('Define the Endpoints to be requested when user authenticates with Vipps for the first time<br>
                                  Enter each endpoint in different lines in the format <em>endpoint</em>|<em>name_of_endpoint</em>.<br>
                                  <b>For instance:</b><br>
                                  /user/repos|user_repos'),
    ];

    $form['other_settings'] = [
      '#type' => 'details',
      '#title' => $this->t('Other settings'),
      '#open' => FALSE,
      '#weight' => 3,
    ];

    $form['other_settings']['clear_cache'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Clear cache'),
      '#default_value' => $config->get('clear_cache') ?? TRUE,
      '#description' => $this->t('Clear the cache after saving configuration'),
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * Validates partner credentials.
   *
   * @param array $element
   *   Form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public static function validatePartnerCredentials(array &$element, FormStateInterface $form_state, &$complete_form) {
    if ($form_state->getValue('partner_mode') && $form_state->isValueEmpty($element['#name'])) {
      $form_state->setError($element, t('@name field is required.', ['@name' => $element['#title']]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();

    $this->config('social_auth_vipps.settings')
      ->set('client_id', $values['client_id'])
      ->set('client_secret', $values['client_secret'])
      ->set('merchant_serial_number', $values['merchant_serial_number'])
      ->set('subscription_key', $values['subscription_key'])
      ->set('partner_mode', $values['partner_mode'])
      ->set('test_mode', $values['test_mode'])
      ->set('show_in_login_form', $values['show_in_login_form'])
      ->set('scopes', $values['scopes'])
      ->set('endpoints', $values['endpoints'])
      ->save();

    if (boolval($values['clear_cache'])) {
      drupal_flush_all_caches();
    }

    parent::submitForm($form, $form_state);
  }

}

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

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