Without coding
Remove X-Generator is a common requirement for Drupal sites. The Mix module can deal with this and many other common situations without coding.
By checking the "Remove X-Generator" checkbox in /admin/config/mix
, the following items can easily be removed:
- The "X-Generator" in the HTTP header
- The meta tag
<meta name="Generator" content="Drupal 10 (https://www.drupal.org)">
from the page source code. - The "X-Generator" in the HTTP header.
Coding
These only take a few lines of code.
To remove the <meta name="Generator" content="Drupal 10 (https://www.drupal.org)">
, implement this in [yourmodule].module
file.
@see https://git.drupalcode.org/project/mix/-/blob/1.x/mix.module#L96
/**
* Implements hook_page_attachments_alter().
*/
function HOOK_page_attachments_alter(array &$attachments) {
foreach ($attachments['#attached']['html_head'] as $key => $attachment) {
if ($attachment[1] == 'system_meta_generator') {
unset($attachments['#attached']['html_head'][$key]);
}
}
}
To remove the "X-Generator" HTTP header, you can implement an EventSubscriber.
@see https://git.drupalcode.org/project/mix/-/blob/1.x/src/EventSubscriber/MixSubscriber.php#L113
public function onKernelResponse(ResponseEvent $event) {
$response = $event->getResponse();
$response->headers->remove('X-Generator');
}