Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions modules/contrib/govcms_dlm/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GovCMS DLM
-------------------

This module adds the option for a user to set a Dissemination Limiting Marker
(DLM) appended to the end of the subject for all outgoing emails sent using
drupal_mail() on your site.


Requirements
------------

* Drupal 10 OR 11


Installation and configuration
------------------------------

1. Install GovCMS DLM module as you install a contributed Drupal module.
See https://www.drupal.org/docs/extending-drupal/installing-modules

2. Go to /admin/config/system/dlm to configure the module.

Your site will now append the selected DLM to the end of all email subjects sent
using drupal_mail(). Please note that any module that sends emails not using
drupal_mail will not append the DLM to the email subject.


Further reading
---------------

* https://govcms.gov.au
* https://drupal.org/project/govcms
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
suffix: '[SEC=UNOFFICIAL]'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Schema for the configuration files of the govcms_dlm module.

govcms_dlm.settings:
type: config_object
label: 'govCMS DLM Email settings'
mapping:
suffix:
type: string
label: 'DLM marking to apply to e-mails'
7 changes: 7 additions & 0 deletions modules/contrib/govcms_dlm/govcms_dlm.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'GovCMS Dissemination Limiting Marker (DLM)'
type: module
description: Add optional DLM to emails sent from drupal mail.
package: GovCMS
core_version_requirement: ^10 || ^11
version: 3.0.1
configure: 'govcms_dlm.form'
16 changes: 16 additions & 0 deletions modules/contrib/govcms_dlm/govcms_dlm.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @file
* Contains install and update functions for govcms_dlm module.
*/

/**
* Reset the configuration for updating the new DLM tags.
*/
function govcms_dlm_8100() {
// Resave default dlm.
\Drupal::configFactory()->getEditable('govcms_dlm.settings')
->set('suffix', '[SEC=UNOFFICIAL]')
->save();
}
5 changes: 5 additions & 0 deletions modules/contrib/govcms_dlm/govcms_dlm.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
govcms_dlm.form:
route_name: govcms_dlm.form
title: 'Email DLM settings'
description: 'Set your email markings here for site wide emails'
parent: system.admin_config_system
134 changes: 134 additions & 0 deletions modules/contrib/govcms_dlm/govcms_dlm.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Entity\Webform;

/**
* @file
* Main functions of the govcms_dlm module.
*/

/**
* Add DLM field to Webform settings.
*
*What it does:
*This hook modifies the Webform settings form.
*It adds a new DLM Classification dropdown field to each webform’s settings page.
*The selected DLM value is stored using third-party settings, specific to each webform.
*Why it's needed:
*So content editors or site admins can choose a different DLM per webform from the UI.
*/
function govcms_dlm_form_webform_settings_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$webform = $form_state->getFormObject()->getEntity();

$form['govcms_dlm'] = [
'#type' => 'details',
'#title' => t('DLM Settings'),
'#open' => TRUE,
];

$form['govcms_dlm']['dlm_classification'] = [
'#type' => 'select',
'#title' => t('DLM Classification'),
'#options' => [
'' => t('- Default (site-wide) -'),
'[SEC=UNOFFICIAL]' => '[SEC=UNOFFICIAL]',
'[SEC=SENSITIVE]' => '[SEC=SENSITIVE]',
'[SEC=PROTECTED]' => '[SEC=PROTECTED]',
],
'#default_value' => $webform->getThirdPartySetting('govcms_dlm', 'dlm_classification', ''),
'#description' => t('Overrides the global DLM for this webform.'),
];
}

/**
* Save per-webform DLM setting.
*
*What it does:
*Before a webform is saved, this hook checks for the selected DLM value from the settings form.
*It then stores the selected DLM value in that webform’s third-party settings (this avoids needing custom config schema).
*Why it's needed:
*Ensures the per-webform DLM selection is persisted to the database.
*/
function govcms_dlm_webform_presave(Webform $webform) {
$dlm = \Drupal::request()->request->get('dlm_classification');
if ($dlm !== NULL) {
$webform->setThirdPartySetting('govcms_dlm', 'dlm_classification', $dlm);
}
}

