Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
justinfreeman (Agileware)
Core
Commits
b958933f
Commit
b958933f
authored
11 years ago
by
eileen
Browse files
Options
Downloads
Patches
Plain Diff
CRM-12768 fix & document api option group creation on custom_field.create'
parent
97d5a31f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CRM/Core/BAO/CustomField.php
+27
-29
27 additions, 29 deletions
CRM/Core/BAO/CustomField.php
api/v3/CustomField.php
+5
-10
5 additions, 10 deletions
api/v3/CustomField.php
tests/phpunit/api/v3/CustomFieldTest.php
+35
-2
35 additions, 2 deletions
tests/phpunit/api/v3/CustomFieldTest.php
with
67 additions
and
41 deletions
CRM/Core/BAO/CustomField.php
+
27
−
29
View file @
b958933f
...
...
@@ -179,8 +179,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
// create any option group & values if required
if
(
$params
[
'html_type'
]
!=
'Text'
&&
in_array
(
$params
[
'data_type'
],
array
(
'String'
,
'Int'
,
'Float'
,
'Money'
))
&&
!
empty
(
$params
[
'option_value'
])
&&
is_array
(
$params
[
'option_value'
])
'String'
,
'Int'
,
'Float'
,
'Money'
))
)
{
$tableName
=
CRM_Core_DAO
::
getFieldValue
(
'CRM_Core_DAO_CustomGroup'
,
...
...
@@ -197,35 +196,34 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
$optionGroup
->
is_active
=
1
;
$optionGroup
->
save
();
$params
[
'option_group_id'
]
=
$optionGroup
->
id
;
if
(
!
empty
(
$params
[
'option_value'
])
&&
is_array
(
$params
[
'option_value'
])){
foreach
(
$params
[
'option_value'
]
as
$k
=>
$v
)
{
if
(
strlen
(
trim
(
$v
)))
{
$optionValue
=
new
CRM_Core_DAO_OptionValue
();
$optionValue
->
option_group_id
=
$optionGroup
->
id
;
$optionValue
->
label
=
$params
[
'option_label'
][
$k
];
$optionValue
->
name
=
CRM_Utils_String
::
titleToVar
(
$params
[
'option_label'
][
$k
]);
switch
(
$params
[
'data_type'
])
{
case
'Money'
:
$optionValue
->
value
=
CRM_Utils_Rule
::
cleanMoney
(
$v
);
break
;
case
'Int'
:
$optionValue
->
value
=
intval
(
$v
);
break
;
case
'Float'
:
$optionValue
->
value
=
floatval
(
$v
);
break
;
default
:
$optionValue
->
value
=
trim
(
$v
);
}
foreach
(
$params
[
'option_value'
]
as
$k
=>
$v
)
{
if
(
strlen
(
trim
(
$v
)))
{
$optionValue
=
new
CRM_Core_DAO_OptionValue
();
$optionValue
->
option_group_id
=
$optionGroup
->
id
;
$optionValue
->
label
=
$params
[
'option_label'
][
$k
];
$optionValue
->
name
=
CRM_Utils_String
::
titleToVar
(
$params
[
'option_label'
][
$k
]);
switch
(
$params
[
'data_type'
])
{
case
'Money'
:
$optionValue
->
value
=
CRM_Utils_Rule
::
cleanMoney
(
$v
);
break
;
case
'Int'
:
$optionValue
->
value
=
intval
(
$v
);
break
;
case
'Float'
:
$optionValue
->
value
=
floatval
(
$v
);
break
;
default
:
$optionValue
->
value
=
trim
(
$v
);
$optionValue
->
weight
=
$params
[
'option_weight'
][
$k
];
$optionValue
->
is_active
=
CRM_Utils_Array
::
value
(
$k
,
$params
[
'option_status'
],
FALSE
);
$optionValue
->
save
();
}
$optionValue
->
weight
=
$params
[
'option_weight'
][
$k
];
$optionValue
->
is_active
=
CRM_Utils_Array
::
value
(
$k
,
$params
[
'option_status'
],
FALSE
);
$optionValue
->
save
();
}
}
}
...
...
This diff is collapsed.
Click to expand it.
api/v3/CustomField.php
+
5
−
10
View file @
b958933f
...
...
@@ -65,16 +65,6 @@
*/
function
civicrm_api3_custom_field_create
(
$params
)
{
if
(
!
(
CRM_Utils_Array
::
value
(
'option_type'
,
$params
)))
{
if
(
CRM_Utils_Array
::
value
(
'id'
,
$params
))
{
$params
[
'option_type'
]
=
2
;
}
else
{
$params
[
'option_type'
]
=
1
;
}
}
// Array created for passing options in params
if
(
isset
(
$params
[
'option_values'
])
&&
is_array
(
$params
[
'option_values'
]))
{
foreach
(
$params
[
'option_values'
]
as
$key
=>
$value
)
{
...
...
@@ -98,6 +88,11 @@ function civicrm_api3_custom_field_create($params) {
function
_civicrm_api3_custom_field_create_spec
(
&
$params
)
{
$params
[
'label'
][
'api.required'
]
=
1
;
$params
[
'custom_group_id'
][
'api.required'
]
=
1
;
$params
[
'option_type'
]
=
array
(
'title'
=>
'This (boolean) field tells the BAO to create an option group for the field if the field type is appropriate'
,
'api.default'
=>
1
,
'type'
=>
CRM_Utils_Type
::
T_BOOL
,
);
}
/**
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/api/v3/CustomFieldTest.php
+
35
−
2
View file @
b958933f
...
...
@@ -220,6 +220,39 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase {
/**
* check with data type - Options with option_values
*/
function
testCustomFieldCreateWithEmptyOptionGroup
()
{
$customGroup
=
$this
->
customGroupCreate
(
'Contact'
,
'select_test_group'
,
3
);
$params
=
array
(
'custom_group_id'
=>
$customGroup
[
'id'
],
'label'
=>
'Country'
,
'html_type'
=>
'Select'
,
'data_type'
=>
'String'
,
'weight'
=>
4
,
'is_required'
=>
1
,
'is_searchable'
=>
0
,
'is_active'
=>
1
,
'version'
=>
$this
->
_apiversion
,
);
$customField
=
civicrm_api
(
'custom_field'
,
'create'
,
$params
);
$this
->
assertAPISuccess
(
$customField
);
$this
->
assertNotNull
(
$customField
[
'id'
]);
$optionGroupID
=
civicrm_api
(
'custom_field'
,
'getvalue'
,
array
(
'version'
=>
3
,
'id'
=>
$customField
[
'id'
],
'return'
=>
'option_group_id'
,
));
$this
->
assertTrue
(
is_numeric
(
$optionGroupID
)
&&
(
$optionGroupID
>
0
));
$optionGroup
=
civicrm_api
(
'option_group'
,
'getsingle'
,
array
(
'version'
=>
3
,
'id'
=>
$optionGroupID
));
$this
->
assertEquals
(
$optionGroup
[
'title'
],
'Country'
);
$optionValueCount
=
civicrm_api
(
'option_value'
,
'getcount'
,
array
(
'version'
=>
3
,
'option_group_id'
=>
$optionGroupID
));
$this
->
assertEquals
(
0
,
$optionValueCount
);
}
function
testCustomFieldCreateWithOptionValues
()
{
$customGroup
=
$this
->
customGroupCreate
(
'Contact'
,
'select_test_group'
,
3
);
...
...
@@ -252,7 +285,7 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase {
$customField
=
civicrm_api
(
'custom_field'
,
'create'
,
$params
);
$this
->
assert
Equal
s
(
$customField
[
'is_error'
],
0
);
$this
->
assert
APISucces
s
(
$customField
);
$this
->
assertNotNull
(
$customField
[
'id'
]);
$getFieldsParams
=
array
(
'options'
=>
array
(
'get_options'
=>
'custom_'
.
$customField
[
'id'
]),
...
...
@@ -329,7 +362,7 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase {
);
$customField
=
civicrm_api
(
'custom_field'
,
'delete'
,
$customOptionValueFields
);
$this
->
assert
Equal
s
(
$customField
[
'is_error'
],
0
);
$this
->
assert
APISucces
s
(
$customField
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment