Skip to content

Commit

Permalink
Merge pull request #880 from seth-shaw-unlv/has_media_view_filter
Browse files Browse the repository at this point in the history
Has media view filter
  • Loading branch information
ajstanley authored Jun 10, 2022
2 parents 491631c + 39c7b31 commit 72c7dff
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
12 changes: 12 additions & 0 deletions islandora.views.inc
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ function islandora_views_data_alter(&$data) {
}
}
}

// Add Has Media filter.
$data['node_field_data']['islandora_has_media'] = [
'title' => t('Node has Media Use'),
'group' => t('Content'),
'filter' => [
'title' => t('Node has media use filter'),
'help' => t('Provides a custom filter for nodes that do or do not have media with a given use.'),
'field' => 'nid',
'id' => 'islandora_node_has_media_use',
],
];
}
94 changes: 94 additions & 0 deletions src/Plugin/views/filter/NodeHasMediaUse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Drupal\islandora\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;

/**
* Views Filter on Having Media of a Type.
*
* @ingroup views_field_handlers
*
* @ViewsFilter("islandora_node_has_media_use")
*/
class NodeHasMediaUse extends FilterPluginBase {

/**
* {@inheritdoc}
*/
protected function defineOptions() {
return [
'use_uri' => ['default' => NULL],
'negated' => ['default' => FALSE],
];
}

/**
* {@inheritdoc}
*/
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$uri = $form_state->getValues()['options']['use_uri'];
$term = \Drupal::service('islandora.utils')->getTermForUri($uri);
if (empty($term)) {
$form_state->setError($form['use_uri'], $this->t('Could not find term with URI: "%uri"', ['%uri' => $uri]));
}
}

/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'islandora_media_use']);
$uris = [];
foreach ($terms as $term) {
foreach ($term->get('field_external_uri')->getValue() as $uri) {
$uris[$uri['uri']] = $term->label();
}
}

$form['use_uri'] = [
'#type' => 'select',
'#title' => "Media Use Term",
'#options' => $uris,
'#default_value' => $this->options['use_uri'],
'#required' => TRUE,
];
$form['negated'] = [
'#type' => 'checkbox',
'#title' => 'Negated',
'#description' => $this->t("Return nodes that <em>don't</em> have this use URI"),
'#default_value' => $this->options['negated'],
];
}

/**
* {@inheritdoc}
*/
public function adminSummary() {
$operator = ($this->options['negated']) ? "does not have" : "has";
$term = \Drupal::service('islandora.utils')->getTermForUri($this->options['use_uri']);
$label = (empty($term)) ? 'BROKEN TERM URI' : $term->label();
return "Node {$operator} a '{$label}' media";
}

/**
* {@inheritdoc}
*/
public function query() {
$condition = ($this->options['negated']) ? 'NOT IN' : 'IN';
$utils = \Drupal::service('islandora.utils');
$term = $utils->getTermForUri($this->options['use_uri']);
if (empty($term)) {
\Drupal::logger('islandora')->warning('Node Has Media Filter could not find term with URI: "%uri"', ['%uri' => $this->options['use_uri']]);
return;
}
$sub_query = \Drupal::database()->select('media', 'm');
$sub_query->join('media__field_media_use', 'use', 'm.mid = use.entity_id');
$sub_query->join('media__field_media_of', 'of', 'm.mid = of.entity_id');
$sub_query->fields('of', ['field_media_of_target_id'])
->condition('use.field_media_use_target_id', $term->id());
$this->query->addWhere(0, 'nid', $sub_query, $condition);
}

}

0 comments on commit 72c7dff

Please sign in to comment.