From a225bb6d3bb444feb9fc9992e3617758bde1ae80 Mon Sep 17 00:00:00 2001 From: Juho Lehmonen Date: Thu, 27 Jun 2024 11:56:03 +0300 Subject: [PATCH] UHF-10255: Add module for noindex robots-tag if helfi_proxy can not be used. --- modules/helfi_robots_header/README.md | 8 ++++ .../helfi_robots_header.info.yml | 5 +++ .../helfi_robots_header.services.yml | 4 ++ .../RobotsResponseSubscriber.php | 40 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 modules/helfi_robots_header/README.md create mode 100644 modules/helfi_robots_header/helfi_robots_header.info.yml create mode 100644 modules/helfi_robots_header/helfi_robots_header.services.yml create mode 100644 modules/helfi_robots_header/src/EventSubscriber/RobotsResponseSubscriber.php diff --git a/modules/helfi_robots_header/README.md b/modules/helfi_robots_header/README.md new file mode 100644 index 000000000..a2cc10551 --- /dev/null +++ b/modules/helfi_robots_header/README.md @@ -0,0 +1,8 @@ +# Helfi Robots Header + +Handles robots-tag header if `helfi_proxy` module can't be installed. + +This module adds `X-Robots-Tag: noindex, nofollow` to all page responses if `DRUPAL_X_ROBOTS_TAG_HEADER` if set to 1. + +**NOTE:** Because this duplicates some functionality from `helfi_proxy` module, both modules should not be enabled at the same time. + diff --git a/modules/helfi_robots_header/helfi_robots_header.info.yml b/modules/helfi_robots_header/helfi_robots_header.info.yml new file mode 100644 index 000000000..977747a2e --- /dev/null +++ b/modules/helfi_robots_header/helfi_robots_header.info.yml @@ -0,0 +1,5 @@ +name: 'HELfi Robots headers' +type: module +description: Adds noindex and nofollow robots tags to headers. +package: Custom +core_version_requirement: ^9 || ^10 diff --git a/modules/helfi_robots_header/helfi_robots_header.services.yml b/modules/helfi_robots_header/helfi_robots_header.services.yml new file mode 100644 index 000000000..79f483fc7 --- /dev/null +++ b/modules/helfi_robots_header/helfi_robots_header.services.yml @@ -0,0 +1,4 @@ +services: + Drupal\helfi_robots_header\EventSubscriber\RobotsResponseSubscriber: + tags: + - { name: event_subscriber } diff --git a/modules/helfi_robots_header/src/EventSubscriber/RobotsResponseSubscriber.php b/modules/helfi_robots_header/src/EventSubscriber/RobotsResponseSubscriber.php new file mode 100644 index 000000000..26f89ba91 --- /dev/null +++ b/modules/helfi_robots_header/src/EventSubscriber/RobotsResponseSubscriber.php @@ -0,0 +1,40 @@ +getResponse(); + + if ((bool) getenv(self::X_ROBOTS_TAG_HEADER_NAME)) { + $response->headers->add(['X-Robots-Tag' => 'noindex, nofollow']); + } + } + +}