diff --git a/config/schema/islandora.schema.yml b/config/schema/islandora.schema.yml index 1df5b7e8..9c2d33fe 100644 --- a/config/schema/islandora.schema.yml +++ b/config/schema/islandora.schema.yml @@ -113,6 +113,13 @@ condition.plugin.media_uses_filesystem: sequence: type: string +condition.plugin.media_has_mimetype: + type: condition.plugin + mapping: + mimetypes: + type: text + label: 'Mime types' + condition.plugin.content_entity_type: type: condition.plugin mapping: diff --git a/islandora.module b/islandora.module index 8b0d52e1..5d55da16 100644 --- a/islandora.module +++ b/islandora.module @@ -347,6 +347,7 @@ function islandora_form_block_form_alter(&$form, FormStateInterface $form_state, unset($form['visibility']['file_uses_filesystem']); unset($form['visibility']['node_has_term']); unset($form['visibility']['media_uses_filesystem']); + unset($form['visibility']['media_has_mimetype']); } /** diff --git a/src/Plugin/Condition/MediaHasMimetype.php b/src/Plugin/Condition/MediaHasMimetype.php new file mode 100644 index 00000000..fcf085ed --- /dev/null +++ b/src/Plugin/Condition/MediaHasMimetype.php @@ -0,0 +1,174 @@ +utils = $utils; + $this->entityTypeManager = $entity_type_manager; + $this->mediaSource = $media_source; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('islandora.utils'), + $container->get('entity_type.manager'), + $container->get('islandora.media_source_service') + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['mimetypes'] = [ + '#type' => 'textfield', + '#title' => t('Mime types'), + '#default_value' => $this->configuration['mimetypes'], + '#required' => TRUE, + '#maxlength' => 256, + '#description' => t('Comma-delimited list of Mime types (e.g. image/jpeg, video/mp4, etc...) that trigger the condition.'), + ]; + return parent::buildConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['mimetypes'] = $form_state->getValue('mimetypes'); + parent::submitConfigurationForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function summary() { + $mimetypes = $this->configuration['mimetypes']; + return $this->t( + 'The media has one of the Mime types @mimetypes', + [ + '@mimetypes' => $mimetypes, + ] + ); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + if (empty($this->configuration['mimetypes']) && !$this->isNegated()) { + return TRUE; + } + + $node = \Drupal::routeMatch()->getParameter('node'); + + if (is_null($node) || is_string($node)) { + return FALSE; + } + + $media = $this->utils->getMedia($node); + + if (count($media) > 0) { + $mimetypes = explode(',', str_replace(' ', '', $this->configuration['mimetypes'])); + foreach ($media as $medium) { + $file = $this->mediaSource->getSourceFile($medium); + if (in_array($file->getMimeType(), $mimetypes)) { + return $this->isNegated() ? FALSE : TRUE; + } + } + } + else { + return FALSE; + } + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return array_merge( + ['mimetypes' => ''], + parent::defaultConfiguration() + ); + } + +}