|
| 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