Compare commits

..

2 Commits

Author SHA1 Message Date
Rosie Le Faive b8f0b9c966
Allow NodeHasMediaUse view filter to work on views with node relationships. (#1010) 7 months ago
Akanksha Singh da47bcfb08
Node has Parent context does not explicitly check if the field exists. (#1019) 7 months ago
  1. 2
      src/IslandoraContextManager.php
  2. 6
      src/Plugin/Condition/NodeHasParent.php
  3. 74
      src/Plugin/views/filter/NodeHasMediaUse.php

2
src/IslandoraContextManager.php

@ -37,7 +37,7 @@ class IslandoraContextManager extends ContextManager {
} }
/** @var \Drupal\context\ContextInterface $context */ /** @var \Drupal\context\ContextInterface $context */
foreach ($this->getContexts() as $context) { foreach ($this->getContexts() as $context) {
if ($this->evaluateContextConditions($context, $provided) && !$context->disabled()) { if (!$context->disabled() && $this->evaluateContextConditions($context, $provided)) {
$this->activeContexts[$context->id()] = $context; $this->activeContexts[$context->id()] = $context;
} }
} }

6
src/Plugin/Condition/NodeHasParent.php

@ -137,9 +137,11 @@ class NodeHasParent extends ConditionPluginBase implements ContainerFactoryPlugi
* TRUE if entity references the specified parent. * TRUE if entity references the specified parent.
*/ */
protected function evaluateEntity(EntityInterface $entity) { protected function evaluateEntity(EntityInterface $entity) {
$parent_reference_field = $this->configuration['parent_reference_field'];
foreach ($entity->referencedEntities() as $referenced_entity) { foreach ($entity->referencedEntities() as $referenced_entity) {
if ($entity->getEntityTypeID() == 'node' && $referenced_entity->getEntityTypeId() == 'node') { // Check whether the entity and the referenced entity are nodes.
$parent_reference_field = $this->configuration['parent_reference_field']; // Also make sure that the field exists.
if ($entity->getEntityTypeID() == 'node' && $entity->hasField($parent_reference_field) && $referenced_entity->getEntityTypeId() == 'node') {
$field = $entity->get($parent_reference_field); $field = $entity->get($parent_reference_field);
if (!$field->isEmpty()) { if (!$field->isEmpty()) {
$nids = $field->getValue(); $nids = $field->getValue();

74
src/Plugin/views/filter/NodeHasMediaUse.php

@ -2,8 +2,13 @@
namespace Drupal\islandora\Plugin\views\filter; namespace Drupal\islandora\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\views\Plugin\views\filter\FilterPluginBase; use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Views Filter on Having Media of a Type. * Views Filter on Having Media of a Type.
@ -14,6 +19,48 @@ use Drupal\views\Plugin\views\filter\FilterPluginBase;
*/ */
class NodeHasMediaUse extends FilterPluginBase { 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} * {@inheritdoc}
*/ */
@ -29,7 +76,7 @@ class NodeHasMediaUse extends FilterPluginBase {
*/ */
public function validateOptionsForm(&$form, FormStateInterface $form_state) { public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$uri = $form_state->getValues()['options']['use_uri']; $uri = $form_state->getValues()['options']['use_uri'];
$term = \Drupal::service('islandora.utils')->getTermForUri($uri); $term = $this->utils->getTermForUri($uri);
if (empty($term)) { if (empty($term)) {
$form_state->setError($form['use_uri'], $this->t('Could not find term with URI: "%uri"', ['%uri' => $uri])); $form_state->setError($form['use_uri'], $this->t('Could not find term with URI: "%uri"', ['%uri' => $uri]));
} }
@ -39,7 +86,7 @@ class NodeHasMediaUse extends FilterPluginBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildOptionsForm(&$form, FormStateInterface $form_state) { 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 = []; $uris = [];
foreach ($terms as $term) { foreach ($terms as $term) {
foreach ($term->get('field_external_uri')->getValue() as $uri) { foreach ($term->get('field_external_uri')->getValue() as $uri) {
@ -67,7 +114,7 @@ class NodeHasMediaUse extends FilterPluginBase {
*/ */
public function adminSummary() { public function adminSummary() {
$operator = ($this->options['negated']) ? "does not have" : "has"; $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(); $label = (empty($term)) ? 'BROKEN TERM URI' : $term->label();
return "Node {$operator} a '{$label}' media"; return "Node {$operator} a '{$label}' media";
} }
@ -77,18 +124,21 @@ class NodeHasMediaUse extends FilterPluginBase {
*/ */
public function query() { public function query() {
$condition = ($this->options['negated']) ? 'NOT IN' : 'IN'; $condition = ($this->options['negated']) ? 'NOT IN' : 'IN';
$utils = \Drupal::service('islandora.utils'); $term = $this->utils->getTermForUri($this->options['use_uri']);
$term = $utils->getTermForUri($this->options['use_uri']);
if (empty($term)) { 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; return;
} }
$sub_query = \Drupal::database()->select('media', 'm'); $sub_query = $this->connection->select('media', 'm');
$sub_query->join('media__field_media_use', 'use', 'm.mid = use.entity_id'); $use_alias = $sub_query->join('media__field_media_use', 'use', 'm.mid = %alias.entity_id');
$sub_query->join('media__field_media_of', 'of', 'm.mid = of.entity_id'); $of_alias = $sub_query->join('media__field_media_of', 'of', 'm.mid = %alias.entity_id');
$sub_query->fields('of', ['field_media_of_target_id']) $sub_query->fields($of_alias, ['field_media_of_target_id'])
->condition('use.field_media_use_target_id', $term->id()); ->condition("{$use_alias}.field_media_use_target_id", $term->id());
$this->query->addWhere(0, 'nid', $sub_query, $condition);
/** @var \Drupal\views\Plugin\views\query\Sql $query */
$query = $this->query;
$alias = $query->ensureTable('node_field_data', $this->relationship);
$query->addWhere(0, "{$alias}.nid", $sub_query, $condition);
} }
} }

Loading…
Cancel
Save