$groupParams = [];
$groupParams['title'] = $params['title'];
$groupParams['description'] = $params['description'];
- $groupParams['visibility'] = "User and User Admin Only";
+ $groupParams['visibility'] = 'User and User Admin Only';
$groupParams['group_type'] = array_keys($params['group_type'] ?? []);
$groupParams['is_active'] = 1;
$groupParams['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_id, 'Group');
$subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
}
- self::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
+ $singleRecord = NULL;
+ if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+ $singleRecord = $form->_groupCount;
+ }
+ $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
+ // when a new record is being added for multivalued custom fields.
+ if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' &&
+ !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+ $singleRecord = 'new';
+ }
+
+ $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
+ NULL,
+ $form->_entityId,
+ $gid,
+ $subType,
+ $form->_subName,
+ $getCachedTree,
+ $onlySubType,
+ FALSE,
+ CRM_Core_Permission::EDIT,
+ $singleRecord
+ );
+
+ if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
+ $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
+ }
+ // we should use simplified formatted groupTree
+ $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
+
+ if (isset($form->_groupTree) && is_array($form->_groupTree)) {
+ $keys = array_keys($groupTree);
+ foreach ($keys as $key) {
+ $form->_groupTree[$key] = $groupTree[$key];
+ }
+ }
+ else {
+ $form->_groupTree = $groupTree;
+ }
}
/**
/**
* Add the group data as a formatted array to the form.
*
+ * Note this is only called from this class in core but it is called
+ * from the gdpr extension so rather than clean it up we will deprecate in place
+ * and stop calling from core. (Calling functions like this from extensions)
+ * is not supported but since we are aware of it we can deprecate rather than
+ * remove it).
+ *
+ * @deprecated since 5.65 will be removed around 5.80.
+ *
* @param CRM_Core_Form $form
* @param string $subType
* @param int $gid
* @throws \CRM_Core_Exception
*/
public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
+ CRM_Core_Error::deprecatedFunctionWarning('no alternative - maybe copy & paste to your extension');
$singleRecord = NULL;
if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
$singleRecord = $form->_groupCount;
CRM_Core_Error::deprecatedWarning('Using a CRM_Core_DAO::VALUE_SEPARATOR separated subType on civicrm/custom route is deprecated, use a comma-separated string instead.');
$this->_subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($this->_subType, CRM_Core_DAO::VALUE_SEPARATOR));
}
- CRM_Custom_Form_CustomData::setGroupTree($this, $this->_subType, $this->_groupID, $this->_onlySubtype);
+ $this->setGroupTree($this, $this->_subType, $this->_groupID, $this->_onlySubtype);
$this->assign('suppressForm', TRUE);
$this->controller->_generateQFKey = FALSE;
}
+ /**
+ * Add the group data as a formatted array to the form.
+ *
+ * This was split off from a shared function.
+ *
+ * @param self $form
+ * @param string $subType
+ * @param int $gid
+ * @param bool $onlySubType
+ * @param bool $getCachedTree
+ *
+ * @return array
+ * @throws \CRM_Core_Exception
+ */
+ private function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
+ $singleRecord = NULL;
+ if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+ $singleRecord = $form->_groupCount;
+ }
+ $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
+ // when a new record is being added for multivalued custom fields.
+ if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' &&
+ !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+ $singleRecord = 'new';
+ }
+
+ $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
+ NULL,
+ $form->_entityId,
+ $gid,
+ $subType,
+ $form->_subName,
+ $getCachedTree,
+ $onlySubType,
+ FALSE,
+ CRM_Core_Permission::EDIT,
+ $singleRecord
+ );
+
+ if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
+ $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
+ }
+ // we should use simplified formatted groupTree
+ $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
+
+ if (isset($form->_groupTree) && is_array($form->_groupTree)) {
+ $keys = array_keys($groupTree);
+ foreach ($keys as $key) {
+ $form->_groupTree[$key] = $groupTree[$key];
+ }
+ return [$form, $groupTree];
+ }
+ else {
+ $form->_groupTree = $groupTree;
+ return [$form, $groupTree];
+ }
+ }
+
/**
* Set defaults.
*
+--------------------------------------------------------------------+
*/
- /**
- * @group headless
- */
-class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
+use Civi\Api4\Group;
+use Civi\Api4\GroupContact;
- protected function setUp(): void {
- parent::setUp();
- }
+/**
+ * @group headless
+ */
+class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
/**
* @param array $formValues
* @return CRM_Contact_Form_Task_AddToGroup
*/
- protected function getSearchTaskFormObject(array $formValues) {
+ protected function getSearchTaskFormObject(array $formValues): CRM_Contact_Form_Task_AddToGroup {
$_POST = $formValues;
$_SERVER['REQUEST_METHOD'] = 'GET';
- /** @var CRM_Core_Form $form */
$form = new CRM_Contact_Form_Task_AddToGroup();
$form->controller = new CRM_Contact_Controller_Search();
$form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller));
/**
* Test delete to trash.
+ *
+ * @throws \CRM_Core_Exception
*/
- public function testAddToGroup() {
+ public function testAddToGroup(): void {
$contact = $this->callAPISuccess('Contact', 'create', [
'contact_type' => 'Individual',
'first_name' => 'John',
$form->setDefaultValues();
$form->postProcess();
- $groupCount = \Civi\Api4\GroupContact::get()
+ $groupCount = GroupContact::get()
->addWhere('group_id', '=', $existingGroupId)
->addWhere('status', '=', 'Added')
->execute()
/**
* Test delete to trash.
+ *
+ * @throws \CRM_Core_Exception
*/
- public function testAddToNewGroupWithCustomField() {
+ public function testAddToNewGroupWithCustomField(): void {
$contact = $this->callAPISuccess('Contact', 'create', [
'contact_type' => 'Individual',
'first_name' => 'Pete',
$form->setDefaultValues();
$form->postProcess();
- $group = \Civi\Api4\Group::get()
+ $group = Group::get()
->addSelect('custom.*', 'id')
->addWhere('title', '=', 'Test Group With Custom Field')
->execute();
$this->assertArrayKeyExists('new_custom_group.Custom_Field', $group);
$this->assertEquals('Custom Value ABC', $group['new_custom_group.Custom_Field']);
- $groupCount = \Civi\Api4\GroupContact::get()
+ $groupCount = GroupContact::get()
->addWhere('group_id', '=', $group['id'])
->addWhere('status', '=', 'Added')
->execute()