View source
<?php
namespace Drupal\siteimprove\Plugin\SiteimproveDomain;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\siteimprove\Plugin\SiteimproveDomainBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class Single extends SiteimproveDomainBase {
protected $request;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactoryInterface $configFactory, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $configFactory);
$this->request = $request;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('request_stack')
->getCurrentRequest());
}
public function buildForm(array &$form, FormStateInterface &$form_state, $plugin_definition) {
parent::buildForm($form, $form_state, $plugin_definition);
$form[$plugin_definition['id']]['single_domain'] = [
'#type' => 'textfield',
'#description' => $this
->t('Input your domain name. If you leave out http:// or https://, the scheme will inherit the scheme of the web request.'),
'#default_value' => $this
->config('siteimprove.domain.single.settings')
->get('domain'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$value = $form_state
->getValue('single_domain');
if (!preg_match('/^(https?:\\/\\/)?([a-zA-Z0-9][a-zA-Z0-9-_]*\\.)*[a-zA-Z0-9]*[a-zA-Z0-9-_]*[[a-zA-Z0-9]+(:\\d+)?$/', $value)) {
$form_state
->setErrorByName('single_domain', $this
->t('Only use valid domain names in this field - no trailing slash, no trailing whitespace.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('siteimprove.domain.single.settings');
$config
->set('domain', $form_state
->getValue('single_domain'))
->save();
}
public function getUrls(EntityInterface $entity) {
$config = $this
->config('siteimprove.domain.single.settings');
$domain = $config
->get('domain');
$scheme = preg_match('/^https?:\\/\\//', $domain) ? '' : $this->request
->getScheme() . '://';
return [
$scheme . $domain,
];
}
protected function getEditableConfigNames() {
return [
'siteimprove.domain.single.settings',
];
}
}