Skip to content

Commit 72c7dff

Browse files
authored
Merge pull request #880 from seth-shaw-unlv/has_media_view_filter
Has media view filter
2 parents 491631c + 39c7b31 commit 72c7dff

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

islandora.views.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,16 @@ function islandora_views_data_alter(&$data) {
2525
}
2626
}
2727
}
28+
29+
// Add Has Media filter.
30+
$data['node_field_data']['islandora_has_media'] = [
31+
'title' => t('Node has Media Use'),
32+
'group' => t('Content'),
33+
'filter' => [
34+
'title' => t('Node has media use filter'),
35+
'help' => t('Provides a custom filter for nodes that do or do not have media with a given use.'),
36+
'field' => 'nid',
37+
'id' => 'islandora_node_has_media_use',
38+
],
39+
];
2840
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Drupal\islandora\Plugin\views\filter;
4+
5+
use Drupal\Core\Form\FormStateInterface;
6+
use Drupal\views\Plugin\views\filter\FilterPluginBase;
7+
8+
/**
9+
* Views Filter on Having Media of a Type.
10+
*
11+
* @ingroup views_field_handlers
12+
*
13+
* @ViewsFilter("islandora_node_has_media_use")
14+
*/
15+
class NodeHasMediaUse extends FilterPluginBase {
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
protected function defineOptions() {
21+
return [
22+
'use_uri' => ['default' => NULL],
23+
'negated' => ['default' => FALSE],
24+
];
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
31+
$uri = $form_state->getValues()['options']['use_uri'];
32+
$term = \Drupal::service('islandora.utils')->getTermForUri($uri);
33+
if (empty($term)) {
34+
$form_state->setError($form['use_uri'], $this->t('Could not find term with URI: "%uri"', ['%uri' => $uri]));
35+
}
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
42+
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'islandora_media_use']);
43+
$uris = [];
44+
foreach ($terms as $term) {
45+
foreach ($term->get('field_external_uri')->getValue() as $uri) {
46+
$uris[$uri['uri']] = $term->label();
47+
}
48+
}
49+
50+
$form['use_uri'] = [
51+
'#type' => 'select',
52+
'#title' => "Media Use Term",
53+
'#options' => $uris,
54+
'#default_value' => $this->options['use_uri'],
55+
'#required' => TRUE,
56+
];
57+
$form['negated'] = [
58+
'#type' => 'checkbox',
59+
'#title' => 'Negated',
60+
'#description' => $this->t("Return nodes that <em>don't</em> have this use URI"),
61+
'#default_value' => $this->options['negated'],
62+
];
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function adminSummary() {
69+
$operator = ($this->options['negated']) ? "does not have" : "has";
70+
$term = \Drupal::service('islandora.utils')->getTermForUri($this->options['use_uri']);
71+
$label = (empty($term)) ? 'BROKEN TERM URI' : $term->label();
72+
return "Node {$operator} a '{$label}' media";
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function query() {
79+
$condition = ($this->options['negated']) ? 'NOT IN' : 'IN';
80+
$utils = \Drupal::service('islandora.utils');
81+
$term = $utils->getTermForUri($this->options['use_uri']);
82+
if (empty($term)) {
83+
\Drupal::logger('islandora')->warning('Node Has Media Filter could not find term with URI: "%uri"', ['%uri' => $this->options['use_uri']]);
84+
return;
85+
}
86+
$sub_query = \Drupal::database()->select('media', 'm');
87+
$sub_query->join('media__field_media_use', 'use', 'm.mid = use.entity_id');
88+
$sub_query->join('media__field_media_of', 'of', 'm.mid = of.entity_id');
89+
$sub_query->fields('of', ['field_media_of_target_id'])
90+
->condition('use.field_media_use_target_id', $term->id());
91+
$this->query->addWhere(0, 'nid', $sub_query, $condition);
92+
}
93+
94+
}

0 commit comments

Comments
 (0)