Skip to content

Commit

Permalink
Fixes #3678 Add Generator Metatag (#3680)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadean authored Sep 11, 2024
1 parent 317d3e7 commit a5c8635
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
20 changes: 20 additions & 0 deletions modules/custom/az_core/az_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ function _az_core_check_attribute_form(array &$form, FormStateInterface $form_st
}
}

/**
* Implements hook_page_attachments_alter().
*
* Override Drupal core generator metatag for distribution.
*/
function az_core_page_attachments_alter(array &$page) {
// Verify we're not trying to add tag to some page that should not have it.
if (!empty($page['#attached']['html_head'])) {
foreach ($page['#attached']['html_head'] as &$head_item) {
// Check if the current item is the meta generator.
if (is_array($head_item) && in_array('system_meta_generator', $head_item, TRUE)) {
// Update the tag if it's present.
if (!empty($head_item[0]['#attributes']['content'])) {
$head_item[0]['#attributes']['content'] = 'Arizona Quickstart (https://quickstart.arizona.edu)';
}
}
}
}
}

/**
* Form validation that fails validation on protected term pages.
*
Expand Down
4 changes: 4 additions & 0 deletions modules/custom/az_core/az_core.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ services:
- '@current_route_match'
tags:
- { name: page_cache_response_policy }
az_core_generator:
public: false
class: Drupal\az_core\EventSubscriber\AZGeneratorSubscriber
decorates: response_generator_subscriber
logger.channel.az_core:
parent: logger.channel_base
arguments:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Drupal\az_core\EventSubscriber;

use Drupal\Core\EventSubscriber\ResponseGeneratorSubscriber;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

/**
* Subscriber to add distribution X-Generator header tag.
*/
class AZGeneratorSubscriber extends ResponseGeneratorSubscriber {

/**
* Sets replacement X-Generator header on successful responses.
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The event to process.
*/
public function onRespond(ResponseEvent $event) {
if (!$event->isMainRequest()) {
return;
}

$response = $event->getResponse();

$response->headers->set('X-Generator', 'Arizona Quickstart (https://quickstart.arizona.edu)');
}

}

0 comments on commit a5c8635

Please sign in to comment.