-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfits.module
196 lines (167 loc) · 5.93 KB
/
fits.module
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* @file
* Contains fits.module.
*/
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function fits_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the islandora_lite_fits module.
case 'help.page.islandora_lite_fits':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module represent DSU''s approach toward Fits in our Islandora Lite Stack') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function fits_theme() {
return [
'islandora_lite_fits' => [
'render element' => 'children',
],
];
}
/**
* Implement fits_file_insert().
*/
function fits_file_insert(EntityInterface $entity) {
// Only extract Fits for File level only.
if ('File' === $entity->getEntityType()->getLabel()->getUntranslatedString() && 1 === \Drupal::config('fits.fitsconfig')->get('fits-extract-ingesting')) {
execute_fits_action($entity);
}
}
/**
* Shared function call for execute Fits Action.
*/
function execute_fits_action(EntityInterface $entity) {
// Fix warning when Config form hasn't been setup.
$config = \Drupal::config('fits.fitsconfig');
if (!isset($config) || empty($config->get('fits-advancedqueue_id'))) {
return;
}
$file = File::load($entity->id());
$utils = \Drupal::service('fits.context_utils');
$utils->executeFileReactions('\Drupal\fits\Plugin\ContextReaction\FitsReaction', $file);
}
/**
* Implement fits_form_alter().
*/
function fits_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (start_with($form_id, 'file_') && end_with($form_id, '_edit_form')) {
$form['fits'] = [
'#type' => 'details',
'#title' => 'Fits',
'#open' => TRUE,
];
global $base_url;
$form['fits']['description'] = [
'#markup' => new FormattableMarkup("<p>The following fields are generated by Fits tool. To update them, run the action <i>FITS - Generate and Extract Technical metadata for File</i> at <a href=\"$base_url/admin/content/files\">$base_url/admin/content/files</a></p>", []),
];
foreach (array_keys($form) as $field) {
if (FALSE !== strpos($field, '_fits') || FALSE !== strpos($field, '_fits_')) {
$form['fits'][$field] = $form[$field];
if ('field_fits_pronom_puid' === $field) {
$form['fits'][$field]['widget'][0]['target_id']['#attributes']['disabled'] = TRUE;
}
else {
$form['fits'][$field]['widget'][0]['value']['#attributes']['readonly'] = 'readonly';
}
unset($form[$field]);
}
}
}
}
/**
* Check if string start with sub-string.
*/
function start_with($haystack, $needle): bool {
$length = strlen($needle);
return substr($haystack, 0, $length) === $needle;
}
/**
* Check if string end with sub-string https://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php.
*/
function end_with($haystack, $needle): bool {
$length = strlen($needle);
if (!$length) {
return TRUE;
}
return substr($haystack, -$length) === $needle;
}
/**
* Modify the Help text field to repurpose its use to contain Jmespath.
*/
function fits_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Get all available file types.
$fileTypes = \Drupal::entityTypeManager()
->getStorage('file_type')
->loadMultiple();
$fileTypes = array_keys($fileTypes);
// Get a need key component to detect fits related fields.
$parts = explode('/', \Drupal::service('path.current')->getPath());
$currentFileEntity = $parts[5];
$field = $parts[count($parts) - 1];
// Limit JmesPath field modification for Fits related.
if (in_array($currentFileEntity, $fileTypes) && FALSE !== strpos($field, '_fits_')) {
$form['description']['#description'] = $form['description']['#description'] . t("
<p><u></i><strong>Important Note:</strong></u> With the Fits module enabled, the Helper text can evaluate JMespath(s) against Fits JSON field to extract technical meatadata.
<br /><strong>Mandatory convention:</strong> <code>[{JmesPath}]</code>. For example. <code>[{fileinfo.md5checksum}]</code>. For multiple, enter them each line.
<br />For further details about JMESPath, please visit: <a href='https://jmespath.org/tutorial.html' target='_blank'>https://jmespath.org/tutorial.html</a> </p>");
}
}
/**
* Leave a note for use case.
*/
function fits_form_field_ui_field_storage_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Get all available file types.
$fileTypes = \Drupal::entityTypeManager()
->getStorage('file_type')
->loadMultiple();
$fileTypes = array_keys($fileTypes);
// Get a need key component to detect fits related fields.
$parts = explode('/', \Drupal::service('path.current')->getPath());
$currentFileEntity = $parts[5];
if (in_array($currentFileEntity, $fileTypes)) {
$form['note-for-fits'] = [
'#markup' => t('<strong>Important Note</strong>: If you are adding textfield(s) for Fits extraction with JMESPath, please make sure to have
<code>_fits_</code> in the field\'s machine name (ie. <cdoe>field_fits_file_size</cdoe>)'),
];
}
}
if (!function_exists('print_log')) {
/**
* Debug function: display any variable to error log.
*/
function print_log($thing) {
error_log(print_r($thing, TRUE), 0);
}
}
if (!function_exists('logging')) {
/**
* Debug function: display any variable to current webpage.
*/
function logging($thing) {
echo '<pre>';
print_r($thing);
echo '</pre>';
}
}
if (!function_exists('drupal_log')) {
/**
* Debug function: display any variable to drupal Reports Log messages.
*/
function drupal_log($msg) {
\Drupal::logger(basename(__FILE__, '.module'))->error($msg);
}
}