Load join blocks primary first
authorColeman Watts <coleman@civicrm.org>
Sun, 5 Jan 2020 19:47:35 +0000 (14:47 -0500)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:21 +0000 (19:13 -0700)
ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php
ext/afform/core/Civi/Api4/Action/Afform/Prefill.php

index 416bde2d90985eaa2c8a0fbec9577e25a3d0d063..74a5da495a207dbfda4a17506179d4738bc2b185 100644 (file)
@@ -77,15 +77,10 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction {
    * @throws \API_Exception
    */
   protected static function getJoinWhereClause($mainEntityName, $joinEntityName, $mainEntityId) {
-    $joinMeta = \Civi::$statics[__CLASS__][__FUNCTION__][$joinEntityName] ?? NULL;
     $params = [];
-    if (!$joinMeta) {
-      $joinMeta = civicrm_api4($joinEntityName, 'getFields', ['checkPermissions' => FALSE, 'action' => 'create', 'select' => ['name']])->column('name');
-      \Civi::$statics[__CLASS__][__FUNCTION__][$joinEntityName] = $joinMeta;
-    }
-    if (in_array('entity_id', $joinMeta)) {
+    if (self::fieldExists($joinEntityName, 'entity_id')) {
       $params[] = ['entity_id', '=', $mainEntityId];
-      if (in_array('entity_table', $joinMeta)) {
+      if (self::fieldExists($joinEntityName, 'entity_table')) {
         $params[] = ['entity_table', '=', 'civicrm_' . _civicrm_api_get_entity_name_from_camel($mainEntityName)];
       }
     }
@@ -96,4 +91,26 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction {
     return $params;
   }
 
+  /**
+   * Check if a field exists for a given entity
+   *
+   * @param $entityName
+   * @param $fieldName
+   * @return bool
+   * @throws \API_Exception
+   */
+  public static function fieldExists($entityName, $fieldName) {
+    if (empty(\Civi::$statics[__CLASS__][__FUNCTION__][$entityName])) {
+      $getFields = \Civi\Api4\Utils\ActionUtil::getAction($entityName, 'getFields');
+      $getFields->setCheckPermissions(FALSE);
+      $getFields->setAction('create');
+      $getFields->addSelect('name');
+      if (property_exists($getFields, 'includeCustom')) {
+        $getFields->setIncludeCustom(FALSE);
+      }
+      \Civi::$statics[__CLASS__][__FUNCTION__][$entityName] = $getFields->execute()->column('name');
+    }
+    return in_array($fieldName, \Civi::$statics[__CLASS__][__FUNCTION__][$entityName]);
+  }
+
 }
index dd9ba99807983c2c8ac2947d3cd6499245f4f2e6..e4b93be85f42960a6038d5667c30db3d087600aa 100644 (file)
@@ -53,10 +53,11 @@ class Prefill extends AbstractProcessor {
       $data = ['fields' => $item];
       foreach ($entity['joins'] ?? [] as $joinEntity => $join) {
         $data['joins'][$joinEntity] = (array) civicrm_api4($joinEntity, 'get', [
-          'where' => $this->getJoinWhereClause($entity['type'], $joinEntity, $item['id']),
+          'where' => self::getJoinWhereClause($entity['type'], $joinEntity, $item['id']),
           'limit' => !empty($join['af-repeat']) ? $join['max'] ?? 0 : 1,
           'select' => array_keys($join['fields']),
           'checkPermissions' => $checkPermissions,
+          'orderBy' => self::fieldExists($joinEntity, 'is_primary') ? ['is_primary' => 'DESC'] : [],
         ]);
       }
       $this->_data[$entity['name']][] = $data;