APIv3 - Legacy support for campaign_id fields which used to have a pseudoconstant
authoreileen <emcnaughton@wikimedia.org>
Fri, 19 Feb 2021 06:18:04 +0000 (19:18 +1300)
committerColeman Watts <coleman@civicrm.org>
Mon, 22 Feb 2021 02:13:29 +0000 (21:13 -0500)
api/v3/Generic.php
api/v3/utils.php
tests/phpunit/api/v3/MailingTest.php

index 0eef89a7a2f6114876af58579cf4df0607e48c75..586d34f5b98ecff1909ac5fdf9cf4a7867ebcac6 100644 (file)
@@ -419,8 +419,23 @@ function civicrm_api3_generic_getoptions($apiRequest) {
   CRM_Core_DAO::buildOptionsContext($context);
   unset($apiRequest['params']['context'], $apiRequest['params']['field'], $apiRequest['params']['condition']);
 
-  $baoName = _civicrm_api3_get_BAO($apiRequest['entity']);
-  $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
+  // Legacy support for campaign_id fields which used to have a pseudoconstant
+  if ($fieldName === 'campaign_id') {
+    $campaignParams = [
+      'select' => ['id', 'name', 'title'],
+      'options' => ['limit' => 0],
+    ];
+    if ($context === 'match' || $context === 'create') {
+      $campaignParams['is_active'] = 1;
+    }
+    $labelField = $context === 'validate' ? 'name' : 'title';
+    $keyField = $context === 'match' ? 'name' : 'id';
+    $options = array_column(civicrm_api3('Campaign', 'get', $campaignParams)['values'], $labelField, $keyField);
+  }
+  else {
+    $baoName = _civicrm_api3_get_BAO($apiRequest['entity']);
+    $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
+  }
   if ($options === FALSE) {
     return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list.");
   }
index 6b8712d44bb09af2ebc8fd6d989477e5c4deaa9a..f0db7003173a142af4fd19586453cf757e99f5cf 100644 (file)
@@ -2062,7 +2062,7 @@ function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $enti
         $fieldValue = NULL;
       }
     }
-    if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
+    if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options']) || $fieldName === 'campaign_id') {
       $additional_lookup_params = [];
       if (strtolower($entity) == 'address' && $fieldName == 'state_province_id') {
         $country_id = _civicrm_api3_resolve_country_id($params);
index 748a73278a8a56c648f5e73bb79a7d559d151d59..bbf99310abdc47c03ade561fde28fe91232f22f6 100644 (file)
@@ -79,8 +79,9 @@ class api_v3_MailingTest extends CiviUnitTestCase {
   /**
    * Test civicrm_mailing_create.
    */
-  public function testMailerCreateSuccess() {
-    $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + ['scheduled_date' => 'now'], __FUNCTION__, __FILE__);
+  public function testMailerCreateSuccess(): void {
+    $this->callAPISuccess('Campaign', 'create', ['name' => 'big campaign', 'title' => 'abc']);
+    $result = $this->callAPIAndDocument('mailing', 'create', $this->_params + ['scheduled_date' => 'now', 'campaign_id' => 'big campaign'], __FUNCTION__, __FILE__);
     $jobs = $this->callAPISuccess('mailing_job', 'get', ['mailing_id' => $result['id']]);
     $this->assertEquals(1, $jobs['count']);
     // return isn't working on this in getAndCheck so lets not check it for now