Skip to content

Commit

Permalink
Implement inputargs in GenerateImageDerivative and GenerateImageDeriv…
Browse files Browse the repository at this point in the history
…ativeFile, instead of in AbstractGenerateDerivative. Add utility function to add a form element (or other array entry) at a specific position.
  • Loading branch information
jasonhildebrand committed Oct 24, 2022
1 parent 94bfdd0 commit deff2dd
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form = parent::buildConfigurationForm($form, $form_state);
$form['mimetype']['#description'] = $this->t('Mimetype to convert to (e.g. audio/mpeg, audio/m4a, etc...)');
$form['args']['#description'] = $this->t('Additional command line parameters for FFMpeg');
unset($form['inputargs']);
return $form;
}

Expand Down
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 @@ -23,7 +23,8 @@ public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config['mimetype'] = 'image/jpeg';
$config['path'] = '[date:custom:Y]-[date:custom:m]/[node:nid].jpg';
$config['destination_media_type'] = 'image';
$config['destination_media_type'] = 'image';
$config['inputargs'] = '';
return $config;
}

Expand All @@ -34,11 +35,22 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form = parent::buildConfigurationForm($form, $form_state);
$form['mimetype']['#description'] = $this->t('Mimetype to convert to (e.g. image/jpeg, image/png, etc...)');

$form['inputargs']['#title'] = $this->t('Additional input arguments');
$form['inputargs']['#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.');

// 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->array_insert_after($form, 'mimetype', $new);


return $form;
}

Expand All @@ -58,4 +70,11 @@ 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 @@ -26,6 +26,7 @@ public function defaultConfiguration() {
$config['queue'] = 'islandora-connector-houdini';
$config['destination_media_type'] = 'file';
$config['scheme'] = $this->config->get('default_scheme');
$config['inputargs'] = '';
return $config;
}

Expand All @@ -38,12 +39,29 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form['mimetype']['#value'] = 'image/jpeg';
$form['mimetype']['#type'] = 'hidden';

$form['inputargs']['#title'] = $this->t('Additional input arguments');
$form['inputargs']['#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.');

// 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->array_insert_after($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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form['mimetype']['#type'] = 'textfield';

unset($form['args']);
unset($form['inputargs']);
return $form;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form = array_merge($first, $middle, $last);

unset($form['args']);
unset($form['inputargs']);
return $form;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form = parent::buildConfigurationForm($form, $form_state);
$form['mimetype']['#description'] = $this->t('Mimetype to convert to (e.g. video/mp4, video/quicktime, etc...)');
$form['args']['#description'] = $this->t('Additional command line parameters for FFMpeg');
unset($form['inputargs']);
return $form;
}

Expand Down
22 changes: 22 additions & 0 deletions src/IslandoraUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,4 +751,26 @@ 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 )
*
* @param array $array
* @param string $key
* @param array $new
*
* @return array
*
* Credit: https://gist.github.com/wpscholar/0deadce1bbfa4adb4e4c
*/
function array_insert_after( 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 ) );
}

}
9 changes: 0 additions & 9 deletions src/Plugin/Action/AbstractGenerateDerivative.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function defaultConfiguration() {
'source_term_uri' => '',
'derivative_term_uri' => '',
'mimetype' => '',
'inputargs' => '',
'args' => '',
'destination_media_type' => '',
'scheme' => $this->config->get('default_scheme'),
Expand Down Expand Up @@ -131,13 +130,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#rows' => '8',
'#description' => $this->t('Mimetype to convert to (e.g. image/jpeg, video/mp4, etc...)'),
];
$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'),
Expand Down Expand Up @@ -207,7 +199,6 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
$this->configuration['derivative_term_uri'] = $this->utils->getUriForTerm($term);

$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

0 comments on commit deff2dd

Please sign in to comment.