Skip to content

Commit

Permalink
Merge pull request #213 from dof-dss/development
Browse files Browse the repository at this point in the history
Merge dev to main for release
  • Loading branch information
omahm authored Jun 16, 2022
2 parents 8893e41 + 46cda26 commit 13a15f0
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
show_banner: false
service_url: 'https://www.nidirect.gov.uk/services/operation-shamrock.json'
9 changes: 5 additions & 4 deletions origins_shamrock/js/shamrock_banner.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(function ($, Drupal) {
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.operationShamrock = {
attach: function attach (context) {
$('body', context).once('shamrock').each(function () {
$.getJSON('/services/operation-shamrock.json', function (data) {
$.getJSON(drupalSettings.origins_shamrock.service_url, function (data) {
if (data.enabled) {
$('body').prepend(data.banner);
$('head').append(data.styling);
$('body').prepend(data.banner_html);
}
});
});
}
};
})(jQuery, Drupal);
})(jQuery, Drupal, drupalSettings);

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
title:
body:
url:
styles:
published: false
modified: ''
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ type: module
description: 'Operation Shamrock Admin'
core_version_requirement: ^8.8 || ^9
package: 'Origins'
dependencies:
- origins_shamrock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/**
* Add styles configuration.
*/
function origins_shamrock_admin_update_8001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('origins_shamrock.admin.settings');
$config->set('styles', "<style type='text/css'>.opsh--banner { background: #000; padding: 1.5em 21px;}@media screen and (max-width: 979px) { .opsh--banner { padding: 1.5em 12px; }}.opsh__link { display: block; max-width: 940px; margin: 0 auto;}.opsh__link:link { display: block; max-width: 940px; margin: 0 auto; color: #fff; text-decoration: none;}.opsh__link:visited { color: #bfb1ba;}.opsh__link:hover { text-decoration: none;}.opsh__link:hover .opsh__read-more { background-color: #fff; color: #000; outline: 1px solid #fff; text-decoration: none;}.opsh__link:focus { outline: 3px solid #fff; outline-offset: .5em;}.opsh__title { display: block; font-size: 3.6rem; margin-bottom: .5em; font-weight: bold; line-height: 1.25;}.opsh__msg { display: block; font-size: 1.8rem; line-height: 2.7rem; margin: .6rem 0 0;}.opsh__read-more { display: inline-block; font-size: 1.6rem; line-height: 2.1rem; margin: .6rem 0; text-decoration: underline;}@media screen and (max-width: 599px) { .opsh__title { font-size: 2.8rem; margin-bottom: 1.6rem; line-height: 3.2rem; } .opsh__msg { font-size: 1.6rem; line-height: 2.4rem; } .opsh__read-more { font-size: 1.4rem; line-height: 2.1rem; margin: .3rem 0; }}@media screen and (max-width: 480px) { .opsh__title { font-size: 2.1rem; margin-bottom: 1.6rem; line-height: 2.4rem; }}</style>");
$config->save(TRUE);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
origins_shamrock.administration:
title: 'Shamrock Administration'
title: 'Administration'
route_name: origins.shamrock.admin.admin_form
description: 'Configure Operation Shamrock banner'
parent: origins.admin_config
weight: 10
parent: origins_shamrock.configuration
weight: 2
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function origins_shamrock_admin_theme() {
'title' => NULL,
'body' => NULL,
'url' => NULL,
'styles' => NULL,
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function index() {
$response->setContent(
json_encode([
'enabled' => $config->get('published'),
'banner' => $this->renderer->render($build),
'banner_html' => $this->renderer->render($build),
'styling' => $config->get('styles'),
])
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#size' => 64,
'#weight' => '3',
];
$form['shamrock']['styles'] = [
'#type' => 'textarea',
'#title' => $this->t('CSS styles'),
'#default_value' => $config->get('styles'),
'#weight' => '4',
];

$form['published'] = [
'#type' => 'checkbox',
'#title' => $this->t('Publish the banner.'),
'#default_value' => $config->get('published'),
'#weight' => '4',
'#weight' => '5',
];
$form['submit'] = [
'#type' => 'submit',
Expand All @@ -93,6 +99,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('title', $form_state->getValue('title'))
->set('body', $form_state->getValue('body'))
->set('url', $form_state->getValue('url'))
->set('styles', $form_state->getValue('styles'))
->set('published', (bool) $form_state->getValue('published'))
->set('modified', time())
->save();
Expand Down
11 changes: 11 additions & 0 deletions origins_shamrock/origins_shamrock.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

/**
* Add service_url configuration.
*/
function origins_shamrock_update_8001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('origins_shamrock.settings');
$config->set('service_url', 'https://www.nidirect.gov.uk/services/operation-shamrock.json');
$config->save(TRUE);
}
1 change: 1 addition & 0 deletions origins_shamrock/origins_shamrock.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ shamrock_banner:
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
11 changes: 8 additions & 3 deletions origins_shamrock/origins_shamrock.links.menu.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
origins_shamrock.settings:
title: 'Shamrock Settings'
origins_shamrock.configuration:
title: 'Operation Shamrock'
route_name: origins.shamrock.settings_form
description: 'Configure Operation Shamrock settings'
parent: origins.admin_config
weight: 10
origins_shamrock.settings:
title: 'Settings'
route_name: origins.shamrock.settings_form
description: 'Configure Operation Shamrock settings'
parent: origins_shamrock.configuration
weight: 1
4 changes: 4 additions & 0 deletions origins_shamrock/origins_shamrock.module
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function origins_shamrock_preprocess_page(&$variables) {
$variables['#cache']['tags'][] = 'origins:operation_shamrock';

if (\Drupal::config('origins_shamrock.settings')->get('show_banner') && \Drupal::service('path.matcher')->isFrontPage()) {
$config_factory = \Drupal::configFactory();
$config = $config_factory->get('origins_shamrock.settings');

$variables['#attached']['drupalSettings']['origins_shamrock']['service_url'] = $config->get('service_url');
$variables['#attached']['library'][] = 'origins_shamrock/shamrock_banner';
}
}
57 changes: 56 additions & 1 deletion origins_shamrock/src/Form/ShamrockSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,43 @@
namespace Drupal\origins_shamrock\Form;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides Operation Shamrock settings form.
*/
class ShamrockSettingsForm extends ConfigFormBase {
const SETTINGS = 'origins_shamrock.settings';

/**
* Module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* Constructs a new BlockListSettings object.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('module_handler')
);
}

/**
* {@inheritdoc}
*/
Expand All @@ -33,7 +61,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(static::SETTINGS);

$form['introduction'] = [
'#markup' => 'Operation Shamrock is the NI arrangements for a cabinet office lead directive known as London Bridge. Should this be triggered, this admin page gives a site editor the option as to whether to render the banner on the front page of the site. It is turned by default for some domains',
'#markup' => 'Operation Shamrock is the NI arrangements for a cabinet office lead directive known as London Bridge. Should this be triggered, this admin page gives a site editor the option as to whether to render the banner on the front page of the site. It is turned off by default for some domains',
];

$form['show_the_banner'] = [
Expand All @@ -43,9 +71,20 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => $config->get('show_banner'),
'#weight' => '0',
];

$form['service_url'] = [
'#type' => 'textfield',
'#title' => $this->t('Service domain'),
'#description' => $this->t('Domain to query for the Shamrock data.'),
'#default_value' => $config->get('service_url'),
'#required' => TRUE,
'#weight' => '1',
];

$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#weight' => '10',
];

return $form;
Expand All @@ -57,8 +96,24 @@ public function buildForm(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->getEditable(static::SETTINGS)
->set('show_banner', (bool) $form_state->getValue('show_the_banner'))
->set('service_url', $form_state->getValue('service_url'))
->save();

// If the Content Security Policy module is enabled, add service url.
if ($this->moduleHandler->moduleExists('csp')) {
$csp_config = $this->configFactory->getEditable('csp.settings');
$report_only = $csp_config->get('report-only');
$service_url = $form_state->getValue('service_url');
$service_domain = substr($service_url, 0, strpos($service_url, '/', 8));

if (!in_array($service_domain, $report_only['directives']['script-src']['sources'])) {
$report_only['directives']['script-src']['sources'][] = $service_domain;
$csp_config->set('report-only', $report_only);
$csp_config->save();
$this->messenger()->addMessage($this->t('Added %domain to Content Security Policy', ['%domain' => $service_domain]));
}
}

Cache::invalidateTags(['origins:operation_shamrock']);
parent::submitForm($form, $form_state);
}
Expand Down
2 changes: 1 addition & 1 deletion origins_toc/origins_toc.module
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function origins_toc_node_view(&$build, EntityInterface $entity, EntityViewDispl
$flags = $flag_service->getEntityFlagCounts($entity);

// Check if the entity has the disable_toc flag set.
if (array_key_exists('disable_toc', $flags) && $flags['disable_toc'] === 1) {
if (array_key_exists('disable_toc', $flags) && $flags['disable_toc'] == 1) {
return $build;
}

Expand Down

0 comments on commit 13a15f0

Please sign in to comment.