Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only set defaults when creating a custom field (not when editing one) #12240

Merged
merged 1 commit into from
Jun 1, 2018

Conversation

michaelmcandrew
Copy link
Contributor

Before

When editing a custom field via the API, any values that were not supplied would revert to the default

After

When editing current values are loaded via the ->find() method.

See test for more details

@michaelmcandrew michaelmcandrew force-pushed the dev/core#156 branch 3 times, most recently from 0970701 to 8926ab5 Compare May 31, 2018 21:19
$customField->is_active = CRM_Utils_Array::value('is_active', $params, TRUE);
$customField->is_view = CRM_Utils_Array::value('is_view', $params, FALSE);
if ($op == 'edit') {
$customField = CRM_Core_DAO_CustomField::findById($params['id']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this - I understand that the params should not be applied by code in edit mode - but I don't think we need to worry about loading them so the DB doesn't apply them.

In general I think the preferred way to set defaults is at the api level. Unfortunately historically some form calls to the BAO::create rely on them being set in the BAO - but when done in the BAO the approach is generally to add to the $params in other BAO create functions - eg.

if ($op === 'create') {
$params = array_merge(['is_required' => 0, 'is_searchable' => 0...], $params);
}

But, let's do a quick audit of where create is called & see how much we really are relying on this anti-pattern of setting defaults in the BAO::Create anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, you are right :) I updated the title and the diff

@michaelmcandrew michaelmcandrew changed the title When editing a custom field, load the existing values before copyValu… Only set defaults when creating a custom field (not when editing one) May 31, 2018
@michaelmcandrew
Copy link
Contributor Author

michaelmcandrew commented May 31, 2018

@eileenmcnaughton - this new diff takes into account your feedback though It is still using the $customField->is_required = CRM_Utils_Array::value('is_required', $params, FALSE); on creation. I could switch that to the array_merge method as well if you like?

'id' => $result['id'],
'is_active' => 0,
];
$result = $this->callAPISuccess('CustomField', 'create', $params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelmcandrew looks like your not actually asserting anything other than the API works

What you probably want do to is add in $this->assertTrue($result['values'][$result['id']]['is_searchable'])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seamuslee001 - The test failed with an exception before the patch not applied. Just seeing it work was good enough for me. Though I take your point that I could be testing a bit more with the test.

@eileenmcnaughton
Copy link
Contributor

test this please

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Jun 1, 2018

This makes sense - I'm Ok to merge it without worrying more about taking the tests further because I think we should try to get the SyntaxConformance ones working

@eileenmcnaughton eileenmcnaughton merged commit 1efe6b6 into civicrm:master Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants