From 227797b8f9cb24fa1ca6b53ae3fcceb240e78afa Mon Sep 17 00:00:00 2001 From: Mark Jordan Date: Wed, 13 Nov 2019 03:38:23 +0000 Subject: [PATCH] Work on #7. --- README.md | 10 ++-- islandora_bagger_integration.module | 39 +++++++++++--- ...doraBaggerConfigurationOptionsReaction.php | 2 +- src/Utils/IslandoraBaggerUtils.php | 54 +++++++++++++++++++ 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 52da8d8..1ae5c85 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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: admin@example.com | 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 diff --git a/islandora_bagger_integration.module b/islandora_bagger_integration.module index c9cf0b3..335416f 100644 --- a/islandora_bagger_integration.module +++ b/islandora_bagger_integration.module @@ -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); } diff --git a/src/Plugin/ContextReaction/IslandoraBaggerConfigurationOptionsReaction.php b/src/Plugin/ContextReaction/IslandoraBaggerConfigurationOptionsReaction.php index 9fb5130..8678fa0 100644 --- a/src/Plugin/ContextReaction/IslandoraBaggerConfigurationOptionsReaction.php +++ b/src/Plugin/ContextReaction/IslandoraBaggerConfigurationOptionsReaction.php @@ -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; diff --git a/src/Utils/IslandoraBaggerUtils.php b/src/Utils/IslandoraBaggerUtils.php index c526c36..db72b39 100644 --- a/src/Utils/IslandoraBaggerUtils.php +++ b/src/Utils/IslandoraBaggerUtils.php @@ -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; + } }