Skip to content

Commit

Permalink
Dependency injection in NodeHasMediaUse views filter. (#1012)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Vessey <[email protected]>
  • Loading branch information
rosiel and adam-vessey authored Apr 2, 2024
1 parent 06d630f commit f1d63ca
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions src/Plugin/views/filter/NodeHasMediaUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Drupal\islandora\Plugin\views\filter;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Views Filter on Having Media of a Type.
Expand All @@ -14,6 +19,48 @@
*/
class NodeHasMediaUse extends FilterPluginBase {

/**
* Islandora's utility service.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected IslandoraUtils $utils;

/**
* Drupal's entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;

/**
* Drupal's database connection service.
*
* @var \Drupal\Core\Database\Connection
*/
protected Connection $connection;

/**
* Logger service.
*
* @var \Psr\Log\LoggerInterface
*/
protected LoggerInterface $logger;

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);

$instance->utils = $container->get('islandora.utils');
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->connection = $container->get('database');
$instance->logger = $container->get('logger.factory')->get('islanodra');

return $instance;
}

/**
* {@inheritdoc}
*/
Expand All @@ -29,7 +76,7 @@ protected function defineOptions() {
*/
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$uri = $form_state->getValues()['options']['use_uri'];
$term = \Drupal::service('islandora.utils')->getTermForUri($uri);
$term = $this->utils->getTermForUri($uri);
if (empty($term)) {
$form_state->setError($form['use_uri'], $this->t('Could not find term with URI: "%uri"', ['%uri' => $uri]));
}
Expand All @@ -39,7 +86,7 @@ public function validateOptionsForm(&$form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'islandora_media_use']);
$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties(['vid' => 'islandora_media_use']);
$uris = [];
foreach ($terms as $term) {
foreach ($term->get('field_external_uri')->getValue() as $uri) {
Expand Down Expand Up @@ -67,7 +114,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
*/
public function adminSummary() {
$operator = ($this->options['negated']) ? "does not have" : "has";
$term = \Drupal::service('islandora.utils')->getTermForUri($this->options['use_uri']);
$term = $this->utils->getTermForUri($this->options['use_uri']);
$label = (empty($term)) ? 'BROKEN TERM URI' : $term->label();
return "Node {$operator} a '{$label}' media";
}
Expand All @@ -77,13 +124,12 @@ public function adminSummary() {
*/
public function query() {
$condition = ($this->options['negated']) ? 'NOT IN' : 'IN';
$utils = \Drupal::service('islandora.utils');
$term = $utils->getTermForUri($this->options['use_uri']);
$term = $this->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']]);
$this->logger->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 = $this->connection->select('media', 'm');
$use_alias = $sub_query->join('media__field_media_use', 'use', 'm.mid = %alias.entity_id');
$of_alias = $sub_query->join('media__field_media_of', 'of', 'm.mid = %alias.entity_id');
$sub_query->fields($of_alias, ['field_media_of_target_id'])
Expand Down

0 comments on commit f1d63ca

Please sign in to comment.