Initial commit

This commit is contained in:
Oliver Davies 2020-07-21 12:20:45 +01:00
commit 520821c6a6
5 changed files with 80 additions and 0 deletions

5
composer.json Normal file
View file

@ -0,0 +1,5 @@
{
"name": "drupal/simple_message",
"description": "Displays a simple message.",
"type": "drupal-module"
}

View 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
View file

@ -0,0 +1,5 @@
name: Simple Message
description: Displays a simple message.
core_version_requirement: ^8 || ^9
type: module
package: Custom

View 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 }

View 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;
}
}