Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit altering the $twig_input #115

Open
patdunlavey opened this issue May 4, 2020 · 2 comments
Open

Permit altering the $twig_input #115

patdunlavey opened this issue May 4, 2020 · 2 comments

Comments

@patdunlavey
Copy link

Our use-case is needing to use the incoming object's PID as part of a MODS identifier string. The PID is not known in advance, but it is available in IslandoraMultiBatch::objectInfo. It seems like the simplest thing is to permit other modules to modify the $twig_input prior to rendering the template.

@patdunlavey
Copy link
Author

My initial idea is to expose a hook hook_imi_twig_input_alter() just before the call to islandora_multi_importer_twig_process in IslandoraMultiBatchObject::getDatastreams that takes as arguments the twig input data, and, for context, $objectInfo and $preprocessorParameters.

However, I see that there are a couple other cases where the twig data is prepared and rendered for preview, which do not have the $objectInfo or $preprocessorParameters. We could either ignore these (i.e. there would be no indication in the twig preview of the altered twig input), or we could invoke the alter hook in each of these places. In this case, the hook implementation would need to be written so as to handle case where context is not available. Here's an idea:

/**
 * This hook permits the input to a twig template to to modified at run time.
 * The impetus for this was a desire to be able to include the PID of the object
 * in an identifier field in the MODS datastream. The PID is provided in the $objectInfo,
 * variable during the batch ingest operation, so we can use it then.
 *
 * @param $twig_input
 *    an associative array, with $twig_input['data'] providing the field data that will
 *    be available to the twig template in the `data` variable.
 * @param $context
 *    NULL, or array that may contain one or both of...
 *    - 'objectInfo' => an array of data returned by IslandoraMultiBatch::getIngestInfo()
 *    - 'preprocessorParameters' => an array of data populated in IslandoraMultiBatch::getDatastreams
 *         prior to calling islandora_multi_importer_twig_process.
 */
function hook_imi_twig_input_alter(&$twig_input, $context = NULL) {
  if(!empty($context['objectInfo']['pid'])) {
    $twig_input['data']['new_pid'] = $context['objectInfo']['pid'];
  }
  else {
    $twig_input['data']['new_pid'] = "[pid placeholder]";
  }
}

@DiegoPino thoughts?

@DiegoPino
Copy link
Contributor

DiegoPino commented May 4, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants