Skip to content

Commit

Permalink
ImportTemplateField - Add pseudoconstants
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jan 6, 2025
1 parent abee4a6 commit 1dbd8e4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
66 changes: 66 additions & 0 deletions CRM/Core/BAO/ImportTemplateField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\Utils\CoreUtil;

/**
* Logic for ImportTemplateField entity
*/
class CRM_Core_BAO_ImportTemplateField extends CRM_Core_DAO_ImportTemplateField {

/**
* Pseudoconstant callback for the 'name' field
*/
public static function getImportableFieldOptions(string $fieldName, array $params):? array {
$values = $params['values'];
$entity = $values['entity'] ?? NULL;
if (!$entity && !empty($values['id'])) {
$entity = self::getDbVal('entity', $values['id']);
}
if (!$entity) {
return NULL;
}
return (array) civicrm_api4($entity, 'getFields', [
'action' => 'save',
'select' => ['name', 'label', 'description'],
'where' => [
['usage', 'CONTAINS', 'import'],
],
]);
}

/**
* Pseudoconstant callback for the 'entity' field
*/
public static function getImportableEntityOptions(string $fieldName, array $params):? array {
$values = $params['values'];
$userJobId = $values['user_job_id'] ?? NULL;
if (!$userJobId && !empty($values['id'])) {
$userJobId = CRM_Core_BAO_UserJob::getDbVal('user_job_id', $values['id']);
}

if (!$userJobId) {
return NULL;
}
$entities = [];
$jobTypes = array_column(CRM_Core_BAO_UserJob::getTypes(), NULL, 'id');
$jobType = CRM_Core_BAO_UserJob::getDbVal('job_type', $values['user_job_id']);

$mainEntityName = $jobTypes[$jobType]['entity'] ?? NULL;
// TODO: For now each job type only supports one entity,
// so this select list doesn't (yet) have more than one option.
$entities[] = [
'id' => $mainEntityName,
'name' => $mainEntityName,
'label' => CoreUtil::getInfoItem($mainEntityName, 'title'),
'icon' => CoreUtil::getInfoItem($mainEntityName, 'icon'),
];

return $entities;
}

}
4 changes: 4 additions & 0 deletions schema/Core/ImportTemplateField.entityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
'sql_type' => 'varchar(1024)',
'input_type' => 'Select',
'description' => ts('Template field key'),
'pseudoconstant' => [
'callback' => ['CRM_Core_BAO_ImportTemplateField', 'getImportableFieldOptions'],
'suffixes' => ['name', 'label', 'description'],
],
'add' => '5.83',
],
'column_number' => [
Expand Down

0 comments on commit 1dbd8e4

Please sign in to comment.