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

Add an "input args" parameter when defining derivative actions. #904

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ action.configuration.generate_image_derivative:
args:
type: text
label: 'Convert Arguments'
inputargs:
type: text
label: 'Input arguments for convert'
scheme:
type: text
label: 'Flysystem scheme'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function defaultConfiguration() {
$config['mimetype'] = 'image/jpeg';
$config['path'] = '[date:custom:Y]-[date:custom:m]/[node:nid].jpg';
$config['destination_media_type'] = 'image';
$config['inputargs'] = '';
return $config;
}

Expand All @@ -33,7 +34,21 @@ public function defaultConfiguration() {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['mimetype']['#description'] = $this->t('Mimetype to convert to (e.g. image/jpeg, image/png, etc...)');
$form['args']['#description'] = $this->t('Additional command line arguments for ImageMagick convert (e.g. -resize 50%');

// Adjust args title and description for better clarity.
$form['args']['#title'] = $this->t('Additional output arguments');
$form['args']['#description'] = $this->t('Additional output options for ImageMagick convert (e.g. -resize 50% -unsharp 0x.5).<br>See <a target="_blank" href="https://imagemagick.org/script/convert.php">documentation</a> for available options.');

$new = [
'inputargs' => [
'#type' => 'textfield',
'#title' => $this->t('Additional input arguments'),
'#default_value' => $this->configuration['inputargs'],
'#rows' => '8',
'#description' => $this->t('Additional input options for ImageMagick convert (e.g. -density 144).<br>Check the <a target="_blank" href="https://manpages.ubuntu.com/manpages/trusty/man1/convert.im6.1.html">man page</a> to see which options are input options.'),
],
];
$form = $this->utils->arrayInsertAfter($form, 'mimetype', $new);
return $form;
}

Expand All @@ -53,4 +68,12 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
}
}

/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['inputargs'] = $form_state->getValue('inputargs');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function defaultConfiguration() {
$config['mimetype'] = 'application/xml';
$config['queue'] = 'islandora-connector-houdini';
$config['scheme'] = $this->config->get('default_scheme');
$config['inputargs'] = '';
return $config;
}

Expand Down Expand Up @@ -60,7 +61,32 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form['mimetype']['#value'] = 'image/jpeg';
$form['mimetype']['#description'] = 'Mimetype to convert to. Must be
compatible with the destination image field.';
$form['mimetype']['#type'] = 'hidden';

// Adjust args title and description for better clarity.
$form['args']['#title'] = $this->t('Additional output arguments');
$form['args']['#description'] = $this->t('Additional output options for ImageMagick convert (e.g. -resize 50% -unsharp 0x.5).<br>See <a target="_blank" href="https://imagemagick.org/script/convert.php">documentation</a> for available options.');

$new = [
'inputargs' => [
'#type' => 'textfield',
'#title' => $this->t('Additional input arguments'),
'#default_value' => $this->configuration['inputargs'],
'#rows' => '8',
'#description' => $this->t('Additional input options for ImageMagick convert (e.g. -density 144).<br>Check the <a target="_blank" href="https://manpages.ubuntu.com/manpages/trusty/man1/convert.im6.1.html">man page</a> to see which options are input options.'),
],
];
$form = $this->utils->arrayInsertAfter($form, 'mimetype', $new);

return $form;
}

/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['inputargs'] = $form_state->getValue('inputargs');
}

}
29 changes: 28 additions & 1 deletion src/IslandoraUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function __construct(
ContextManager $context_manager,
FlysystemFactory $flysystem_factory,
LanguageManagerInterface $language_manager,
AccountInterface $current_user
AccountInterface $current_user,
) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
Expand Down Expand Up @@ -768,6 +768,33 @@ protected function getParentsByEntityReference(ContentEntityInterface $entity, a
return $parents;
}

/**
* Insert a value or key/value pair after a specific key in an array.
*
* If key doesn't exist, value is appended to the end of the array.
* This is not islandora specific, but a useful function that neither PHP
* nor Drupal provide. (is there still hope after 16 years?
* https://www.drupal.org/project/drupal/issues/66183 )
*
* Credit: https://gist.github.com/wpscholar/0deadce1bbfa4adb4e4c
*
* @param array $array
* Array in which the new element should be inserted.
* @param string $key
* The key after which the new element should be inserted.
* @param array $new
* The new element to insert.
*
* @return array
* The new array.
*/
public function arrayInsertAfter(array $array, $key, array $new) {
$keys = array_keys($array);
$index = array_search($key, $keys);
$pos = FALSE === $index ? count($array) : $index + 1;
return array_merge(array_slice($array, 0, $pos), $new, array_slice($array, $pos));
}

/**
* Deletes Media and all associated files.
*
Expand Down
15 changes: 13 additions & 2 deletions src/Plugin/Action/AbstractGenerateDerivativeMediaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function defaultConfiguration() {
'event' => 'Generate Derivative',
'source_term_uri' => $uri,
'mimetype' => '',
'inputargs' => '',
'args' => '',
'path' => '[date:custom:Y]-[date:custom:m]/[media:mid].bin',
'source_field_name' => 'field_media_file',
Expand Down Expand Up @@ -72,6 +73,7 @@ protected function generateData(EntityInterface $entity) {
$allowed = [
'queue',
'event',
'inputargs',
'args',
'source_uri',
'destination_uri',
Expand Down Expand Up @@ -120,12 +122,20 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
Selected destination field must be present on the media.'),
];

$form['inputargs'] = [
'#type' => 'textfield',
'#title' => $this->t('Additional input arguments'),
'#default_value' => $this->configuration['inputargs'],
'#rows' => '8',
'#description' => $this->t('Additional command line options related to the source file'),
];

$form['args'] = [
'#type' => 'textfield',
'#title' => $this->t('Additional arguments'),
'#title' => $this->t('Additional output arguments'),
'#default_value' => $this->configuration['args'],
'#rows' => '8',
'#description' => $this->t('Additional command line arguments'),
'#description' => $this->t('Additional command line options related to the output file (derivative)'),
];

$form['mimetype'] = [
Expand Down Expand Up @@ -181,6 +191,7 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['mimetype'] = $form_state->getValue('mimetype');
$this->configuration['inputargs'] = $form_state->getValue('inputargs');
$this->configuration['args'] = $form_state->getValue('args');
$this->configuration['scheme'] = $form_state->getValue('scheme');
$this->configuration['path'] = trim($form_state->getValue('path'), '\\/');
Expand Down