You are here

class RemoveHttpHeadersSettings in Remove HTTP headers 8

Form for the module settings.

Hierarchy

Expanded class hierarchy of RemoveHttpHeadersSettings

1 string reference to 'RemoveHttpHeadersSettings'
remove_http_headers.routing.yml in ./remove_http_headers.routing.yml
remove_http_headers.routing.yml

File

src/Form/RemoveHttpHeadersSettings.php, line 14

Namespace

Drupal\remove_http_headers\Form
View source
class RemoveHttpHeadersSettings extends ConfigFormBase {

  /**
   * The config manager service.
   *
   * @var \Drupal\remove_http_headers\Config\ConfigManager
   */
  private $configManager;

  /**
   * RemoveResponseHeaders constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The factory for configuration objects.
   * @param \Drupal\remove_http_headers\Config\ConfigManager $configManager
   *   The config manager service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ConfigManager $configManager) {
    parent::__construct($configFactory);
    $this->configManager = $configManager;
  }

  /**
   * {@inheritdoc}
   *
   * @return static
   *   The created object.
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('remove_http_headers.config_manager'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['headers_to_remove'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('HTTP headers'),
      '#description' => $this
        ->t('Add headers that should be removed from responses, one header per line.'),
      '#default_value' => implode("\n", $this->configManager
        ->getHeadersToRemove(TRUE)),
    ];
    $form['x_generator_info'] = [
      '#markup' => $this
        ->t("If the header <code>X-Generator</code> is configured for removal, Drupal's default <code>&lt;meta name='Generator' value='Drupal (version N)'&gt;</code> will be removed from HTML output."),
      '#prefix' => '<div class="description">',
      '#suffix' => '</div>',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   *
   * We cannot add a return typehint or add a return type.
   * Because of the interface / PHPCS rule "Drupal.Commenting.FunctionComment.VoidReturn".
   *
   * @phpstan-ignore-next-line
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $headersToRemove = $this
      ->convertHeadersToRemoveValueToArray($form_state
      ->getValue('headers_to_remove'));
    foreach ($headersToRemove as $headerToRemove) {

      // Set error if any HTTP header contains whitespace.
      preg_match('/^[^\\s]+$/', $headerToRemove, $matches);
      if (count($matches) !== 1) {
        $form_state
          ->setErrorByName('headers_to_remove', $this
          ->t('The format of the "HTTP headers" field is not valid.</br>Make sure every HTTP header is on a separate line.'));
        break;
      }
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   *
   * We cannot add a return typehint or add a return type.
   * Because of the interface / PHPCS rule "Drupal.Commenting.FunctionComment.VoidReturn".
   *
   * @phpstan-ignore-next-line
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $headersToRemove = $this
      ->convertHeadersToRemoveValueToArray($form_state
      ->getValue('headers_to_remove'));
    $this->configManager
      ->saveHeadersToRemoveToConfig($headersToRemove);
    parent::submitForm($form, $form_state);
  }

  /**
   * Converts the value of the headers to remove textarea to an array.
   *
   * @param string $headersToRemoveValue
   *   The value of the headers to remove textarea.
   *
   * @return array
   *   Array with item for each textarea line.
   */
  private function convertHeadersToRemoveValueToArray($headersToRemoveValue) {

    // Convert string to array and trim empty values and spaces.
    return array_filter(array_map('trim', explode(PHP_EOL, $headersToRemoveValue)));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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::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.
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.
RemoveHttpHeadersSettings::$configManager private property The config manager service.
RemoveHttpHeadersSettings::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
RemoveHttpHeadersSettings::convertHeadersToRemoveValueToArray private function Converts the value of the headers to remove textarea to an array.
RemoveHttpHeadersSettings::create public static function Overrides ConfigFormBase::create
RemoveHttpHeadersSettings::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
RemoveHttpHeadersSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RemoveHttpHeadersSettings::submitForm public function We cannot add a return typehint or add a return type. Because of the interface / PHPCS rule "Drupal.Commenting.FunctionComment.VoidReturn". Overrides ConfigFormBase::submitForm
RemoveHttpHeadersSettings::validateForm public function We cannot add a return typehint or add a return type. Because of the interface / PHPCS rule "Drupal.Commenting.FunctionComment.VoidReturn". Overrides FormBase::validateForm
RemoveHttpHeadersSettings::__construct public function RemoveResponseHeaders constructor. Overrides ConfigFormBase::__construct
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.