Skip to content

Commit

Permalink
Work on #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Nov 13, 2019
1 parent 0eec6d9 commit 227797b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This module provides no mechanism for uploading configuration files via Drupal's

### Using Context to add or modify Islandora Bagger config settings

The other of Reaction allows you to use add or modify options to the Islandora Bagger configuration file. To enable this, do the folowing:
The other Reaction allows you to add options to the Islandora Bagger configuration file or override existing options. To enable this, do the folowing:

1. Install Context and Context UI modules (requirements for Islandora, so will already be done).
1. Create a Context or edit an existing Context.
Expand All @@ -72,9 +72,13 @@ The other of Reaction allows you to use add or modify options to the Islandora B

```
serialize: tgz
output_dir: /my/alternative/output/path
bag-info: Contact-Email: [email protected] | Custom-Tag: Some value.
plugins: AddBasicTags | AddMedia | AddFedoraTurtle
```
If the option's key exists in the file, that option will be updated with the new value. If the option's key doesn't exist in the file, it will be added.

`bag-info`,`drupal_basic_auth`,`drupal_media_tags`, `plugins`, and `post_bag_scripts` are pipe-separated lists. For `bag-info`, each member of the list is a tag:value pair (separated by a colon). The other list options takes a pipe-separated list of values.

If the option's key exists in the configuration file, that option will be updated with the new value. If the option's key doesn't exist in the configuration file, it will be added. The only exception is `bag-info`: for this option, its values provided in the Context Reaction will be merged with any existing values from the configuration file.

## Modifying the Islandora Bagger configuration from other modules

Expand Down
39 changes: 31 additions & 8 deletions islandora_bagger_integration.module
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,50 @@
use Symfony\Component\Yaml\Yaml;

/**
* Implements hook_islandora_bagger_config_file_contents_alter().
* 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 = $settings = Yaml::parse($config_file_contents);
$options_from_context = Yaml::parse($options);
// If key exists in cofig file, update value; if not, add it as an option.
$config = Yaml::parse($config_file_contents);
foreach ($options_from_context as $key => $value) {
if (in_array(trim($key), $incoming_config)) {
$config[trim($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;
}
else {
$config[trim($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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form['bagger_config_options'] = [
'#title' => $this->t('Islandora Bagger config options'),
'#type' => 'textarea',
'#description' => $this->t('Key: value pairs of options to add/modify. One pair per line. Must be valid YAML.'),
'#description' => $this->t("Key:value pairs of options to add/modify. One pair per line. See module's README file for examples."),
'#default_value' => isset($config['bagger_config_options']) ? $config['bagger_config_options'] : '',
];
return $form;
Expand Down
54 changes: 54 additions & 0 deletions src/Utils/IslandoraBaggerUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,59 @@ public function getIslandoraBaggerConfig($path) {
return $settings;
}


/**
* Incorporates bag-info tags from the Context "IslandoraBaggerConfigurationOptionsReaction" Reaction into the existing YAML config data.
*
* New tags are added, and existing tags are overwritten with new values.
*
* @param array $existing_config
* The YAML from the config file template.
* @param string $bag_info_tags_from_context
* The pipe-separated bag-info tags from the Context configuration.
*
* @return array
* The modified YAML configuration data as an associative array.
*/
public function addBagInfoTags($existing_config, $bag_info_tags_from_context) {
$bag_info_tags_from_context = explode('|', $bag_info_tags_from_context);
foreach ($bag_info_tags_from_context as $tag_from_context) {
list($context_tag_key, $context_tag_value) = explode(':', $tag_from_context, 2);
$context_tag_key = trim($context_tag_key);
$context_tag_value = trim($context_tag_value);
// If the tag exists, replace it with the corresponding value from the Context;
// if it doesn't, add it using the value from the Context.
$existing_config['bag-info'][$context_tag_key] = $context_tag_value;
}

return $existing_config;
}

/**
* Incorporates list values from the Context "IslandoraBaggerConfigurationOptionsReaction" Reaction into the existing YAML config data.
*
* New tags are added, and existing tags are overwritten with new values.
*
* @param array $existing_config
* The YAML from the config file template.
* @param string $key
* The YAML key to update.
* @param string $list_from_context
* The pip-separate list of values from the Context configuration.
*
* @return array
* The modified YAML configuration data as an associative array.
*/
public function addListConfigOptions($existing_config, $key, $list_from_context) {
$list_from_context = explode('|', $list_from_context);
foreach ($list_from_context as &$member) {
$member = trim($member);
// If the key exists, replace it with the corresponding list value from the Context;
// if it doesn't, add it using the list value from the Context.
$existing_config[$key] = $list_from_context;
}

return $existing_config;
}
}

0 comments on commit 227797b

Please sign in to comment.