You are here

class W3CValidatorOperationForm in W3C Validator 8

Provides the operation form on report page.

Hierarchy

Expanded class hierarchy of W3CValidatorOperationForm

File

src/Form/W3CValidatorOperationForm.php, line 16

Namespace

Drupal\w3c_validator\Form
View source
class W3CValidatorOperationForm extends FormBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The W3C process service.
   *
   * @var \Drupal\w3c_validator\W3CProcessor
   */
  protected $w3cProcessor;

  /**
   * Constructs a new W3CValidatorOperationForm.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\w3c_validator\W3CProcessor $w3c_processor
   *   The validation processor.
   */
  public function __construct(Connection $connection, W3CProcessor $w3c_processor) {
    $this->connection = $connection;
    $this->w3cProcessor = $w3c_processor;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('w3c.processor'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'w3c_validator_operation_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $validator_url = $this->w3cProcessor
      ->getValidatorUrl();
    $warning = '';
    if ($this
      ->isRateLimitService($validator_url)) {
      $warning = $this
        ->t('This will revalidate all of the following pages. This operation may be long.');
      $warning .= '<br/><br/><i><b>' . $this
        ->t('BEWARE : You are using the W3C HTML Validator at @path.', [
        '@path' => Link::fromTextAndUrl($validator_url, Url::fromUri($validator_url))
          ->toString(),
      ]) . '<br/>' . $this
        ->t('Using an external online service may be consider spam and abuse of service. Therefore, performing this operation, you may get banned temporarily.') . '<br/>' . $this
        ->t('Configure a local service on @configuration', [
        '@configuration' => Link::createFromRoute('W3C validator settings page', 'w3c_validator.settings')
          ->toString(),
      ]) . '</b></i>';
    }
    $form['advanced_operations'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('advanced operations'),
      '#description' => $warning,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];

    // Load module settings.
    $module_settings = $this
      ->configFactory()
      ->get('w3c_validator.settings');

    // Use admin helper tool option settings.
    $form['advanced_operations']['use_token'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Validate as logged-in user.'),
      '#description' => $this
        ->t('If enabled, pages will be validated as you can see it. Otherwise, as an anonymous user.'),
      '#default_value' => $module_settings
        ->get('use_token'),
    ];

    // Include admin pages.
    $form['advanced_operations']['admin_pages'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Include admin pages.'),
      '#description' => $this
        ->t('If enabled, admin pages will be included in validation.'),
      '#default_value' => $module_settings
        ->get('admin_pages'),
    ];
    $form['advanced_operations']['w3c_validator_revalidate_all'] = [
      '#type' => 'submit',
      '#value' => 'Re-validate all pages',
      '#prefix' => '<br/>',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();

    // Save global configurations.
    $this->configFactory
      ->getEditable('w3c_validator.settings')
      ->set('use_token', $values['use_token'])
      ->set('admin_pages', $values['admin_pages'])
      ->save();
    $form_state
      ->setRedirect('w3c_validator.confirm');
  }

  /**
   * Checks if service belong to a known rate limited service.
   *
   * @param string $service
   *   The service name.
   *
   * @return bool
   *   true/false if the service is know to have a rate limit.
   */
  protected function isRateLimitService($service) {
    $services = [
      'validator.nu',
      'validator.w3.org',
    ];
    return in_array(parse_url($service, PHP_URL_HOST), $services);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
W3CValidatorOperationForm::$connection protected property The database connection.
W3CValidatorOperationForm::$w3cProcessor protected property The W3C process service.
W3CValidatorOperationForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
W3CValidatorOperationForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
W3CValidatorOperationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
W3CValidatorOperationForm::isRateLimitService protected function Checks if service belong to a known rate limited service.
W3CValidatorOperationForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
W3CValidatorOperationForm::__construct public function Constructs a new W3CValidatorOperationForm.