From 62211ff90980f02706ca5131a411149d595c4fed Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Wed, 8 Jun 2022 16:43:39 -0700 Subject: [PATCH 1/5] add NodeHasMediaUse views filter --- islandora.views.inc | 12 +++ src/Plugin/views/filter/NodeHasMediaUse.php | 88 +++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/Plugin/views/filter/NodeHasMediaUse.php diff --git a/islandora.views.inc b/islandora.views.inc index cd826d08..de176f15 100644 --- a/islandora.views.inc +++ b/islandora.views.inc @@ -22,4 +22,16 @@ function islandora_views_data_alter(&$data) { $data['node__' . $field][$field]['field']['id'] = 'integer_weight_selector'; } } + + // 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', + ], + ]; } diff --git a/src/Plugin/views/filter/NodeHasMediaUse.php b/src/Plugin/views/filter/NodeHasMediaUse.php new file mode 100644 index 00000000..e83c6306 --- /dev/null +++ b/src/Plugin/views/filter/NodeHasMediaUse.php @@ -0,0 +1,88 @@ + NULL]; + $options['negated'] = ['default' => FALSE]; + + return $options; + } + + /** + * {@inheritdoc} + */ + public function validateOptionsForm(&$form, FormStateInterface $form_state) { + parent::validateOptionsForm($form, $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} + */ + protected function valueForm(&$form, FormStateInterface $form_state) { + $form['use_uri'] = [ + '#type' => 'textfield', + '#title' => "Media Use URI", + '#default_value' => $this->options['use_uri'], + '#required' => TRUE, + ]; + $form['negated'] = [ + '#type' => 'checkbox', + '#title' => 'Negated', + '#description' => $this->t("Return nodes that don't have this use URI"), + '#default_value' => $this->options['negated'], + ]; + } + + /** + * {@inheritdoc} + */ + public function adminSummary() { + $operator = ($this->options['negated']) ? "does not have" : "has"; + return "Node {$operator} media with use: '{$this->options['use_uri']}'"; + } + + /** + * {@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); + + } + +} From a90630d9760c4daf5c9951e6bc2302c251f96d3b Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Thu, 9 Jun 2022 12:34:44 -0700 Subject: [PATCH 2/5] turn textfield into a select --- src/Plugin/views/filter/NodeHasMediaUse.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Plugin/views/filter/NodeHasMediaUse.php b/src/Plugin/views/filter/NodeHasMediaUse.php index e83c6306..bb84ba6b 100644 --- a/src/Plugin/views/filter/NodeHasMediaUse.php +++ b/src/Plugin/views/filter/NodeHasMediaUse.php @@ -43,9 +43,17 @@ class NodeHasMediaUse extends FilterPluginBase { * {@inheritdoc} */ protected function valueForm(&$form, FormStateInterface $form_state) { + $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'islandora_media_use']); + $uris = []; + foreach ($terms as $term) { + $uri = reset($term->get('field_external_uri')->getValue()); + $uris[$uri['uri']] = $term->label(); + } + $form['use_uri'] = [ - '#type' => 'textfield', - '#title' => "Media Use URI", + '#type' => 'select', + '#title' => "Media Use Term", + '#options' => $uris, '#default_value' => $this->options['use_uri'], '#required' => TRUE, ]; From f6a66fe082bf61806f2bf3b031bdf3879be4e09d Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Thu, 9 Jun 2022 12:45:37 -0700 Subject: [PATCH 3/5] use term label for filter summary --- src/Plugin/views/filter/NodeHasMediaUse.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Plugin/views/filter/NodeHasMediaUse.php b/src/Plugin/views/filter/NodeHasMediaUse.php index bb84ba6b..a13bdad4 100644 --- a/src/Plugin/views/filter/NodeHasMediaUse.php +++ b/src/Plugin/views/filter/NodeHasMediaUse.php @@ -70,7 +70,8 @@ class NodeHasMediaUse extends FilterPluginBase { */ public function adminSummary() { $operator = ($this->options['negated']) ? "does not have" : "has"; - return "Node {$operator} media with use: '{$this->options['use_uri']}'"; + $term = \Drupal::service('islandora.utils')->getTermForUri($this->options['use_uri']); + return "Node {$operator} a '{$term->label()}' media"; } /** From 352631099e98510eac9180ddf08de2a912555e7d Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Thu, 9 Jun 2022 12:49:35 -0700 Subject: [PATCH 4/5] paranoid checking --- src/Plugin/views/filter/NodeHasMediaUse.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Plugin/views/filter/NodeHasMediaUse.php b/src/Plugin/views/filter/NodeHasMediaUse.php index a13bdad4..e17cdf3a 100644 --- a/src/Plugin/views/filter/NodeHasMediaUse.php +++ b/src/Plugin/views/filter/NodeHasMediaUse.php @@ -71,7 +71,8 @@ class NodeHasMediaUse extends FilterPluginBase { public function adminSummary() { $operator = ($this->options['negated']) ? "does not have" : "has"; $term = \Drupal::service('islandora.utils')->getTermForUri($this->options['use_uri']); - return "Node {$operator} a '{$term->label()}' media"; + $label = (empty($term)) ? 'BROKEN TERM URI' : $term->label(); + return "Node {$operator} a '{$label}' media"; } /** From 39c7b3180a3acfad0583a7747794f77543d0069a Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Fri, 10 Jun 2022 08:58:13 -0700 Subject: [PATCH 5/5] remove ability to expose form; todo: bring back some day --- src/Plugin/views/filter/NodeHasMediaUse.php | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Plugin/views/filter/NodeHasMediaUse.php b/src/Plugin/views/filter/NodeHasMediaUse.php index e17cdf3a..0209505d 100644 --- a/src/Plugin/views/filter/NodeHasMediaUse.php +++ b/src/Plugin/views/filter/NodeHasMediaUse.php @@ -18,20 +18,16 @@ class NodeHasMediaUse extends FilterPluginBase { * {@inheritdoc} */ protected function defineOptions() { - $options = parent::defineOptions(); - - $options['use_uri'] = ['default' => NULL]; - $options['negated'] = ['default' => FALSE]; - - return $options; + return [ + 'use_uri' => ['default' => NULL], + 'negated' => ['default' => FALSE], + ]; } /** * {@inheritdoc} */ public function validateOptionsForm(&$form, FormStateInterface $form_state) { - parent::validateOptionsForm($form, $form_state); - $uri = $form_state->getValues()['options']['use_uri']; $term = \Drupal::service('islandora.utils')->getTermForUri($uri); if (empty($term)) { @@ -42,12 +38,13 @@ class NodeHasMediaUse extends FilterPluginBase { /** * {@inheritdoc} */ - protected function valueForm(&$form, FormStateInterface $form_state) { + public function buildOptionsForm(&$form, FormStateInterface $form_state) { $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'islandora_media_use']); $uris = []; foreach ($terms as $term) { - $uri = reset($term->get('field_external_uri')->getValue()); - $uris[$uri['uri']] = $term->label(); + foreach ($term->get('field_external_uri')->getValue() as $uri) { + $uris[$uri['uri']] = $term->label(); + } } $form['use_uri'] = [ @@ -92,7 +89,6 @@ class NodeHasMediaUse extends FilterPluginBase { $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); - } }