Browse Source

created sapi processor to unindex unused taxo terms, added term id condition on sapi query

bach 3 years ago
parent
commit
182b61b08f

+ 3 - 0
config/sync/search_api.index.autocomplete.yml

@@ -7,6 +7,7 @@ dependencies:
     - taxonomy
     - synonyms
     - search_api
+    - materio_sapi
   config:
     - search_api.server.database_search_autocomplete
 third_party_settings:
@@ -60,6 +61,7 @@ field_settings:
     datasource_id: 'entity:taxonomy_term'
     property_path: name
     type: 'solr_text_custom:ngram'
+    boost: !!float 2
     dependencies:
       module:
         - taxonomy
@@ -108,6 +110,7 @@ processor_settings:
   solr_date_range:
     weights:
       preprocess_index: 0
+  term_used: {  }
   type_boost:
     boosts:
       'entity:taxonomy_term':

+ 2 - 4
config/sync/search_api.index.database.yml

@@ -183,7 +183,7 @@ field_settings:
     label: 'Tags » Taxonomy term » Term ID'
     datasource_id: 'entity:node'
     property_path: 'field_tags:entity:tid'
-    type: solr_text_wstoken
+    type: integer
     boost: !!float 8
     dependencies:
       config:
@@ -217,7 +217,7 @@ field_settings:
     label: 'Thesaurus » Taxonomy term » Term ID'
     datasource_id: 'entity:node'
     property_path: 'field_thesaurus:entity:tid'
-    type: solr_text_wstoken
+    type: integer
     boost: !!float 8
     dependencies:
       config:
@@ -300,10 +300,8 @@ processor_settings:
       - processed
       - tag_name
       - tag_synonyms
-      - tag_tid
       - thesaurus_name
       - thesaurus_synonyms
-      - thesaurus_tid
       - title
       - uuid
     title: true

+ 45 - 9
config/sync/views.view.admin_taxo.yml

@@ -1047,6 +1047,7 @@ display:
         operator: AND
         groups:
           1: AND
+      group_by: false
     cache_metadata:
       max-age: 0
       contexts:
@@ -3205,6 +3206,7 @@ display:
         filters: false
         filter_groups: false
         title: false
+        relationships: false
       row:
         type: fields
         options: {  }
@@ -3332,7 +3334,7 @@ display:
           click_sort_column: value
           type: number_unformatted
           settings: {  }
-          group_column: value
+          group_column: entity_id
           group_columns: {  }
           group_rows: true
           delta_limit: 0
@@ -3808,6 +3810,7 @@ display:
         context: '0'
         menu_name: main
       title: Thesaurus
+      relationships: {  }
     cache_metadata:
       max-age: 0
       contexts:
@@ -3915,6 +3918,7 @@ display:
         filters: false
         filter_groups: false
         title: false
+        sorts: false
       row:
         type: fields
         options: {  }
@@ -4353,14 +4357,14 @@ display:
           type_custom_false: ''
           not: false
           plugin_id: boolean
-        langcode:
-          id: langcode
-          table: content_lock
-          field: langcode
+        changed:
+          id: changed
+          table: taxonomy_term_field_revision
+          field: changed
           relationship: none
           group_type: group
           admin_label: ''
-          label: 'Lock Language'
+          label: Modifié
           exclude: false
           alter:
             alter_text: false
@@ -4401,9 +4405,25 @@ display:
           hide_empty: false
           empty_zero: false
           hide_alter_empty: true
-          native_language: false
-          entity_field: langcode
-          plugin_id: language
+          click_sort_column: value
+          type: timestamp
+          settings:
+            date_format: short
+            custom_date_format: ''
+            timezone: ''
+          group_column: value
+          group_columns: {  }
+          group_rows: true
+          delta_limit: 0
+          delta_offset: 0
+          delta_reversed: false
+          delta_first_last: false
+          multi_type: separator
+          separator: ', '
+          field_api_classes: false
+          entity_type: taxonomy_term
+          entity_field: changed
+          plugin_id: field
       filters:
         vid:
           id: vid
@@ -4518,6 +4538,22 @@ display:
         context: '0'
         menu_name: main
       title: Tags
+      sorts:
+        changed:
+          id: changed
+          table: taxonomy_term_field_revision
+          field: changed
+          relationship: none
+          group_type: group
+          admin_label: ''
+          order: ASC
+          exposed: false
+          expose:
+            label: Modifié
+          granularity: second
+          entity_type: taxonomy_term
+          entity_field: changed
+          plugin_id: date
     cache_metadata:
       max-age: 0
       contexts:

