self::addOptionProps($options, $spec, $bao, $fieldName, $values, $returnFormat);
}
}
+ // Special 'current_domain' option
+ if ($spec->getFkEntity() === 'Domain') {
+ array_unshift($options, [
+ 'id' => 'current_domain',
+ 'name' => 'current_domain',
+ 'label' => ts('Current Domain'),
+ 'icon' => 'fa-sign-in',
+ ]);
+ }
return $options;
}
*/
public static function formatInputValue(&$value, ?string $fieldName, array $fieldSpec, array $params = [], &$operator = NULL, $index = NULL) {
// Evaluate pseudoconstant suffix
- $suffix = strpos(($fieldName ?? ''), ':');
+ $suffix = str_replace(':', '', strstr(($fieldName ?? ''), ':'));
+ $fk = $fieldSpec['name'] == 'id' ? $fieldSpec['entity'] : $fieldSpec['fk_entity'] ?? NULL;
+
+ // Handle special 'current_domain' option. See SpecFormatter::getOptions
+ $currentDomain = ($fk === 'Domain' && in_array('current_domain', (array) $value, TRUE));
+ if ($currentDomain) {
+ // If the fieldName uses a suffix, convert
+ $domainKey = $suffix ?: 'id';
+ $domainValue = \CRM_Core_BAO_Domain::getDomain()->$domainKey;
+ // If the value is an array, only convert the current_domain item
+ if (is_array($value)) {
+ foreach ($value as $idx => $val) {
+ if ($val === 'current_domain') {
+ $value[$idx] = $domainValue;
+ }
+ }
+ }
+ else {
+ $value = $domainValue;
+ }
+ }
+
+ // Convert option list suffix to value
if ($suffix) {
$options = self::getPseudoconstantList($fieldSpec, $fieldName, $params, $operator ? 'get' : 'create');
$value = self::replacePseudoconstant($options, $value, TRUE);
}
return;
}
- $fk = $fieldSpec['name'] == 'id' ? $fieldSpec['entity'] : $fieldSpec['fk_entity'] ?? NULL;
-
- if ($fk === 'Domain' && $value === 'current_domain') {
- $value = \CRM_Core_Config::domainID();
- }
+ // Special handling for 'current_user' and user lookups
if ($fk === 'Contact' && !is_numeric($value)) {
$value = \_civicrm_api3_resolve_contactID($value);
if ('unknown-user' === $value) {
use api\v4\Api4TestBase;
use Civi\Api4\Domain;
+use Civi\Api4\WordReplacement;
use Civi\Test\TransactionalInterface;
/**
->addValue('name', 'Not current')
->addValue('version', \CRM_Utils_System::version())
->execute();
+ Domain::create(FALSE)
+ ->addValue('name', 'Also not current')
+ ->addValue('version', \CRM_Utils_System::version())
+ ->execute();
Domain::update(FALSE)
->addValue('name', 'Currently the current domain')
$this->assertTrue($getAll['Currently the current domain']['is_active']);
$this->assertFalse($getAll['Not current']['is_active']);
+ $this->assertFalse($getAll['Also not current']['is_active']);
+
+ $getNotCurrent = Domain::get(FALSE)
+ ->addWhere('id', '!=', 'current_domain')
+ ->execute()->column('name');
+
+ $this->assertContains('Not current', $getNotCurrent);
+ $this->assertContains('Also not current', $getNotCurrent);
+ $this->assertNotContains('Currently the current domain', $getNotCurrent);
+
+ $wordReplacements = $this->saveTestRecords('WordReplacement', [
+ 'records' => [
+ ['find_word' => 'One', 'replace_word' => 'First'],
+ ['find_word' => 'Two', 'replace_word' => 'Second', 'domain_id:name' => 'Not current'],
+ ['find_word' => 'Three', 'replace_word' => 'Third', 'domain_id:name' => 'Also not current'],
+ ],
+ ])->column('id');
+
+ $fromTwoDomains = WordReplacement::get(FALSE)
+ ->addWhere('domain_id:name', 'IN', ['current_domain', 'Not current'])
+ ->execute()->column('id');
+
+ $this->assertContains($wordReplacements[0], $fromTwoDomains);
+ $this->assertContains($wordReplacements[1], $fromTwoDomains);
+ $this->assertNotContains($wordReplacements[2], $fromTwoDomains);
}
}