From 520821c6a6b1cfa9410cc173acfd727e9ab9cf03 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 21 Jul 2020 12:20:45 +0100 Subject: [PATCH] Initial commit --- composer.json | 5 +++ config/schema/simple_message.schema.yml | 6 +++ simple_message.info.yml | 5 +++ simple_message.services.yml | 14 +++++++ src/DisplaySimpleMessage.php | 50 +++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 composer.json create mode 100644 config/schema/simple_message.schema.yml create mode 100644 simple_message.info.yml create mode 100644 simple_message.services.yml create mode 100644 src/DisplaySimpleMessage.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..47ad0df --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "name": "drupal/simple_message", + "description": "Displays a simple message.", + "type": "drupal-module" +} diff --git a/config/schema/simple_message.schema.yml b/config/schema/simple_message.schema.yml new file mode 100644 index 0000000..7a535e7 --- /dev/null +++ b/config/schema/simple_message.schema.yml @@ -0,0 +1,6 @@ +simple_message.config: + type: config_object + label: Simple Message + mapping: + message: + type: text \ No newline at end of file diff --git a/simple_message.info.yml b/simple_message.info.yml new file mode 100644 index 0000000..fb2d2de --- /dev/null +++ b/simple_message.info.yml @@ -0,0 +1,5 @@ +name: Simple Message +description: Displays a simple message. +core_version_requirement: ^8 || ^9 +type: module +package: Custom diff --git a/simple_message.services.yml b/simple_message.services.yml new file mode 100644 index 0000000..18af427 --- /dev/null +++ b/simple_message.services.yml @@ -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 } diff --git a/src/DisplaySimpleMessage.php b/src/DisplaySimpleMessage.php new file mode 100644 index 0000000..2e460b2 --- /dev/null +++ b/src/DisplaySimpleMessage.php @@ -0,0 +1,50 @@ +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; + } + +}