+ 17 - 3
web/modules/custom/materio_sapi/src/Controller/Base.php

@@ -40,11 +40,25 @@ class Base extends ControllerBase {
     // Set additional conditions.
     // in case we search for material references like W0117
     if (preg_match_all('/[WTRPCMFGSO]\d{4}/i', $this->keys, $matches)) {
-      $conditions = $this->query->createConditionGroup('OR');
+      $ref_conditions = $this->query->createConditionGroup('OR');
       foreach ($matches[0] as $key => $value) {
-        $conditions->addCondition('field_reference', $value);
+        $ref_conditions->addCondition('field_reference', $value);
       }
-      $this->query->addConditionGroup($conditions);
+      $this->query->addConditionGroup($ref_conditions);
+    }
+    // in case of term id provided restrict the keys to taxo fields
+    if ($this->term) {
+      $term_conditions = $this->query->createConditionGroup('OR');
+      $term = (int) $this->term;
+      foreach (['tag_tid', 'thesaurus_tid'] as $field) {
+        $term_conditions->addCondition($field, $term);
+      }
+      // foreach (['tag_name', 'thesaurus_name'] as $field) {
+      //   $term_conditions->addCondition($field, $this->keys);
+      // }
+      // $term_conditions->addCondition('tag_name', $this->keys);
+      // $term_conditions->addCondition('thesaurus_name', $this->keys);
+      $this->query->addConditionGroup($term_conditions);
     }
 
     // Restrict the search to specific languages.

+ 78 - 0
web/modules/custom/materio_sapi/src/Plugin/search_api/processor/TermUsed.php

@@ -0,0 +1,78 @@
+<?php
+
+namespace Drupal\materio_sapi\Plugin\search_api\processor;
+
+use Drupal\Core\Entity\EntityPublishedInterface;
+use Drupal\search_api\IndexInterface;
+use Drupal\search_api\Processor\ProcessorPluginBase;
+use Drupal\user\UserInterface;
+
+/**
+ * Excludes unused terms from indexes.
+ *
+ * @SearchApiProcessor(
+ *   id = "term_used",
+ *   label = @Translation("Term used"),
+ *   description = @Translation("Exclude unused taxonomy terms from being indexed."),
+ *   stages = {
+ *     "alter_items" = 0,
+ *   },
+ * )
+ */
+class TermUsed extends ProcessorPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function supportsIndex(IndexInterface $index) {
+    $interface = EntityPublishedInterface::class;
+    foreach ($index->getDatasources() as $datasource) {
+      $entity_type_id = $datasource->getEntityTypeId();
+      if (!$entity_type_id) {
+        continue;
+      }
+      // We support users and any entities that implement
+      // \Drupal\Core\Entity\EntityPublishedInterface.
+      if ($entity_type_id === 'taxonomy_term') {
+        return TRUE;
+      }
+      // $entity_type = \Drupal::entityTypeManager()
+      //   ->getDefinition($entity_type_id);
+      // if ($entity_type && $entity_type->entityClassImplements($interface)) {
+      //   return TRUE;
+      // }
+    }
+    return FALSE;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function alterIndexedItems(array &$items) {
+    // Annoyingly, this doc comment is needed for PHPStorm. See
+    // http://youtrack.jetbrains.com/issue/WI-23586
+    /** @var \Drupal\search_api\Item\ItemInterface $item */
+    foreach ($items as $item_id => $item) {
+      $term = $item->getOriginalObject()->getValue();
+      $enabled = TRUE;
+      // $entity_storage = \Drupal::entityTypeManager()->getStorage('node');
+      // $count_query = $entity_storage->getQuery()
+      //   ->condition('type', 'materiau')
+      //   ->accessCheck(TRUE)
+      //   ->condition('status', '1')
+      //   ->count();
+      //
+      // $this->count = $this->count_query->execute();
+      $query = \Drupal::database()->select('taxonomy_index', 'ti');
+      $query->fields('ti', array('nid'));
+      $query->condition('ti.tid', $term->id());
+      $query->distinct(TRUE);
+      $result = $query->execute();
+      $nodeIds = $result->fetchCol();
+      if (!$nodeIds) {
+        unset($items[$item_id]);
+      }
+    }
+  }
+
+}