social_auth_drupal-8.x-1.x-dev/src/Form/DrupalAuthSettingsForm.php
src/Form/DrupalAuthSettingsForm.php
<?php
namespace Drupal\social_auth_drupal\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\social_auth\Form\SocialAuthSettingsForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Settings form for Social Auth Drupal.
*/
class DrupalAuthSettingsForm extends SocialAuthSettingsForm {
/**
* The request context.
*
* @var \Drupal\Core\Routing\RequestContext
*/
protected $requestContext;
/**
* Constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* Used to check if route exists.
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator
* Used to check if path is valid and exists.
* @param \Drupal\Core\Routing\RequestContext $request_context
* Holds information about the current request.
*/
public function __construct(ConfigFactoryInterface $config_factory, RouteProviderInterface $route_provider, PathValidatorInterface $path_validator, RequestContext $request_context) {
parent::__construct($config_factory, $route_provider, $path_validator);
$this->requestContext = $request_context;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this class.
return new static(
// Load the services required to construct this class.
$container->get('config.factory'),
$container->get('router.route_provider'),
$container->get('path.validator'),
$container->get('router.request_context')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'social_auth_drupal_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return array_merge(
parent::getEditableConfigNames(),
['social_auth_drupal.settings']
);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('social_auth_drupal.settings');
$form['drupal_settings'] = [
'#type' => 'details',
'#title' => $this->t('Drupal Client settings'),
'#open' => TRUE,
'#description' => $this->t('You need to first create a Drupal App at <a href="@drupal-dev">@drupal-dev</a>', ['@drupal-dev' => 'https://developers.drupal.com/apps']),
];
$form['drupal_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['drupal_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['drupal_settings']['base_url'] = [
'#type' => 'textfield',
'#required' => TRUE,
'#title' => $this->t('Base Url'),
'#default_value' => $config->get('base_url'),
'#description' => $this->t('Copy the Base Url here.'),
];
$form['drupal_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 Drupal App settings.'),
'#default_value' => $GLOBALS['base_url'] . '/user/login/drupal/callback',
];
$form['drupal_settings']['authorized_javascript_origin'] = [
'#type' => 'textfield',
'#disabled' => TRUE,
'#title' => $this->t('Authorized Javascript Origin'),
'#description' => $this->t('Copy this value to <em>Authorized Javascript Origins</em> field of your Drupal App settings.'),
'#default_value' => $this->requestContext->getHost(),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$this->config('social_auth_drupal.settings')
->set('client_id', $values['client_id'])
->set('client_secret', $values['client_secret'])
->set('base_url', $values['base_url'])
->save();
parent::submitForm($form, $form_state);
}
}
