-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatom.module
More file actions
173 lines (147 loc) · 4.05 KB
/
atom.module
File metadata and controls
173 lines (147 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* @file
* Contains atom.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
function atom_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'atom/atom';
}
/**
* Implements hook_help().
*/
function atom_help($route_name, RouteMatchInterface $route_match)
{
switch ($route_name) {
// Main module help for the atom module.
case 'help.page.atom':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module integrates AtoM REST API and feed to drupal nodes Holdings') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function atom_theme()
{
return [
'atom' => [
'render element' => 'children',
],
];
}
/**
* Implement hook_cron
*/
function atom_cron()
{
startDownloadAtomHoldings();
}
/**
* Trigger download Holdings process
*/
function startDownloadAtomHoldings() {
$config = \Drupal::config('atom.atomapiconfig');
$service = \Drupal::service('atom.download');
$repoIds = explode(',', $config->get('atom-repoid'));
$result = [];
foreach ($repoIds as $repoId) {
$r = $service->get($repoId, null);
$result = array_merge($result, $r);
}
$nids = $service->atomHoldingToNode($result);
$service->deleteStaleHoldings($nids);
\Drupal::logger('atom')->notice(
'AtoM sync complete. Found @total holdings, created @created, updated @updated, deleted @deleted.',
[
'@total' => count($nids),
'@created' => $service->created,
'@updated' => $service->updated,
'@deleted' => $service->deleted
]
);
}
/**
* Generate Path Alias for Holding node only
* @param $node
*/
function generateHoldingAlias($node)
{
$tag = "/holdings/" . createSlug($node->title->value);
if (!\Drupal::service('path_alias.repository')->lookupByAlias($tag, 'en')) {
//$path = \Drupal::service('path_alias.repository')->save("/node/" . $node->id(), $tag, "en");
$path_alias = \Drupal::entityTypeManager()->getStorage('path_alias')->create([
'path' => "/node/" . $node->id(),
'langcode' => "en",
]);
$path_alias->save();
}
}
/**
* Implements hook_insert().
*/
function atom_entity_insert(\Drupal\Core\Entity\EntityInterface $node)
{
// Set the URL alias
//if (get_class($node) == 'Drupal\node\Entity\Node') {
if ($node->getEntityType()->id() == 'node' && in_array($node->getType(), ['holding'])) {
generateHoldingAlias($node);
// trigger email sending action.
$action = \Drupal::entityTypeManager()
->getStorage('action')
->load('send_email');
if ($action) {
$action->execute([$node]);
}
}
}
function atom_entity_update(\Drupal\Core\Entity\EntityInterface $node)
{
// Set the URL alias
//if (get_class($node) == 'Drupal\node\Entity\Node') {
if ($node->getEntityType()->id() == 'node' && in_array($node->getType(), ['holding'])) {
generateHoldingAlias($node);
}
}
/**
* Debug function: display any variable to error log
*
* @param $thing
*/
if (!function_exists('logging')) {
function print_log($thing)
{
error_log(print_r($thing, true), 0);
}
}
/**
* Debug function: display any variable to current webpage
* @param $thing
*/
if (!function_exists('logging')) {
function logging($thing)
{
echo "<pre>";
print_r($thing);
echo "</pre>";
}
}
function atom_update_9020() {
$schema = \Drupal\Core\Database\Database::getConnection()->schema();
$spec = ['type' => 'text', 'size' => 'big', 'not null' => TRUE];
$tables = [
'node__field_conditions_governing_acces',
'node_revision__field_conditions_governing_acces'
];
foreach ($tables as $table) {
$schema->changeField(
$table,
'field_conditions_governing_acces_value',
'field_conditions_governing_acces_value',
$spec
);
}
}