contextly-8.x-2.1/src/Form/ContextlyAdminForm.php
src/Form/ContextlyAdminForm.php
<?php
namespace Drupal\contextly\Form;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The ContextlyAdminForm class.
*/
class ContextlyAdminForm extends ConfigFormBase implements ContainerInjectionInterface {
/**
* The contextly base service.
*
* @var \Drupal\contextly\ContextlyBaseServiceInterface
*/
protected $contextlybase;
/**
* The CSRF token.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator
*/
protected $crsfToken;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->contextlybase = $container->get('contextly.base');
$instance->crsfToken = $container->get('csrf_token');
return $instance;
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'contextly.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'contextly_admin_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form,
FormStateInterface $form_state) {
$config = $this->config('contextly.settings');
$key = $config->get('api_key');
$settings_token = $this->contextlybase->settingsCpTokenValue('tour');
$form['module'] = [
'#type' => 'details',
'#title' => $this->t('Module settings'),
'#open' => empty($key),
];
if (empty($key)) {
// Add registration link if empty API key.
$link = Link::createFromRoute(
$this->t("Contextly's settings panel"),
'contextly.admin_controller_cp_tour',
['token' => $this->crsfToken->get($settings_token)],
['attributes' => ['target' => '_blank']])->toString();
$form['module']['link'] = [
'#type' => 'item',
'#markup' => $this->t('In order to communicate securely, we use a shared secret key. You can find your secret API key on @home_link. Copy and paste it below.', [
'@home_link' => $link,
]),
];
}
$form['module']['api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('API key'),
'#description' => $this->t('Enter your contextly api key.'),
'#maxlength' => 64,
'#size' => 64,
'#default_value' => $key,
];
$form['module']['server_mode'] = [
'#type' => 'select',
'#title' => $this->t('Server mode'),
'#options' => [
'live' => $this->t('Live'),
'dev' => $this->t('Development'),
],
'#default_value' => $config->get('server_mode') ?: 'dev',
];
$form['service'] = [
'#type' => 'details',
'#title' => $this->t('Service settings'),
'#open' => !empty($key),
];
$form['service']['notice'] = [
'#type' => 'item',
'#title' => $this->t('The majority of the settings for Contextly are handled outside of Drupal. Click the settings button to securely login to your settings panel. If this fails, please email us at <a href="@url">info@contextly.com</a>.', [
'@url' => 'mailto:info@contextly.com',
]),
'#default_value' => $config->get('notice'),
];
$form['service']['go'] = [
'#type' => 'submit',
'#value' => $this->t('Settings'),
'#name' => 'go',
];
$form['#theme'] = 'system_config_form';
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form,
FormStateInterface $form_state) {
switch ($form_state->getTriggeringElement()['#id']) {
case 'edit-submit':
parent::submitForm($form, $form_state);
$this->config('contextly.settings')
->set('api_key', $form_state->getValue('api_key'))
->set('server_mode', $form_state->getValue('server_mode'))
->save();
$this->contextlybase->settingsResetSharedToken();
break;
case 'edit-go':
// Redirects to Contextly service settings page.
return $this->contextlybase->settingsUrl('settings');
}
}
}
