View source
<?php
namespace Drupal\siteimprove\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use GuzzleHttp\Client;
use Drupal\Core\Messenger\Messenger;
use Drupal\siteimprove\Plugin\SiteimproveDomainManager;
use Drupal\siteimprove\SiteimproveUtils;
use Drupal\taxonomy\Entity\Vocabulary;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\Cache;
class SettingsForm extends ConfigFormBase {
protected $siteimprove;
protected $pluginManagerSiteimproveDomain;
protected $messenger;
protected $httpClient;
public function __construct(ConfigFactoryInterface $config_factory, SiteimproveUtils $siteimprove, SiteimproveDomainManager $pluginManagerSiteimproveDomain, Messenger $messenger, Client $httpClient) {
parent::__construct($config_factory);
$this->siteimprove = $siteimprove;
$this->pluginManagerSiteimproveDomain = $pluginManagerSiteimproveDomain;
$this->messenger = $messenger;
$this->httpClient = $httpClient;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('siteimprove.utils'), $container
->get('plugin.manager.siteimprove_domain'), $container
->get('messenger'), $container
->get('http_client'));
}
protected function getEditableConfigNames() {
return [
'siteimprove.settings',
];
}
public function getFormId() {
return 'siteimprove_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('siteimprove.settings');
$form['container'] = [
'#title' => $this
->t('Token'),
'#type' => 'fieldset',
];
$form['container']['token'] = [
'#default_value' => $config
->get('token'),
'#description' => $this
->t('Configure Siteimprove Plugin token.'),
'#maxlength' => 50,
'#prefix' => '<div id="token-wrapper">',
'#required' => TRUE,
'#size' => 50,
'#suffix' => '</div>',
'#title' => $this
->t('Token'),
'#type' => 'textfield',
];
$form['container']['request_new_token'] = [
'#ajax' => [
'callback' => '::requestToken',
'wrapper' => 'token-wrapper',
],
'#limit_validation_errors' => [],
'#type' => 'button',
'#value' => $this
->t('Request new token'),
];
$plugins = $this->pluginManagerSiteimproveDomain
->getDefinitions();
$plugin_definitions = [];
$options = [];
foreach ($plugins as $plugin) {
$options[$plugin['id']] = $plugin['label'];
$plugin_definitions[$plugin['id']] = $plugin;
}
$form['domain'] = [
'#title' => $this
->t('Frontend domain'),
'#type' => 'fieldset',
];
$form['domain']['domain_plugin'] = [
'#type' => 'select',
'#title' => $this
->t('Siteimprove Domain Plugins'),
'#description' => $this
->t('Choose which Siteimprove Domain plugin to use'),
'#options' => $options,
'#size' => 1,
'#default_value' => $config
->get('domain_plugin_id'),
'#weight' => '0',
];
foreach ($options as $key => $option) {
$plugin_definition = $plugin_definitions[$key];
$plugin = $this->pluginManagerSiteimproveDomain
->createInstance($plugin_definition['id']);
$plugin
->buildForm($form, $form_state, $plugin_definition);
$form[$plugin_definition['id']]['#states']['visible'] = [
':input[name="domain_plugin"]' => [
'value' => $plugin_definition['id'],
],
];
$form['domain'][$plugin_definition['id']] = [
'#type' => 'markup',
'#markup' => '<strong>' . $plugin_definition['label'] . '</strong><br />' . $plugin_definition['description'],
'#prefix' => '<div name="' . $plugin_definition['id'] . '_description' . '">',
'#suffix' => '</div>',
];
}
$form['prepublish'] = [
'#title' => $this
->t('Prepublish check'),
'#type' => 'fieldset',
];
$form['prepublish']['description'] = [
'#markup' => "<p>" . $this
->t("When this is enabled, it's possible to perform a SiteImprove prepublish check when editing content, before publishing content.") . "</p>",
];
$form['prepublish']['prepublish_enabled'] = [
'#title' => $this
->t('Enable prepublish check'),
'#type' => 'checkbox',
'#default_value' => $config
->get('prepublish_enabled'),
];
$form['prepublish']['api_username'] = [
'#type' => 'textfield',
'#description' => 'SiteImprove API username',
'#default_value' => $config
->get('api_username'),
'#states' => [
'enabled' => [
':input[name="prepublish_enabled"]' => [
'checked' => TRUE,
],
],
],
];
$form['prepublish']['api_key'] = [
'#type' => 'textfield',
'#description' => 'SiteImprove API key',
'#default_value' => $config
->get('api_key'),
'#states' => [
'enabled' => [
':input[name="prepublish_enabled"]' => [
'checked' => TRUE,
],
],
],
];
if ($config
->get('prepublish_enabled')) {
$url = 'https://api.siteimprove.com/v2/settings/content_checking';
$res = $this->httpClient
->request('GET', $url, [
'auth' => [
$config
->get('api_username'),
$config
->get('api_key'),
],
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$form['prepublish']['api'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Siteimprove API response'),
'#states' => [
'visible' => [
':input[name="prepublish_enabled"]' => [
'checked' => TRUE,
],
],
],
];
if ($res
->getStatusCode() == 200) {
$form['#attached']['library'][] = 'siteimprove/siteimprove.settings';
$result = Json::decode($res
->getBody());
if (isset($result['is_ready']) && $result['is_ready']) {
$form['prepublish']['api']['is_ready'] = [
'#type' => 'markup',
'#markup' => '<p><span class="prepublish-is-ready">' . $this
->t('Prepublish check has been enabled') . '</span></p>',
];
}
else {
$form['prepublish']['api']['is_ready'] = [
'#type' => 'markup',
'#markup' => '<p><span class="prepublish-is-not-ready">' . $this
->t('Prepublish check has not been enabled yet in Siteimprove. This can take several minutes. Check back later.') . '</span></p>',
];
$this
->setRepublish($config
->get('api_username'), $config
->get('api_key'));
}
}
else {
$form['prepublish']['api']['error'] = [
'#type' => 'markup',
'#markup' => '<p><span>' . $this
->t('There were problems contacting the API - see error below. Check your username and API key.') . '</span></p>' . '<p>HTTP status: <strong>' . $res
->getStatusCode() . ' ' . $res
->getReasonPhrase() . '</strong></p>',
];
}
}
$form['prepublish']['content_types'] = [
'#type' => 'fieldset',
'#title' => 'Enabled content types',
'#states' => [
'visible' => [
':input[name="prepublish_enabled"]' => [
'checked' => TRUE,
],
],
],
];
$options = node_type_get_names();
$form['prepublish']['content_types']['enabled_content_types'] = [
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $config
->get('enabled_content_types'),
'#title' => $this
->t('Select prepublish check enabled content types'),
'#description' => $this
->t('Select which content types Siteimprove Prepublish check is enabled for'),
];
$form['prepublish']['taxonomies'] = [
'#type' => 'fieldset',
'#title' => 'Enabled taxonomies',
'#states' => [
'visible' => [
':input[name="prepublish_enabled"]' => [
'checked' => TRUE,
],
],
],
];
$vocabulary_names = taxonomy_vocabulary_get_names();
$vocabularies = Vocabulary::loadMultiple($vocabulary_names);
$taxonomy_options = [];
foreach ($vocabularies as $vocabulary) {
$taxonomy_options[$vocabulary
->id()] = $vocabulary
->label();
}
$form['prepublish']['taxonomies']['enabled_taxonomies'] = [
'#type' => 'checkboxes',
'#options' => $taxonomy_options,
'#default_value' => $config
->get('enabled_taxonomies'),
'#title' => $this
->t('Select prepublish check enabled taxonomies'),
'#description' => $this
->t('Select which taxonomies Siteimprove Prepublish check is enabled for'),
];
Cache::invalidateTags([
'siteimprove_toolbar',
]);
return parent::buildForm($form, $form_state);
}
protected function setRepublish($username, $key) {
$url = 'https://api.siteimprove.com/v2/settings/content_checking';
$res = $this->httpClient
->request('POST', $url, [
'auth' => [
$username,
$key,
],
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
}
public function requestToken(array &$form, FormStateInterface &$form_state) {
if ($token = $this->siteimprove
->requestToken()) {
$form['container']['token']['#value'] = $token;
}
else {
$this->messenger
->addError($this
->t('There was an error requesting a new token. Please try again in a few minutes.'));
}
$form_state
->setRebuild(TRUE);
return $form['container']['token'];
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$domain_plugin = $form_state
->getValue('domain_plugin');
$plugin = $this->pluginManagerSiteimproveDomain
->createInstance($domain_plugin);
$plugin
->validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$domain_plugin = $form_state
->getValue('domain_plugin');
$this
->config('siteimprove.settings')
->set('token', $form_state
->getValue('token'))
->set('domain_plugin_id', $domain_plugin)
->set('prepublish_enabled', $form_state
->getValue('prepublish_enabled'))
->set('api_username', $form_state
->getValue('api_username'))
->set('api_key', $form_state
->getValue('api_key'))
->set('enabled_content_types', $form_state
->getValue('enabled_content_types'))
->set('enabled_taxonomies', $form_state
->getValue('enabled_taxonomies'))
->save();
$plugin = $this->pluginManagerSiteimproveDomain
->createInstance($domain_plugin);
$plugin
->submitForm($form, $form_state);
}
}