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#244 Allow use of custom fields of type select without specifying an optiongroup #12440

Merged

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Jul 7, 2018

Overview

For an extension I had a requirement to add a "Select Month" custom field. The options are populated dynamically using hook_civicrm_fieldOptions and there is no need for an option group to be associated with them (manually specifying the months of the year in an optiongroup means they won't be updated to match the current locale).

Additionally, if populated dynamically using the hook_civicrm_fieldOptions and there is an optiongroup associated with the field, the "Edit options" element is automatically added to the UI which is not desirable and does not work properly as the optiongroup is not populated.

There is a single line in CRM_Core_BAO_CustomField::getOptions which causes the function to return before calling hooks if the element is a Select and doesn't have an option group defined which makes it impossible to populate the field using hooks. Removing this allows the hook to populate the custom field values and everything works.

For an example see https://github.com/mattwire/uk.co.mjwconsult.variablerecurpayments (Edit a Membership Type and select the Pro-Rata Start Month).

#12439 is also required.

Before

Could not dynamically populate a custom field of type select using hooks without specifying an optiongroup as well.

After

Can dynamically populate a custom field of type select using hooks without specifying an optiongroup (create the custom field via XML or API).

Technical Details

The code makes an assumption in one place that a select element will have an optiongroup. But this unnecessarily restricts the ability to dynamically populate the fields.

https://lab.civicrm.org/dev/core/issues/244

@civibot
Copy link

civibot bot commented Jul 7, 2018

(Standard links)

@@ -419,9 +419,6 @@ public function getOptions($context = NULL) {
elseif ($this->data_type === 'Boolean') {
$options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean();
}
else {
return FALSE;
}
CRM_Utils_Hook::customFieldOptions($this->id, $options, FALSE);
CRM_Utils_Hook::fieldOptions($this->getEntity(), "custom_{$this->id}", $options, array('context' => $context));
return $options;
Copy link
Contributor

Choose a reason for hiding this comment

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

don't you need to define $options = FALSE at the start to avoid e-notices?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eileenmcnaughton Yes I do, and now I have :-)

@eileenmcnaughton eileenmcnaughton self-assigned this Jul 16, 2018
…n option group (so they can be populated dynamically using hook_civicrm_fieldOptions
@mattwire mattwire force-pushed the customfield_selectnooptiongroup branch from ec15a25 to 6c5d3fc Compare July 23, 2018 16:33
@colemanw
Copy link
Member

Hmm, I think there's more than just 1 place where this assumption is made. I'm going to work up an api4 patch to better guess whether a field may have associated options, bearing this in mind.
But I'm still +1 on this patch.

@colemanw colemanw added the merge ready PR will be merged after a few days if there are no objections label Jul 23, 2018
@eileenmcnaughton eileenmcnaughton merged commit 8c70172 into civicrm:master Jul 24, 2018
@eileenmcnaughton
Copy link
Contributor

I think this is right

@colemanw
Copy link
Member

@mattwire mattwire deleted the customfield_selectnooptiongroup 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 merge ready PR will be merged after a few days if there are no objections
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants