View source
<?php
namespace Drupal\simple_oauth\Entity\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class Oauth2TokenSettingsForm extends ConfigFormBase {
public function getFormId() {
return 'oauth2_token_settings';
}
protected function getEditableConfigNames() {
return [
'simple_oauth.settings',
];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = $this
->config('simple_oauth.settings');
$settings
->set('access_token_expiration', $form_state
->getValue('access_token_expiration'));
$settings
->set('refresh_token_expiration', $form_state
->getValue('refresh_token_expiration'));
$settings
->set('public_key', $form_state
->getValue('public_key'));
$settings
->set('private_key', $form_state
->getValue('private_key'));
$settings
->save();
parent::submitForm($form, $form_state);
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['access_token_expiration'] = [
'#type' => 'number',
'#title' => $this
->t('Access token expiration time'),
'#description' => $this
->t('The default value, in seconds, to be used as expiration time when creating new tokens.'),
'#default_value' => $this
->config('simple_oauth.settings')
->get('access_token_expiration'),
];
$form['refresh_token_expiration'] = [
'#type' => 'number',
'#title' => $this
->t('Refresh token expiration time'),
'#description' => $this
->t('The default value, in seconds, to be used as expiration time when creating new tokens.'),
'#default_value' => $this
->config('simple_oauth.settings')
->get('refresh_token_expiration'),
];
$form['public_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Public Key'),
'#description' => $this
->t('The path to the public key file.'),
'#default_value' => $this
->config('simple_oauth.settings')
->get('public_key'),
'#element_validate' => [
'::validateExistingFile',
],
'#required' => TRUE,
];
$form['private_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Private Key'),
'#description' => $this
->t('The path to the private key file.'),
'#default_value' => $this
->config('simple_oauth.settings')
->get('private_key'),
'#element_validate' => [
'::validateExistingFile',
],
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
public function validateExistingFile(&$element, FormStateInterface $form_state, &$complete_form) {
if (!empty($element['#value'])) {
$path = $element['#value'];
if (!file_exists($path)) {
$form_state
->setError($element, $this
->t('The %field file does not exist.', [
'%field' => $element['#title'],
]));
}
if (!is_readable($path)) {
$form_state
->setError($element, $this
->t('The %field file at the specified location is not readable.', [
'%field' => $element['#title'],
]));
}
}
}
}