Skip to content

Commit

Permalink
Merge pull request #51 from UN-OCHA/OPS-10197-3
Browse files Browse the repository at this point in the history
feat: Separate home page
  • Loading branch information
attiks authored Mar 28, 2024
2 parents 613a070 + 45ef6a9 commit 3a3cb10
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/views.view.my_documents.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ display:
plugin_id: text_custom
label: ''
empty: true
content: "<p>Welcome to OCHA’s internal AI-based document summarization tool. This proof-of-concept is only available to OCHA personnel.</p>\r\n\r\n<p class=\"button-login\"><a href=\"/user/login/hid\">Log in</a></p>"
content: "<p>Welcome to OCHA’s internal AI-based document summarization tool.</p>\r\n\r\n<p class=\"button-login\"><a href=\"/node/add\">Add your first document.</a></p>"
tokenize: false
title:
id: title
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
ocha_ai_summarize.event_subscriber:
class: Drupal\ocha_ai_summarize\EventSubscriber\AnonymousFrontpageSubscriber
arguments: ['@messenger','@current_user','@path.matcher', '@config.factory', '@state']
tags:
- { name: event_subscriber }
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Drupal\ocha_ai_summarize\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\State\State;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Custom Frontpage for anonymous users event subscriber.
*/
class AnonymousFrontpageSubscriber implements EventSubscriberInterface {

/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;

/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;

/**
* The path matcher service.
*
* @var \Drupal\Core\Path\PathMatcherInterface
*/
protected $pathMatcher;

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* The config factory.
*
* @var \Drupal\Core\State\State
*/
protected $state;

/**
* Constructs event subscriber.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Path\PathMatcherInterface $pathMatcher
* The path matcher.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
* @param \Drupal\Core\State\State $state
* The state service.
*/
public function __construct(MessengerInterface $messenger,
AccountProxyInterface $currentUser,
PathMatcherInterface $pathMatcher,
ConfigFactoryInterface $configFactory,
State $state) {
$this->messenger = $messenger;
$this->currentUser = $currentUser;
$this->pathMatcher = $pathMatcher;
$this->configFactory = $configFactory;
$this->state = $state;
}

/**
* Kernel request event handler.
*
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
* Response event.
*/
public function onKernelRequest(RequestEvent $event) {
// Make sure front page module is not run when using cli (drush).
// Make sure front page module does not run when installing Drupal either.
if (PHP_SAPI === 'cli' || InstallerKernel::installationAttempted()) {
return;
}

// Don't run when site is in maintenance mode.
if ($this->state->get('system.maintenance_mode')) {
return;
}

// Ignore non index.php requests (like cron).
if (!empty($_SERVER['SCRIPT_FILENAME'])
&& realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
return;
}

// Ignore authenticated users.
if ($this->currentUser->isAuthenticated()) {
return;
}

if ($this->pathMatcher->isFrontPage()) {
$url_object = Url::fromUserInput('/home');
$event->setResponse(new RedirectResponse($url_object->toString()));
}

}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => ['onKernelRequest'],
];
}

}

0 comments on commit 3a3cb10

Please sign in to comment.