From 4ce5e44c29a3500198237335fa7b7d25fbdabeab Mon Sep 17 00:00:00 2001 From: Jared Whiklo Date: Wed, 12 Dec 2018 13:44:08 -0600 Subject: [PATCH] Make derivative Drupal media type configurable (#106) * Make derivative media type configurable * Unset parameter or Alpaca vomits * Coder * More coder --- .../Plugin/Action/GenerateImageDerivative.php | 1 + .../Plugin/Action/GenerateVideoDerivative.php | 1 + .../Action/AbstractGenerateDerivative.php | 38 ++++++++++++++++++- .../FormDisplayAlterReactionTest.php | 3 +- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/modules/islandora_image/src/Plugin/Action/GenerateImageDerivative.php b/modules/islandora_image/src/Plugin/Action/GenerateImageDerivative.php index 4e6a36e2..fa713a71 100644 --- a/modules/islandora_image/src/Plugin/Action/GenerateImageDerivative.php +++ b/modules/islandora_image/src/Plugin/Action/GenerateImageDerivative.php @@ -23,6 +23,7 @@ class GenerateImageDerivative extends AbstractGenerateDerivative { $config = parent::defaultConfiguration(); $config['mimetype'] = 'image/jpeg'; $config['path'] = '[date:custom:Y]-[date:custom:m]/[node:nid].jpg'; + $config['destination_media_type'] = 'image'; return $config; } diff --git a/modules/islandora_video/src/Plugin/Action/GenerateVideoDerivative.php b/modules/islandora_video/src/Plugin/Action/GenerateVideoDerivative.php index 81d5ac7c..c28ffdc0 100644 --- a/modules/islandora_video/src/Plugin/Action/GenerateVideoDerivative.php +++ b/modules/islandora_video/src/Plugin/Action/GenerateVideoDerivative.php @@ -24,6 +24,7 @@ class GenerateVideoDerivative extends AbstractGenerateDerivative { $config['path'] = '[date:custom:Y]-[date:custom:m]/[node:nid].mp4'; $config['mimetype'] = 'video/mp4'; $config['queue'] = 'islandora-connector-homarus'; + $config['destination_media_type'] = 'video'; return $config; } diff --git a/src/Plugin/Action/AbstractGenerateDerivative.php b/src/Plugin/Action/AbstractGenerateDerivative.php index d7acc5a2..4635a95c 100644 --- a/src/Plugin/Action/AbstractGenerateDerivative.php +++ b/src/Plugin/Action/AbstractGenerateDerivative.php @@ -126,6 +126,7 @@ class AbstractGenerateDerivative extends EmitEvent { 'derivative_term_uri' => '', 'mimetype' => '', 'args' => '', + 'destination_media_type' => '', 'scheme' => file_default_scheme(), 'path' => '[date:custom:Y]-[date:custom:m]/[node:nid].bin', ]; @@ -165,7 +166,7 @@ class AbstractGenerateDerivative extends EmitEvent { $route_params = [ 'node' => $entity->id(), - 'media_type' => $source_media->bundle(), + 'media_type' => $this->configuration['destination_media_type'], 'taxonomy_term' => $derivative_term->id(), ]; $data['destination_uri'] = Url::fromRoute('islandora.media_source_put_to_node', $route_params) @@ -186,6 +187,7 @@ class AbstractGenerateDerivative extends EmitEvent { unset($data['derivative_term_uri']); unset($data['path']); unset($data['scheme']); + unset($data['destination_media_type']); return $data; } @@ -216,6 +218,14 @@ class AbstractGenerateDerivative extends EmitEvent { '#required' => TRUE, '#description' => t('Term indicating the derivative media'), ]; + $form['destination_media_type'] = [ + '#type' => 'entity_autocomplete', + '#target_type' => 'media_type', + '#title' => t('Derivative media type'), + '#default_value' => $this->getEntityById($this->configuration['destination_media_type']), + '#required' => TRUE, + '#description' => t('The Drupal media type to create with this derivative, can be different than the source'), + ]; $form['mimetype'] = [ '#type' => 'textfield', '#title' => t('Mimetype'), @@ -296,6 +306,32 @@ class AbstractGenerateDerivative extends EmitEvent { $this->configuration['args'] = $form_state->getValue('args'); $this->configuration['scheme'] = $form_state->getValue('scheme'); $this->configuration['path'] = trim($form_state->getValue('path'), '\\/'); + $this->configuration['destination_media_type'] = $form_state->getValue('destination_media_type'); + } + + /** + * Find a media_type by id and return it or nothing. + * + * @param string $entity_id + * The media type. + * + * @return \Drupal\Core\Entity\EntityInterface|string + * Return the loaded entity or nothing. + * + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * Thrown by getStorage() if the entity type doesn't exist. + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * Thrown by getStorage() if the storage handler couldn't be loaded. + */ + protected function getEntityById($entity_id) { + $entity_ids = $this->entityTypeManager->getStorage('media_type') + ->getQuery()->condition('id', $entity_id)->execute(); + + $id = reset($entity_ids); + if ($id !== FALSE) { + return $this->entityTypeManager->getStorage('media_type')->load($id); + } + return ''; } } diff --git a/tests/src/Functional/FormDisplayAlterReactionTest.php b/tests/src/Functional/FormDisplayAlterReactionTest.php index d5d41dea..08501579 100644 --- a/tests/src/Functional/FormDisplayAlterReactionTest.php +++ b/tests/src/Functional/FormDisplayAlterReactionTest.php @@ -40,7 +40,7 @@ class FormDisplayAlterReactionTest extends IslandoraFunctionalTestBase { $url = $this->getUrl(); // Visit the edit url and make sure we're on the default form display - // (e.g. there's an autocomplete for the member of field); + // (e.g. there's an autocomplete for the member of field). $this->drupalGet($url . "/edit"); $this->assertSession()->pageTextContains("Member Of"); @@ -62,4 +62,3 @@ class FormDisplayAlterReactionTest extends IslandoraFunctionalTestBase { } } -