forked from mjordan/islandora_bagger_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_bagger_integration.module
53 lines (46 loc) · 2.11 KB
/
islandora_bagger_integration.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
<?php
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_islandora_bagger_config_file_contents_alter().
*/
function islandora_bagger_integration_islandora_bagger_config_file_contents_alter($nid, &$config_file_contents) {
if (\Drupal::moduleHandler()->moduleExists('context')) {
$utils = \Drupal::service('islandora_bagger_integration.utils');
$context_manager = \Drupal::service('context.manager');
foreach ($context_manager->getActiveReactions('islandora_bagger_integration_config_options') as $reaction) {
// Last one wins.
$options = $reaction->execute();
}
$options_array = preg_split("/\\r\\n|\\r|\\n/", $options);
$options_from_context = array();
foreach ($options_array as $option) {
list($option_key, $option_value) = explode(':', $option, 2);
$options_from_context[trim($option_key)] = trim($option_value);
}
$config = Yaml::parse($config_file_contents);
foreach ($options_from_context as $key => $value) {
// Skip options that have lists as values, process them below.
$skip_keys = array('bag-info', 'drupal_basic_auth', 'drupal_media_tags', 'plugins', 'post_bag_scripts');
if (!in_array($key, $skip_keys)) {
// If key exists in config file, update value; if not, add it as an option.
$config[$key] = $value;
}
}
// Handle 'bag-info' separately since its value is a list of key:value pairs.
if (array_key_exists('bag-info', $config) && array_key_exists('bag-info', $options_from_context)
&& strlen($options_from_context['bag-info'])) {
$config = $utils->addBagInfoTags($config, $options_from_context['bag-info']);
}
// Handle 'plugins' separately since its value is a list of strings.
foreach ($skip_keys as $key) {
if ($key != 'bag-info') {
if (array_key_exists($key, $config) && array_key_exists($key, $options_from_context)
&& strlen($options_from_context[$key])) {
$config = $utils->addListConfigOptions($config, $key, $options_from_context[$key]);
}
}
}
// Reserialize the modified YAML into $config_file_contents.
$config_file_contents = Yaml::dump($config);
}
}