/**
* Implements hook_help().
*/
function govcms_dlm_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.govcms_dlm':
$readme_file = dirname(__FILE__) . '/README.txt';

if (!file_exists($readme_file)) {
return FALSE;
}

$readme = file_get_contents($readme_file);

$element = [
'#type' => 'html_tag',
'#tag' => 'pre',
'#value' => $readme,
];

// Markdown is optionally supported if you already have it installed.
if (\Drupal::moduleHandler()->moduleExists('markdown')) {
$filters = \Drupal::moduleHandler()->invoke('markdown', 'filter_info');
$info = $filters['filter_markdown'];

if (function_exists($info['process callback'])) {
$function = $info['process callback'];
$element = ['#markup' => \Drupal\Component\Utility\Xss::filterAdmin($function($readme, NULL))];
}
}

return \Drupal::service('renderer')->render($element);
}
}

/**
* Implements hook_mail_alter().
*
* Adds the config variable govcms_current_dlm to the end of emails sent using
* drupal_mail
*
*
*What it does:
*This runs when Drupal sends an email.
*If the email was triggered by a Webform submission, it looks up the related webform.
*It then:
*Checks if the webform has a custom DLM setting.
*Falls back to the default site-wide DLM if none is set.
*Prepends the DLM string to the email subject.
*Why it's needed:
*So emails sent from different webforms carry their appropriate classification markers, improving DLM compliance on a per-form basis.
*/
function govcms_dlm_mail_alter(&$message) {
$message['subject'] = t('@subject @dlm', [
'@subject' => $message['subject'],
'@dlm' => \Drupal::config('govcms_dlm.settings')->get('suffix'),
]);

if (isset($message['params']['webform_submission']) && $message['params']['webform_submission'] instanceof \Drupal\webform\WebformSubmissionInterface) {
$submission = $message['params']['webform_submission'];
$webform = $submission->getWebform();

// Check for per-webform DLM
$dlm = $webform->getThirdPartySetting('govcms_dlm', 'dlm_classification', '');

// Fallback to default
if (empty($dlm)) {
$dlm = \Drupal::config('govcms_dlm.settings')->get('default_dlm');
}

if (!empty($dlm)) {
$message['subject'] = $dlm . ' ' . $message['subject'];
}
}

}
2 changes: 2 additions & 0 deletions modules/contrib/govcms_dlm/govcms_dlm.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
administer govcms_dlm:
title: 'Administer DLM settings'
7 changes: 7 additions & 0 deletions modules/contrib/govcms_dlm/govcms_dlm.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
govcms_dlm.form:
path: /admin/config/system/dlm
defaults:
_title: 'Email DLM settings'
_form: \Drupal\govcms_dlm\Form\GovcmsDlmForm
requirements:
_permission: 'administer govcms_dlm'
61 changes: 61 additions & 0 deletions modules/contrib/govcms_dlm/src/Form/GovcmsDlmForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\govcms_dlm\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

/**
* Class GovcmsDlmForm.
*
* @package Drupal\govcms_dlm\Form
*/
class GovcmsDlmForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'govcms_dlm_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form['suffix'] = [
'#type' => 'select',
'#title' => $this->t('Select DLM to append to all outgoing e-mails'),
'#default_value' => \Drupal::config('govcms_dlm.settings')->get('suffix'),
'#options' => [
'[SEC=UNOFFICIAL]' => '[SEC=UNOFFICIAL]',
'[SEC=OFFICIAL]' => '[SEC=OFFICIAL]',
'[SEC=OFFICIAL:Sensitive]' => '[SEC=OFFICIAL:Sensitive]',
],
'#description' => $this->t("Note: Just because you set this doesn't ensure the email is sent securely, you must also ensure your email gateway is configured appropriately."),
'#required' => TRUE,
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('govcms_dlm.settings')
->set('suffix', $form_state->getValue('suffix'))
->save();

parent::submitForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['govcms_dlm.settings'];
}

}