class RemoveHttpHeadersSettings in Remove HTTP headers 8
Form for the module settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\remove_http_headers\Form\RemoveHttpHeadersSettings
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of RemoveHttpHeadersSettings
1 string reference to 'RemoveHttpHeadersSettings'
File
- src/
Form/ RemoveHttpHeadersSettings.php, line 14
Namespace
Drupal\remove_http_headers\FormView 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><meta name='Generator' value='Drupal (version N)'></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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
RemoveHttpHeadersSettings:: |
private | property | The config manager service. | |
RemoveHttpHeadersSettings:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
RemoveHttpHeadersSettings:: |
private | function | Converts the value of the headers to remove textarea to an array. | |
RemoveHttpHeadersSettings:: |
public static | function |
Overrides ConfigFormBase:: |
|
RemoveHttpHeadersSettings:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
RemoveHttpHeadersSettings:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
RemoveHttpHeadersSettings:: |
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:: |
|
RemoveHttpHeadersSettings:: |
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:: |
|
RemoveHttpHeadersSettings:: |
public | function |
RemoveResponseHeaders constructor. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |