Use select2 to display field mappings
authorColeman Watts <coleman@civicrm.org>
Thu, 11 Jul 2019 14:07:13 +0000 (10:07 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 11 Jul 2019 14:07:13 +0000 (10:07 -0400)
CRM/Core/BAO/Mapping.php
CRM/Export/Form/Select.php

index d740ce2a2723c5dc96b55dd405b700e6c92e0950..0f5be86454f2c912d85a8d8feabbdee5f7c9a85a 100644 (file)
@@ -106,17 +106,20 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping {
   }
 
   /**
-   * Get the list of mappings.
+   * Get the list of mappings for a select or select2 element.
    *
    * @param string $mappingType
    *   Mapping type name.
+   * @param bool $select2
+   *   Format for select2
    *
    * @return array
    *   Array of mapping names, keyed by id.
    */
-  public static function getMappings($mappingType) {
+  public static function getMappings($mappingType, $select2 = FALSE) {
     $result = civicrm_api3('Mapping', 'get', [
       'mapping_type_id' => $mappingType,
+      'return' => ['name', 'description'],
       'options' => [
         'sort' => 'name',
         'limit' => 0,
@@ -124,8 +127,17 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping {
     ]);
     $mapping = [];
 
-    foreach ($result['values'] as $key => $value) {
-      $mapping[$key] = $value['name'];
+    foreach ($result['values'] as $id => $value) {
+      if ($select2) {
+        $item = ['id' => $id, 'text' => $value['name']];
+        if (!empty($value['description'])) {
+          $item['description'] = $value['description'];
+        }
+        $mapping[] = $item;
+      }
+      else {
+        $mapping[$id] = $value['name'];
+      }
     }
     return $mapping;
   }
index 315c622cb146d3c824729b10c0686e393204b2f5..c2a97d0acb34d6be1a3d10395b195e2b927a22f1 100644 (file)
@@ -466,9 +466,9 @@ FROM   {$this->_componentTable}
 
     $this->set('mappingTypeId', CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', $exportType));
 
-    $mappings = CRM_Core_BAO_Mapping::getMappings($exportType);
+    $mappings = CRM_Core_BAO_Mapping::getMappings($exportType, TRUE);
     if (!empty($mappings)) {
-      $this->add('select', 'mapping', ts('Use Saved Field Mapping'), ['' => '-select-'] + $mappings);
+      $this->add('select2', 'mapping', ts('Use Saved Field Mapping'), $mappings, FALSE, ['placeholder' => ts('- select -')]);
     }
   }