From 69b5688ed2040e5e3cabcf48bafa0af1ab13512a Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Mon, 11 Jul 2022 20:49:51 -0300 Subject: [PATCH 1/6] IsIslandora views filter and context condition use Islandora Utils. --- islandora.views.inc | 12 ++ .../Condition/NodeIsIslandoraObject.php | 13 +- src/Plugin/views/filter/NodeIsIslandora.php | 121 ++++++++++++++++++ 3 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 src/Plugin/views/filter/NodeIsIslandora.php diff --git a/islandora.views.inc b/islandora.views.inc index 574fa4155..97dbf08c1 100644 --- a/islandora.views.inc +++ b/islandora.views.inc @@ -37,4 +37,16 @@ function islandora_views_data_alter(&$data) { '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', + ], + ]; } diff --git a/src/Plugin/Condition/NodeIsIslandoraObject.php b/src/Plugin/Condition/NodeIsIslandoraObject.php index 6448cfdcb..4e4689d72 100644 --- a/src/Plugin/Condition/NodeIsIslandoraObject.php +++ b/src/Plugin/Condition/NodeIsIslandoraObject.php @@ -5,13 +5,14 @@ use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Islandora\IslandoraUtils; /** * Checks whether node has fields that qualify it as an "Islandora" node. * * @Condition( * id = "node_is_islandora_object", - * label = @Translation("Node is an Islandora object"), + * label = @Translation("Node is an Islandora node"), * context_definitions = { * "node" = @ContextDefinition("entity:node", required = TRUE , label = @Translation("node")) * } @@ -19,6 +20,7 @@ */ class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFactoryPluginInterface { + /** * {@inheritdoc} */ @@ -38,8 +40,9 @@ public function evaluate() { if (!$node) { return FALSE; } - // Islandora objects have these two fields. - if ($node->hasField('field_model') && $node->hasField('field_member_of')) { + // Islandora objects are determined by Islandora Utils. + $utils = \Drupal::service('islandora.utils'); + if ($utils->isIslandoraType('node',$node->bundle())) { return TRUE; } } @@ -49,10 +52,10 @@ public function evaluate() { */ public function summary() { 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 { - return $this->t('The node is an Islandora object.'); + return $this->t('The node is an Islandora node.'); } } diff --git a/src/Plugin/views/filter/NodeIsIslandora.php b/src/Plugin/views/filter/NodeIsIslandora.php new file mode 100644 index 000000000..6b5aa43db --- /dev/null +++ b/src/Plugin/views/filter/NodeIsIslandora.php @@ -0,0 +1,121 @@ +joinHandler = $join_handler; + } + + /** + * {@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') + ); + } + + /** + * {@inheritdoc} + */ + protected function defineOptions() { + return [ + 'negated' => ['default' => FALSE], + ]; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + $types = []; + $utils = \Drupal::service('islandora.utils'); + foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo('node') as $bundle_id => $bundle) { + if ($utils->isIslandoraType('node', $bundle_id)) { + $types[] = $bundle['label'] . ' (' . $bundle_id . ')' ; + } + } + $form['info'] = [ + '#type' => 'item', + '#title' => 'Information', + '#description' => t("Configured Islandora bundles: ") . implode(', ', $types), + ]; + $form['negated'] = [ + '#type' => 'checkbox', + '#title' => 'Negated', + '#description' => $this->t("Return nodes that don't 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 = []; + $utils = \Drupal::service('islandora.utils'); + foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('node')) as $bundle_id) { + if ($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); + } + +} From d2fd78ad5179a14f6890dfa9c0eef65b710c0f04 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Wed, 13 Jul 2022 20:36:52 -0300 Subject: [PATCH 2/6] Dependency injection. --- src/Plugin/views/filter/NodeIsIslandora.php | 57 +++++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/Plugin/views/filter/NodeIsIslandora.php b/src/Plugin/views/filter/NodeIsIslandora.php index 6b5aa43db..0c1741d25 100644 --- a/src/Plugin/views/filter/NodeIsIslandora.php +++ b/src/Plugin/views/filter/NodeIsIslandora.php @@ -7,6 +7,8 @@ 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. @@ -18,12 +20,26 @@ class NodeIsIslandora extends FilterPluginBase implements ContainerFactoryPluginInterface { /** - * Views Handler Plugin Manager. - * - * @var \Drupal\views\Plugin\ViewsHandlerManager - */ + * 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. * @@ -35,19 +51,27 @@ class NodeIsIslandora extends FilterPluginBase implements ContainerFactoryPlugin * 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) { + 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('plugin.manager.views.join'), + $container->get('islandora.utils'), + $container->get('entity_type.bundle.info') ); } @@ -65,11 +89,10 @@ protected function defineOptions() { */ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $types = []; - $utils = \Drupal::service('islandora.utils'); - foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo('node') as $bundle_id => $bundle) { - if ($utils->isIslandoraType('node', $bundle_id)) { - $types[] = $bundle['label'] . ' (' . $bundle_id . ')' ; - } + foreach ($this->entityTypeBundleInfo->getBundleInfo('node') as $bundle_id => $bundle) { + if ($this->utils->isIslandoraType('node', $bundle_id)) { + $types[] = $bundle['label'] . ' (' . $bundle_id . ')' ; + } } $form['info'] = [ '#type' => 'item', @@ -97,11 +120,10 @@ public function adminSummary() { */ public function query() { $types = []; - $utils = \Drupal::service('islandora.utils'); - foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('node')) as $bundle_id) { - if ($utils->isIslandoraType('node', $bundle_id)) { - $types[] = $bundle_id; - } + 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'); @@ -119,3 +141,4 @@ public function query() { } } + From 4266ab147a3cc8ad5c41e10b736ec051a8a6438c Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Thu, 14 Jul 2022 11:10:10 -0300 Subject: [PATCH 3/6] Coder. --- .../Condition/NodeIsIslandoraObject.php | 33 ++++++++++++++++--- src/Plugin/views/filter/NodeIsIslandora.php | 6 ++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Plugin/Condition/NodeIsIslandoraObject.php b/src/Plugin/Condition/NodeIsIslandoraObject.php index 4e4689d72..0a212aafe 100644 --- a/src/Plugin/Condition/NodeIsIslandoraObject.php +++ b/src/Plugin/Condition/NodeIsIslandoraObject.php @@ -20,6 +20,31 @@ */ 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. + * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info + * The entity type bundle service. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, IslandoraUtils $islandora_utils) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->utils = $islandora_utils; + } /** * {@inheritdoc} @@ -28,7 +53,8 @@ public static function create(ContainerInterface $container, array $configuratio return new static( $configuration, $plugin_id, - $plugin_definition + $plugin_definition, + $container->get('islandora.utils') ); } @@ -40,9 +66,8 @@ public function evaluate() { if (!$node) { return FALSE; } - // Islandora objects are determined by Islandora Utils. - $utils = \Drupal::service('islandora.utils'); - if ($utils->isIslandoraType('node',$node->bundle())) { + // Determine if node is Islandora. + if ($this->utils->isIslandoraType('node', $node->bundle())) { return TRUE; } } diff --git a/src/Plugin/views/filter/NodeIsIslandora.php b/src/Plugin/views/filter/NodeIsIslandora.php index 0c1741d25..a94da147d 100644 --- a/src/Plugin/views/filter/NodeIsIslandora.php +++ b/src/Plugin/views/filter/NodeIsIslandora.php @@ -91,13 +91,14 @@ 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[] = "{$bundle['label']} ($bundle_id)"; } } + $types_list = implode(', ', $types); $form['info'] = [ '#type' => 'item', '#title' => 'Information', - '#description' => t("Configured Islandora bundles: ") . implode(', ', $types), + '#description' => t("Configured Islandora bundles: {$types_list} "), ]; $form['negated'] = [ '#type' => 'checkbox', @@ -139,6 +140,5 @@ public function query() { $node_table_alias = $this->query->addTable('node', $this->relationship, $join); $this->query->addWhere($this->options['group'], "$node_table_alias.type", $types, $condition); } - } From fc424c81f156226cb02c4ebb797c6e4595040321 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Thu, 14 Jul 2022 16:19:03 -0300 Subject: [PATCH 4/6] coding standards. --- src/Plugin/Condition/NodeIsIslandoraObject.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Plugin/Condition/NodeIsIslandoraObject.php b/src/Plugin/Condition/NodeIsIslandoraObject.php index 0a212aafe..06ff62596 100644 --- a/src/Plugin/Condition/NodeIsIslandoraObject.php +++ b/src/Plugin/Condition/NodeIsIslandoraObject.php @@ -26,8 +26,8 @@ class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFact * @var \Drupal\islandora\IslandoraUtils */ protected $utils; - - /** + + /** * Constructs a Node is Islandora Condition plugin. * * @param array $configuration @@ -38,8 +38,6 @@ class NodeIsIslandoraObject extends ConditionPluginBase implements ContainerFact * The plugin implementation definition. * @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, IslandoraUtils $islandora_utils) { parent::__construct($configuration, $plugin_id, $plugin_definition); From b91a29b64fdbc7b1dd58e13abc668c62ccc6329c Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Fri, 15 Jul 2022 17:25:36 -0300 Subject: [PATCH 5/6] coder --- src/Plugin/views/filter/NodeIsIslandora.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Plugin/views/filter/NodeIsIslandora.php b/src/Plugin/views/filter/NodeIsIslandora.php index a94da147d..a1f1585c6 100644 --- a/src/Plugin/views/filter/NodeIsIslandora.php +++ b/src/Plugin/views/filter/NodeIsIslandora.php @@ -98,7 +98,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $form['info'] = [ '#type' => 'item', '#title' => 'Information', - '#description' => t("Configured Islandora bundles: {$types_list} "), + '#description' => t("Configured Islandora bundles: @types", ['@types' => $types_list]), ]; $form['negated'] = [ '#type' => 'checkbox', @@ -140,5 +140,6 @@ public function query() { $node_table_alias = $this->query->addTable('node', $this->relationship, $join); $this->query->addWhere($this->options['group'], "$node_table_alias.type", $types, $condition); } + } From 8756e1c40bebd2c8c8a9091f2fc77e06ec10e081 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 19 Jul 2022 09:16:47 -0300 Subject: [PATCH 6/6] Coder, finally. --- src/Plugin/views/filter/NodeIsIslandora.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/views/filter/NodeIsIslandora.php b/src/Plugin/views/filter/NodeIsIslandora.php index a1f1585c6..2065c46b9 100644 --- a/src/Plugin/views/filter/NodeIsIslandora.php +++ b/src/Plugin/views/filter/NodeIsIslandora.php @@ -142,4 +142,3 @@ public function query() { } } -