SearchKit - Change @searchable annotation from boolean to option list
[civicrm-core.git] / ext / search_kit / Civi / Search / Admin.php
index ad606704f45afc6c53c0068c4d7c116ae508ed89..9d314a8118f3bb381a74b4ffe78923fc1682b249 100644 (file)
@@ -82,8 +82,8 @@ class Admin {
   public static function getSchema() {
     $schema = [];
     $entities = \Civi\Api4\Entity::get()
-      ->addSelect('name', 'title', 'type', 'title_plural', 'description', 'label_field', 'icon', 'paths', 'dao', 'bridge', 'ui_join_filters')
-      ->addWhere('searchable', '=', TRUE)
+      ->addSelect('name', 'title', 'type', 'title_plural', 'description', 'label_field', 'icon', 'paths', 'dao', 'bridge', 'ui_join_filters', 'searchable')
+      ->addWhere('searchable', '!=', 'none')
       ->addOrderBy('title_plural')
       ->setChain([
         'get' => ['$name', 'getActions', ['where' => [['name', '=', 'get']]], ['params']],
@@ -190,6 +190,7 @@ class Admin {
         });
         $fields = array_column($entity['fields'], NULL, 'name');
         $bridge = in_array('EntityBridge', $entity['type']) ? $entity['name'] : NULL;
+        $bridgeFields = array_keys($entity['bridge'] ?? []);
         foreach ($references as $reference) {
           $keyField = $fields[$reference->getReferenceKey()] ?? NULL;
           if (
@@ -198,7 +199,7 @@ class Admin {
             // Exclude any joins that are better represented by pseudoconstants
             is_a($reference, 'CRM_Core_Reference_OptionValue') || (!$bridge && !empty($keyField['options'])) ||
             // Limit bridge joins to just the first
-            ($bridge && array_search($keyField['name'], $entity['bridge']) !== 0) ||
+            ($bridge && array_search($keyField['name'], $bridgeFields) !== 0) ||
             // Sanity check - table should match
             $daoClass::getTableName() !== $reference->getReferenceTable()
           ) {
@@ -240,7 +241,7 @@ class Admin {
             // Bridge joins (sanity check - bridge must specify exactly 2 FK fields)
             elseif (count($entity['bridge']) === 2) {
               // Get the other entity being linked through this bridge
-              $baseKey = array_search($reference->getReferenceKey(), $entity['bridge']) ? $entity['bridge'][0] : $entity['bridge'][1];
+              $baseKey = array_search($reference->getReferenceKey(), $bridgeFields) ? $bridgeFields[0] : $bridgeFields[1];
               $baseEntity = $allowedEntities[$fields[$baseKey]['fk_entity']] ?? NULL;
               if (!$baseEntity) {
                 continue;
@@ -251,7 +252,7 @@ class Admin {
               $alias = $baseEntity['name'] . "_{$bridge}_" . $targetEntityName;
               $joins[$baseEntity['name']][] = [
                 'label' => $baseEntity['title'] . ' ' . $targetsTitle,
-                'description' => E::ts('Multiple %1 per %2', [1 => $targetsTitle, 2 => $baseEntity['title']]),
+                'description' => $entity['bridge'][$baseKey]['description'] ?? E::ts('Multiple %1 per %2', [1 => $targetsTitle, 2 => $baseEntity['title']]),
                 'entity' => $targetEntityName,
                 'conditions' => array_merge(
                   [$bridge],
@@ -266,7 +267,7 @@ class Admin {
                 $alias = $targetEntityName . "_{$bridge}_" . $baseEntity['name'];
                 $joins[$targetEntityName][] = [
                   'label' => $targetEntity['title'] . ' ' . $baseEntity['title_plural'],
-                  'description' => E::ts('Multiple %1 per %2', [1 => $baseEntity['title_plural'], 2 => $targetEntity['title']]),
+                  'description' => $entity['bridge'][$reference->getReferenceKey()]['description'] ?? E::ts('Multiple %1 per %2', [1 => $baseEntity['title_plural'], 2 => $targetEntity['title']]),
                   'entity' => $baseEntity['name'],
                   'conditions' => array_merge(
                     [$bridge],