InviteConfig.php in Invite 8
File
src/Form/InviteConfig.php
View source
<?php
namespace Drupal\invite\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InviteConfig extends ConfigFormBase {
protected $routeProvider;
public function __construct(ConfigFactoryInterface $config_factory, RouteProvider $routeProvider) {
parent::__construct($config_factory);
$this->routeProvider = $routeProvider;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('router.route_provider'));
}
protected function getEditableConfigNames() {
return [
'invite.invite_config',
];
}
public function getFormId() {
return 'invite_config';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('invite.invite_config');
$form['invite_expiration'] = [
'#type' => 'textfield',
'#title' => $this
->t('Invite Expiration'),
'#description' => $this
->t('Enter the number of days before the invitation expires.'),
'#maxlength' => 6,
'#size' => 6,
'#default_value' => $config
->get('invite_expiration'),
];
$form['accept_redirect'] = [
'#type' => 'textfield',
'#required' => FALSE,
'#title' => $this
->t('Accept Redirect'),
'#description' => $this
->t('The route the user will be redirected to when registering. Defaults to "user.register"'),
'#default_value' => $config
->get('accept_redirect'),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$route_name = $form_state
->getValue('accept_redirect');
$route_exists = count($this->routeProvider
->getRoutesByNames([
$route_name,
])) === 1;
if (!$route_exists) {
$form_state
->setErrorByName('accept_redirect', $this
->t('Route "@route" does not exist.', [
'@route' => $route_name,
]));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config('invite.invite_config')
->set('invite_expiration', $form_state
->getValue('invite_expiration'))
->set('accept_redirect', $form_state
->getValue('accept_redirect'))
->save();
}
}