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

dev/core#155 Fix optiongroup is_reserved data and use when selecting option group for custom fields #12423

Merged
merged 2 commits into from
Jul 16, 2018

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Jul 5, 2018

Overview

(Ref: #12235)

As the is_reserved flag is not being shown in the UI, and is not editable from the UI it has not been used for anything in core even though it is there.

A common use-case is adding an option group and then adding a set of custom fields that use that option group (eg. for surveys) but that is currently not possible via the UI because the query that looks for available option groups is looking ONLY for option groups that are already linked to custom fields - a catch 22!

Before

Not possible to create optiongroups via the UI that can be used with custom fields.

After

Option groups that are created via the UI can be selected for use with custom fields.
customfieldoptiongroup

Technical Details

The is_reserved flag has always existed in the database but has not been used and has not been set correctly in some cases.

Comments

This fixes a common UI workflow where you need to create a set of options for use by a set of custom fields (for multiple choice questions/forms/surveys eg. forms used to calculate health scores etc.).

Currently you have to create the set of options along with the first custom field and you have no control over it's name. Currently, only when created in this way can you select that option group for use by other custom fields.

@civibot
Copy link

civibot bot commented Jul 5, 2018

(Standard links)

@mattwire mattwire changed the title Optiongroup isreserved dev/core#155 Fix optiongroup is_reserved data and use when selecting option group for custom fields Jul 5, 2018
Copy link
Member

@colemanw colemanw left a comment

Choose a reason for hiding this comment

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

I think this makes sense, provided that we really do think the is_reserved flag should be used for this purpose.

In addition to my code nitpicks, if option groups are getting saved as reserved from the custom field screen, need to fix that as well.

UPDATE civicrm_option_group AS cog INNER JOIN civicrm_custom_field AS ccf
ON cog.id = ccf.option_group_id
SET cog.is_reserved = 0 WHERE cog.is_active = 1 AND ccf.is_active = 1;
UPDATE civicrm_option_group SET is_reserved = 1 WHERE name='environment';
Copy link
Member

Choose a reason for hiding this comment

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

The upgrade code does not affect new installs, so the environment group needs to be set to reserved in the install sql as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@colemanw I've updated the install sql as well now.

$optionGroupParams = array(
'is_reserved' => 0,
'is_active' => 1,
'options' => array('limit' => 0, 'sort' => "title ASC"),
Copy link
Member

Choose a reason for hiding this comment

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

Set a return param here for efficiency if all you need is the title.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

$emptyOptGroup = FALSE;
foreach ($optionGroupMetadata['values'] as $id => $metadata) {
$optionGroups[$id] = $metadata['title'];
}
Copy link
Member

Choose a reason for hiding this comment

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

Consider using CRM_Utils_Array::collect() instead of a loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice! I didn't know about that function.

@mattwire mattwire force-pushed the optiongroup_isreserved branch 2 times, most recently from 79cb9b0 to 91c29f6 Compare July 11, 2018 12:00
… of only ones that are already linked to custom fields
@mattwire
Copy link
Contributor Author

@colemanw Thankyou very much for your review. I've made the requested changes. Could you confirm if you are happy from a code perspective now?

@colemanw
Copy link
Member

This is shaping up nicely. However we still need to change the custom field form postSave to create a non-reserved group when saving a custom field with a new set of options.

Also I'm a little worried about non-reserved groups floating around in the system. Some extensions create option groups and they probably shouldn't be tampered with but extension authors may not have known to set them to reserved (I just checked and the api defaults to not reserved when creating a group). I guess we just post a memo to the dev channel about this and suggest extensions add an upgrade routine to reserve their option groups.

@mattwire
Copy link
Contributor Author

This is shaping up nicely. However we still need to change the custom field form postSave to create a non-reserved group when saving a custom field with a new set of options.

@colemanw That is now changed.

@mattwire
Copy link
Contributor Author

I guess we just post a memo to the dev channel about this and suggest extensions add an upgrade routine to reserve their option groups.

The whole thing re is_reserved was a bit of a mess before. Once we are happy for this to be merged I'll post a message to the dev list noting which version it will be in.

@colemanw
Copy link
Member

I haven't had a chance to test this out yet but the code looks good and I agree with the concept.

@eileenmcnaughton
Copy link
Contributor

I tested the upgrade script and that I can now chose an unreserved option group. @colemanw has approved the concept so merging

@eileenmcnaughton eileenmcnaughton merged commit cd761cf into civicrm:master Jul 16, 2018
@eileenmcnaughton eileenmcnaughton added the sig:extension support Supports being able to integrate extensions with CiviCRM label Jul 16, 2018
@colemanw
Copy link
Member

@eileenmcnaughton @mattwire This PR, specifically commit c0fce6b has broken the custom field edit form.
To reproduce:

  1. On a stock demo site, go to the custom fields screen.
  2. Click on "View and Edit Fields" for Constituent Information
  3. Click "Edit Field" next to "Most Important Issue"
  4. The options will appear garbled, and you will be unable to save.

I think this is a critical regression and needs to be fixed immediately, but I'm hesitant to just revert this PR because it has an upgrade script. @mattwire - thoughts?

@seamuslee001
Copy link
Contributor

@colemanw i have opened a partial revert here #12717 and put that against 5.5. I think the changes in the CustomGroup file are fine and same with the xml and the upgrade step given they were tested

mattwire added a commit to mattwire/civicrm-core that referenced this pull request Aug 22, 2018
mattwire added a commit to mattwire/civicrm-core that referenced this pull request Aug 22, 2018
mattwire added a commit to mattwire/civicrm-core that referenced this pull request Aug 22, 2018
monishdeb added a commit that referenced this pull request Aug 24, 2018
Fix for issue editing custom fields with option groups after #12423
mattwire added a commit to mattwire/civicrm-core that referenced this pull request Aug 24, 2018
@mattwire mattwire deleted the optiongroup_isreserved branch September 25, 2018 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
master sig:extension support Supports being able to integrate extensions with CiviCRM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants