You are here

class Single in Siteimprove 8

Provides simple plugin instance of Siteimprove Domain settings.

@package Drupal\siteimprove\Plugin\SiteimproveDomain

Plugin annotation


@SiteimproveDomain(
  id = "siteimprovedomain_single",
  label = @Translation("Single frontend domain"),
  description = @Translation("Set a single domain for Siteimprove. Useful if you have a different backend domain than frontend domain."),
)

Hierarchy

Expanded class hierarchy of Single

File

src/Plugin/SiteimproveDomain/Single.php, line 23

Namespace

Drupal\siteimprove\Plugin\SiteimproveDomain
View source
class Single extends SiteimproveDomainBase {

  /**
   * Current request service.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * {@inheritDoc}
   */
  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;
  }

  /**
   * {@inheritDoc}
   */
  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());
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('siteimprove.domain.single.settings');
    $config
      ->set('domain', $form_state
      ->getValue('single_domain'))
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  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,
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'siteimprove.domain.single.settings',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
Single::$request protected property Current request service.
Single::buildForm public function Form constructor. Overrides SiteimproveDomainBase::buildForm
Single::create public static function Creates an instance of the plugin. Overrides SiteimproveDomainBase::create
Single::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides SiteimproveDomainBase::getEditableConfigNames
Single::getUrls public function Return urls for active domains for this entity. Overrides SiteimproveDomainBase::getUrls
Single::submitForm public function Form submission handler. Overrides SiteimproveDomainBase::submitForm
Single::validateForm public function Form validation handler. Overrides SiteimproveDomainBase::validateForm
Single::__construct public function Constructs a new SiteimproveDomainBase object. Overrides SiteimproveDomainBase::__construct
SiteimproveDomainBase::$configFactory protected property The config factory.
SiteimproveDomainBase::getName public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.