Skip to content

Commit 7fed2df

Browse files
committed
initial commit
1 parent 7b3f584 commit 7fed2df

File tree

162 files changed

+5453
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+5453
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Ignore potentially dangerous files.
2+
*.sql*
3+
*.tar*
4+
*.*zip
5+
6+
# Misc IDE's and tooling files to ingore.
7+
.swp
8+
.vscode
9+
/.idea/
10+
.DS_Store
11+
.talismanrc
12+
node_modules

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[![CircleCI](https://circleci.com/gh/dof-dss/nicsdru_origins_modules.svg?style=svg)](https://circleci.com/gh/dof-dss/nicsdru_origins_modules)
2+
3+
# DOF-DSS Unity Modules
4+
5+
A collection of modules for use across all Unity Drupal sites
6+
7+
## Usage
8+
9+
You can include this package into your project using composer:
10+
```
11+
composer require dof-dss/nicsdru_unity_modules
12+
```
13+
Details can be found at: https://packagist.org/packages/dof-dss/nicsdru_unity_modules

package-lock.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unity_common/drush.services.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
unity_common.commands:
3+
class: \Drupal\unity_common\Commands\UnityDrushCommands
4+
tags:
5+
- { name: drush.command }
6+

unity_common/js/link-manager.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @file
3+
* Defines Javascript behaviors for Link handling.
4+
*/
5+
(function ($) {
6+
7+
"use strict";
8+
9+
/**
10+
* Changes any page links, within certain defined regions, from
11+
* <a> elements to <span> elements if the current path matches the
12+
* 'href' attribute.
13+
*
14+
* header.header
15+
* #menu-main
16+
* #main-content
17+
* #sidebar-first
18+
* #sidebar-second
19+
* #footer
20+
*/
21+
Drupal.behaviors.rewriteSelfReferencingLinks = {
22+
attach: function (context, settings) {
23+
let pathname = $(location).attr('pathname');
24+
// Trim query parameters, if any, from pathname.
25+
pathname = pathname.substr(0, (pathname.indexOf('?') < 0) ? pathname.length : pathname.indexOf('?'));
26+
27+
const elements = ['header.header', '#menu-main', '#main-content', '#footer', '#sidebar-second', '#sidebar-first'];
28+
29+
$.each(elements, function (index, elementRef) {
30+
let classes = ['active', 'link__self'];
31+
32+
if (elementRef === '#block-mainnavigation') {
33+
classes.push('nav-link');
34+
}
35+
36+
$(elementRef + ' a[href*="' + pathname + '"]').each(function () {
37+
// Move to next item if we have a data attribute to indicate we should ignore this element.
38+
if ($(this).attr('data-self-ref')) {
39+
return true;
40+
}
41+
42+
let href = $(this).attr('href');
43+
// Prune away any query parameters, if they exist.
44+
href = href.substr(0, (href.indexOf('?') < 0) ? href.length : href.indexOf('?'));
45+
46+
// Need a regex object to inject the pathname variable as a matching value.
47+
let regex = new RegExp('^' + pathname + '$'.replace(/\//g, '\\/'), 'g');
48+
49+
if (href.match(regex)) {
50+
$(this).replaceWith('<span class="' + classes.join(' ') + '">' + $(this).text() + '</span>');
51+
}
52+
});
53+
});
54+
}
55+
};
56+
57+
}(jQuery, Drupal));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
namespace Drupal\unity_common\Commands;
4+
5+
use Drush\Commands\DrushCommands;
6+
use Drupal\structure_sync\StructureSyncHelper;
7+
8+
/**
9+
* Drush custom commands.
10+
*/
11+
class UnityDrushCommands extends DrushCommands {
12+
13+
/**
14+
* Core EntityTypeManager instance.
15+
*
16+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
17+
*/
18+
protected $entityTypeManager;
19+
20+
/**
21+
* Class constructor.
22+
*/
23+
public function __construct() {
24+
parent::__construct();
25+
$this->entityTypeManager = \Drupal::entityTypeManager();
26+
}
27+
28+
/**
29+
* Drush command import blocks, taxonomies and menus using structure_sync.
30+
*
31+
* @param string $option
32+
* Argument to select 'safe', 'full' or 'force'.
33+
*
34+
* @command import-all-if-installed
35+
*/
36+
public function importAllIfInstalled($option = 'safe') {
37+
// Only import if the structure_sync module is installed.
38+
if (\Drupal::moduleHandler()->moduleExists('structure_sync')) {
39+
$config = \Drupal::config('structure_sync.data');
40+
// Import blocks if necessary.
41+
$blocks = $config->get('blocks');
42+
if (!empty($blocks)) {
43+
StructureSyncHelper::importCustomBlocks([
44+
'style' => $option,
45+
'drush' => TRUE,
46+
]);
47+
}
48+
// Import taxonomies if necessary.
49+
$taxonomies = $config->get('taxonomies');
50+
if (!empty($taxonomies)) {
51+
StructureSyncHelper::importTaxonomies([
52+
'style' => $option,
53+
'drush' => TRUE,
54+
]);
55+
}
56+
// Import menus if necessary.
57+
$menus = $config->get('menus');
58+
if (!empty($menus)) {
59+
StructureSyncHelper::importMenuLinks([
60+
'style' => $option,
61+
'drush' => TRUE,
62+
]);
63+
}
64+
}
65+
}
66+
67+
/**
68+
* Drush command replace SITE_FOLDER_NAME and STARTERKIT text in a unity site
69+
* sub-theme.
70+
*
71+
* @param string $site_folder_name
72+
* Argument for user to enter the site directory they wish to target.
73+
* @param string $starterkit
74+
* Argument for user to add the new name of the sub-theme, replacing
75+
* STARTERKIT.
76+
*
77+
* @command update-starterkit-text
78+
* @aliases upst
79+
* @usage update-starterkit-text --site_folder_name --new_theme_name
80+
*/
81+
public function changeText($site_folder_name = '', $starterkit = '') {
82+
// Get the site folder name entered as the 1st parameter by the user.
83+
$site_path = getcwd() . '/sites/' . $site_folder_name;
84+
85+
// Rename the STARTERKIT directory to the new sub-theme name entered by the user as the 2nd parameter.
86+
rename($site_path . '/themes/STARTERKIT', $site_path . '/themes/' . $starterkit);
87+
$new_theme_path = $site_path . '/themes/' . $starterkit;
88+
89+
// Change the theme file names.
90+
$theme_file_names = glob($new_theme_path . '/*');
91+
foreach ($theme_file_names as $theme_file_name) {
92+
$new_file_name = str_replace('STARTERKIT', $starterkit, $theme_file_name);
93+
rename($theme_file_name, $new_file_name);
94+
}
95+
96+
// Loop through the new theme directories and update any STARTERKIT and SITE_DIRECTORY_TEXT text.
97+
$site_directories = [
98+
glob($new_theme_path . '/config/*'),
99+
glob($new_theme_path . '/*'),
100+
];
101+
foreach ($site_directories as $site_directory) {
102+
foreach ($site_directory as $file) {
103+
$file_contents = file_get_contents($file);
104+
$strings = ['STARTERKIT',
105+
'SITE_DIRECTORY_NAME',
106+
];
107+
$string_replacements = [$starterkit,
108+
$site_folder_name,
109+
];
110+
$file_contents = str_replace($strings, $string_replacements, $file_contents);
111+
if (!is_dir($file)) {
112+
file_put_contents($file, $file_contents);
113+
}
114+
}
115+
}
116+
}
117+
118+
/**
119+
* Remove select content from the site.
120+
*
121+
* @command unity-migrate:content-purge
122+
*
123+
* @param string $content_type
124+
* Argument to select content type to purge (machine name).
125+
*
126+
* @aliases mig-purge
127+
* @usage mig-purge <machine name of content type>
128+
*/
129+
public function contentPurge($content_type = NULL) {
130+
if (empty($content_type)) {
131+
$this->io()->write("Please specify the machine name of a content type to purge", TRUE);
132+
return;
133+
}
134+
// Load all nodes of this content type.
135+
$storage = $this->entityTypeManager->getStorage('node');
136+
$entities = $storage->loadByProperties(["type" => $content_type]);
137+
138+
$rows[] = [
139+
'entity' => 'node',
140+
'bundle' => $content_type,
141+
'total' => count($entities),
142+
];
143+
// Show the user a count of nodes.
144+
$this->io()->table(['Entity', 'Bundle', 'Total'], $rows);
145+
146+
// Ask user to confirm.
147+
if ($this->io()->confirm("Are you sure you want to delete all $content_type content", TRUE)) {
148+
$storage->delete($entities);
149+
$this->io()->write("<comment>$bundle content deleted</comment>", TRUE);
150+
}
151+
}
152+
153+
/**
154+
* Drush command to disable Fastly logging if Fastly module installed.
155+
*
156+
* @command disable-fastly-logging
157+
*/
158+
public function disableFastlyLogging() {
159+
// Only disable logging if the Fastly module is installed.
160+
if (\Drupal::moduleHandler()->moduleExists('fastly')) {
161+
\Drupal::configFactory()->getEditable('fastly.settings')
162+
->set('logging', FALSE)->save();
163+
$this->io()->write("Fastly logging disabled", TRUE);
164+
}
165+
else {
166+
$this->io()->write("Fastly module not installed", TRUE);
167+
}
168+
}
169+
170+
/**
171+
* Drush command to enable Fastly logging if Fastly module installed.
172+
*
173+
* @command enable-fastly-logging
174+
*/
175+
public function enableFastlyLogging() {
176+
// Only enable logging if the Fastly module is installed.
177+
if (\Drupal::moduleHandler()->moduleExists('fastly')) {
178+
\Drupal::configFactory()->getEditable('fastly.settings')
179+
->set('logging', TRUE)->save();
180+
$this->io()->write("Fastly logging enabled", TRUE);
181+
}
182+
else {
183+
$this->io()->write("Fastly module not installed", TRUE);
184+
}
185+
}
186+
187+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Drupal\unity_common\EventSubscriber;
4+
5+
use Drupal\Core\Config\ConfigCollectionInfo;
6+
use Drupal\Core\Config\ConfigCrudEvent;
7+
use Drupal\Core\Config\ConfigEvents;
8+
use Drupal\Core\Config\ConfigImporterEvent;
9+
use Drupal\Core\Config\Importer\MissingContentEvent;
10+
use Drupal\Core\Site\Settings;
11+
use Drupal\unity_common\UpdateConfigFromEnvironment;
12+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13+
14+
/**
15+
* A subscriber that overwrites Google Maps api keys on config import.
16+
*/
17+
class UpdateConfigAfterImport implements EventSubscriberInterface {
18+
19+
/**
20+
* The upadte config from env service.
21+
*
22+
* @var Drupal\unity_common\UpdateConfigFromEnvironment
23+
*/
24+
protected $updateEnvService;
25+
26+
/**
27+
* Constructs a new UpdateConfigAfterImport instance.
28+
*
29+
* @param Drupal\unity_common\UpdateConfigFromEnvironment $update_service
30+
* The entity type manager.
31+
*/
32+
public function __construct(UpdateConfigFromEnvironment $update_service) {
33+
$this->updateEnvService = $update_service;
34+
}
35+
36+
/**
37+
* Registers the methods in this class that should be listeners.
38+
*
39+
* @return array
40+
* An array of event listener definitions.
41+
*/
42+
public static function getSubscribedEvents() {
43+
return [
44+
ConfigEvents::IMPORT => ['onConfigImport']
45+
];
46+
}
47+
48+
/**
49+
* Overwrite various settings when config is imported.
50+
*
51+
* @param \Drupal\Core\Config\ConfigImporterEvent $event
52+
* The Event to process.
53+
*/
54+
public function onConfigImport(ConfigImporterEvent $event) {
55+
$change_list = $event->getChangelist();
56+
if (!empty($change_list)) {
57+
if ((isset($change_list['update']) && ($change_list['update'][0] == 'google_analytics.settings')) ||
58+
(isset($change_list['create']) && ($change_list['create'][0] == 'google_analytics.settings'))) {
59+
if (Settings::get('subsite_id')) {
60+
$this->updateEnvService->updateApiKey('google_analytics.settings', 'account', Settings::get('subsite_id') . '_GOOGLE_ANALYTICS_KEY');
61+
}
62+
}
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)