Browse Source

IsIslandora views filter and context condition use Islandora Utils. (#881)

* IsIslandora views filter and context condition use Islandora Utils.
pull/895/head
Rosie Le Faive 2 years ago committed by GitHub
parent
commit
7bca3d5675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      islandora.views.inc
  2. 38
      src/Plugin/Condition/NodeIsIslandoraObject.php
  3. 144
      src/Plugin/views/filter/NodeIsIslandora.php

12
islandora.views.inc

@ -37,4 +37,16 @@ function islandora_views_data_alter(&$data) {
'id' => 'islandora_node_has_media_use', 'id' => 'islandora_node_has_media_use',
], ],
]; ];
// Add Is Islandora filter.
$data['node_field_data']['islandora_node_is_islandora'] = [
'title' => t('Node is Islandora'),
'group' => t('Content'),
'filter' => [
'title' => t('Node is Islandora'),
'help' => t('Node has a content type that possesses the mandatory Islandora fields.'),
'field' => 'nid',
'id' => 'islandora_node_is_islandora',
],
];
} }

38
src/Plugin/Condition/NodeIsIslandoraObject.php

@ -5,13 +5,14 @@ namespace Drupal\islandora\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Islandora\IslandoraUtils;
/** /**
* Checks whether node has fields that qualify it as an "Islandora" node. * Checks whether node has fields that qualify it as an "Islandora" node.
* *
* @Condition( * @Condition(
* id = "node_is_islandora_object", * id = "node_is_islandora_object",
* label = @Translation("Node is an Islandora object"), * label = @Translation("Node is an Islandora node"),
* context_definitions = { * context_definitions = {
* "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node")) * "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node"))
* } * }
@ -19,6 +20,30 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/ */
class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFactoryPluginInterface { class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFactoryPluginInterface {
/**
* Islandora Utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
* Constructs a Node is Islandora Condition plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\islandora\IslandoraUtils $islandora_utils
* Islandora utilities.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, IslandoraUtils $islandora_utils) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->utils = $islandora_utils;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -26,7 +51,8 @@ class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFact
return new static( return new static(
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition $plugin_definition,
$container->get('islandora.utils')
); );
} }
@ -38,8 +64,8 @@ class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFact
if (!$node) { if (!$node) {
return FALSE; return FALSE;
} }
// Islandora objects have these two fields. // Determine if node is Islandora.
if ($node->hasField('field_model') && $node->hasField('field_member_of')) { if ($this->utils->isIslandoraType('node', $node->bundle())) {
return TRUE; return TRUE;
} }
} }
@ -49,10 +75,10 @@ class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFact
*/ */
public function summary() { public function summary() {
if (!empty($this->configuration['negate'])) { if (!empty($this->configuration['negate'])) {
return $this->t('The node is not an Islandora object.'); return $this->t('The node is not an Islandora node.');
} }
else { else {
return $this->t('The node is an Islandora object.'); return $this->t('The node is an Islandora node.');
} }
} }

144
src/Plugin/views/filter/NodeIsIslandora.php

@ -0,0 +1,144 @@
<?php
namespace Drupal\islandora\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\Plugin\ViewsHandlerManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
/**
* Views Filter to show only Islandora nodes.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("islandora_node_is_islandora")
*/
class NodeIsIslandora extends FilterPluginBase implements ContainerFactoryPluginInterface {
/**
* Views Handler Plugin Manager.
*
* @var \Drupal\views\Plugin\ViewsHandlerManager
*/
protected $joinHandler;
/**
* Islandora Utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
* The entity type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Constructs a Node is Islandora views filter plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\views\Plugin\ViewsHandlerManager $join_handler
* Views Handler Plugin Manager.
* @param \Drupal\islandora\IslandoraUtils $islandora_utils
* Islandora utilities.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ViewsHandlerManager $join_handler, IslandoraUtils $islandora_utils, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->joinHandler = $join_handler;
$this->utils = $islandora_utils;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration, $plugin_id, $plugin_definition,
$container->get('plugin.manager.views.join'),
$container->get('islandora.utils'),
$container->get('entity_type.bundle.info')
);
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
return [
'negated' => ['default' => FALSE],
];
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$types = [];
foreach ($this->entityTypeBundleInfo->getBundleInfo('node') as $bundle_id => $bundle) {
if ($this->utils->isIslandoraType('node', $bundle_id)) {
$types[] = "{$bundle['label']} ($bundle_id)";
}
}
$types_list = implode(', ', $types);
$form['info'] = [
'#type' => 'item',
'#title' => 'Information',
'#description' => t("Configured Islandora bundles: @types", ['@types' => $types_list]),
];
$form['negated'] = [
'#type' => 'checkbox',
'#title' => 'Negated',
'#description' => $this->t("Return nodes that <em>don't</em> have islandora fields"),
'#default_value' => $this->options['negated'],
];
}
/**
* {@inheritdoc}
*/
public function adminSummary() {
$operator = ($this->options['negated']) ? "is not" : "is";
return "Node {$operator} an islandora node";
}
/**
* {@inheritdoc}
*/
public function query() {
$types = [];
foreach (array_keys($this->entityTypeBundleInfo->getBundleInfo('node')) as $bundle_id) {
if ($this->utils->isIslandoraType('node', $bundle_id)) {
$types[] = $bundle_id;
}
}
$condition = ($this->options['negated']) ? 'NOT IN' : 'IN';
$query_base_table = $this->relationship ?: $this->view->storage->get('base_table');
$definition = [
'table' => 'node',
'type' => 'LEFT',
'field' => 'nid',
'left_table' => $query_base_table,
'left_field' => 'nid',
];
$join = $this->joinHandler->createInstance('standard', $definition);
$node_table_alias = $this->query->addTable('node', $this->relationship, $join);
$this->query->addWhere($this->options['group'], "$node_table_alias.type", $types, $condition);
}
}
Loading…
Cancel
Save