CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Option group based on query results
Pages: [1]

Author Topic: Option group based on query results  (Read 488 times)

jlwood

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.2.9
Option group based on query results
December 19, 2014, 11:55:38 am
My Problem: Some of my civicrm contacts have publications in a bibliography database, I want to add a custom field that allows me to set their "author id" in the civicrm contcact record. Since new author id's are added frequently, the select box opitons in the civicrm field need to be based on a query of the current author data instead of a static option group. I know what the query should look like, but how do a create an "Option group" based on a query instead of a static list?

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Option group based on query results
December 19, 2014, 11:57:11 am
With http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_customFieldOptions
Try asking your question on the new CiviCRM help site.

jlwood

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.2.9
Re: Option group based on query results
December 19, 2014, 02:41:35 pm
That looks like just what I was searching for, Thanks! It seemed like something that must exist, but I wasn't finding the right hook.

jlwood

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.2.9
Re: Option group based on query results
January 05, 2015, 01:56:32 pm
Just a quick follow-up and question. The hook populated my select box just as needed, but it appears as though the function runs everytime the data is displayed or queried not just when the field is editable. I'm creating a Drupal view where I need the underlying numeric value, not the label, but the view only returns the label result. I can see in the data that the correct value is present, and that each time the query runs, the hook code is executed. Is this behavior by design?

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Option group based on query results
January 05, 2015, 02:24:04 pm
There is no other way to fetch the option list without executing your code, thus it runs every time.
Try asking your question on the new CiviCRM help site.

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Option group based on query results
January 05, 2015, 02:24:29 pm
You could implement some sort of caching in your hook code if you want to be more efficient.
Try asking your question on the new CiviCRM help site.

jlwood

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.2.9
Re: Option group based on query results
January 05, 2015, 02:30:08 pm
That's what I was beginning to surmise. None of the variable's passed into hook_customFieldOptions have any information about the context ($fieldID, $options, and $detailedFormat). Is there a 'best practices' method for detecting whether or not the field is being displayed as an editable value?

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Option group based on query results
January 05, 2015, 03:34:03 pm
I wouldn't try to get too clever if I were you. Simplest solution would be to stash the values in a static variable at the top of your hook fn and return it if it's already populated. That way your query only runs once per request, which should give you perfectly good performance.
Try asking your question on the new CiviCRM help site.

jlwood

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.2.9
Re: Option group based on query results
January 05, 2015, 03:39:56 pm
I fine tuned it by using Drupal's context module and creating a "no_auth_labels" context, then wrapping the relevant code in this:

Code: [Select]
$contexts = context_active_contexts();
if (!array_key_exists('no_auth_labels', $contexts)) :
    /*  All the good stuff goes in here;*/
endif;

Thanks for all your help

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Option group based on query results

This forum was archived on 2017-11-26.