Hide generator metatag

Components

If you want to hide the generator metatag that exposes information about your Drupal version then you can use the hook_page_attachments_alter() hook. This applies only for versions 8 and 9!

/**
 * Implements hook_page_attachments_alter().
 */
function MY_MODULE_page_attachments_alter(array &$attachments) {
  foreach ($attachments['#attached']['html_head'] as $key => $attachment) {
    if (isset($attachment[1]) && $attachment[1] == 'system_meta_generator') {
      unset($attachments['#attached']['html_head'][$key]);
    }
  }
}