From f2ba51c100a658bfda2104957b1d0765bc1da9c7 Mon Sep 17 00:00:00 2001
From: Jaap Jansma <jaap.jansma@civicoop.org>
Date: Fri, 22 Nov 2019 16:38:20 +0100
Subject: [PATCH] Fixed issue with aggregate fields

---
 .../AggregateFunctionFieldOutputHandler.php   |  5 +++-
 .../Source/AbstractCivicrmEntitySource.php    | 26 +++++++++++--------
 2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/Civi/DataProcessor/FieldOutputHandler/AggregateFunctionFieldOutputHandler.php b/Civi/DataProcessor/FieldOutputHandler/AggregateFunctionFieldOutputHandler.php
index 72717c03..14e56869 100644
--- a/Civi/DataProcessor/FieldOutputHandler/AggregateFunctionFieldOutputHandler.php
+++ b/Civi/DataProcessor/FieldOutputHandler/AggregateFunctionFieldOutputHandler.php
@@ -86,7 +86,10 @@ class AggregateFunctionFieldOutputHandler extends AbstractSimpleFieldOutputHandl
     if (!$this->dataSource) {
       throw new DataSourceNotFoundException(E::ts("Field %1 requires data source '%2' which could not be found. Did you rename or deleted the data source?", array(1=>$title, 2=>$configuration['datasource'])));
     }
-    $this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
+    $this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByAlias($configuration['field']);
+    if (!$this->inputFieldSpec) {
+      $this->inputFieldSpec = $this->dataSource->getAvailableFields()->getFieldSpecificationByName($configuration['field']);
+    }
     if (!$this->inputFieldSpec) {
       throw new FieldNotFoundException(E::ts("Field %1 requires a field with the name '%2' in the data source '%3'. Did you change the data source type?", array(
         1 => $title,
diff --git a/Civi/DataProcessor/Source/AbstractCivicrmEntitySource.php b/Civi/DataProcessor/Source/AbstractCivicrmEntitySource.php
index 2d580710..db9e1aca 100644
--- a/Civi/DataProcessor/Source/AbstractCivicrmEntitySource.php
+++ b/Civi/DataProcessor/Source/AbstractCivicrmEntitySource.php
@@ -327,22 +327,26 @@ abstract class AbstractCivicrmEntitySource extends AbstractSource {
    */
   public function ensureFieldInSource(FieldSpecification $fieldSpecification) {
     try {
+      $originalFieldSpecification = null;
       if ($this->getAvailableFields()->doesAliasExists($fieldSpecification->alias)) {
         $originalFieldSpecification = $this->getAvailableFields()->getFieldSpecificationByAlias($fieldSpecification->alias);
-        if ($originalFieldSpecification instanceof CustomFieldSpecification) {
-          $customGroupDataFlow = $this->ensureCustomGroup($originalFieldSpecification->customGroupTableName, $originalFieldSpecification->customGroupName);
-          if (!$customGroupDataFlow->getDataSpecification()
-            ->doesFieldExist($fieldSpecification->alias)) {
-            $customGroupDataFlow->getDataSpecification()
-              ->addFieldSpecification($fieldSpecification->alias, $fieldSpecification);
-          }
-        }
-        else {
-          $entityDataFlow = $this->ensureEntity();
-          $entityDataFlow->getDataSpecification()
+      } elseif ($this->getAvailableFields()->doesFieldExist($fieldSpecification->name)) {
+        $originalFieldSpecification = $this->getAvailableFields()
+          ->getFieldSpecificationByName($fieldSpecification->name);
+      }
+      if ($originalFieldSpecification && $originalFieldSpecification instanceof CustomFieldSpecification) {
+        $customGroupDataFlow = $this->ensureCustomGroup($originalFieldSpecification->customGroupTableName, $originalFieldSpecification->customGroupName);
+        if (!$customGroupDataFlow->getDataSpecification()
+          ->doesFieldExist($fieldSpecification->alias)) {
+          $customGroupDataFlow->getDataSpecification()
             ->addFieldSpecification($fieldSpecification->alias, $fieldSpecification);
         }
       }
+      elseif ($originalFieldSpecification) {
+        $entityDataFlow = $this->ensureEntity();
+        $entityDataFlow->getDataSpecification()
+          ->addFieldSpecification($fieldSpecification->alias, $fieldSpecification);
+      }
     } catch (FieldExistsException $e) {
       // Do nothing.
     }
-- 
GitLab