Initial commit
This commit is contained in:
commit
520821c6a6
5
composer.json
Normal file
5
composer.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "drupal/simple_message",
|
||||
"description": "Displays a simple message.",
|
||||
"type": "drupal-module"
|
||||
}
|
6
config/schema/simple_message.schema.yml
Normal file
6
config/schema/simple_message.schema.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
simple_message.config:
|
||||
type: config_object
|
||||
label: Simple Message
|
||||
mapping:
|
||||
message:
|
||||
type: text
|
5
simple_message.info.yml
Normal file
5
simple_message.info.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
name: Simple Message
|
||||
description: Displays a simple message.
|
||||
core_version_requirement: ^8 || ^9
|
||||
type: module
|
||||
package: Custom
|
14
simple_message.services.yml
Normal file
14
simple_message.services.yml
Normal file
|
@ -0,0 +1,14 @@
|
|||
services:
|
||||
Drupal\Core\Config\ConfigFactoryInterface:
|
||||
alias: config.factory
|
||||
|
||||
Drupal\Core\Messenger\MessengerInterface:
|
||||
alias: messenger
|
||||
|
||||
Drupal\Core\Routing\AdminContext:
|
||||
alias: router.admin_context
|
||||
|
||||
Drupal\simple_message\DisplaySimpleMessage:
|
||||
autowire: true
|
||||
tags:
|
||||
- { name: event_subscriber }
|
50
src/DisplaySimpleMessage.php
Normal file
50
src/DisplaySimpleMessage.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\simple_message;
|
||||
|
||||
use Drupal\Core\Routing\AdminContext;
|
||||
use Drupal\Core\Messenger\MessengerInterface;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
final class DisplaySimpleMessage implements EventSubscriberInterface {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
private $messenger;
|
||||
private $adminContext;
|
||||
private $config;
|
||||
|
||||
public function __construct(
|
||||
MessengerInterface $messenger,
|
||||
AdminContext $adminContext,
|
||||
ConfigFactoryInterface $configFactory
|
||||
) {
|
||||
$this->messenger = $messenger;
|
||||
$this->adminContext = $adminContext;
|
||||
$this->config = $configFactory->get('simple_message.config');
|
||||
}
|
||||
|
||||
public function displayMessage(GetResponseEvent $event) {
|
||||
if ($this->adminContext->isAdminRoute()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($message = $this->config->get('message')) {
|
||||
$this->messenger->addMessage($this->t($message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
$events[KernelEvents::REQUEST][] = ['displayMessage'];
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue