1

Is there any kind of integration between Civi option groups and Drupal taxonomy vocabs?

For instance I'd like to create a Civi custom field with an option group whose options are populated from a Drupal taxonomy. Or vice versa!

Has that ever been acheived? I'd be glad to know if it has. Thanks for your thoughts.

2

This is definitely possible. I've done it to have a custom field store a term id, and user selects from a select list of term names...

You need to implement hook_civicrm_customFieldOptions(). http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_customFieldOptions

In this provided example below I am using EntityFieldQuery to fetch all terms of the vocabulary with machine name center. $fieldID is the custom field id

The custom field with ID 4 in my example was configured as an Alphanumeric data type with a Multi-Select widget, but you could use a regular select widget as well.

I hope this gets you started!

function YOURMODULE_civicrm_customFieldOptions( $fieldID, &$options, $detailedFormat = false ) {
    if($fieldID == 4) {
        $center_query = new EntityFieldQuery();
        $center_query->entityCondition('entity_type', 'taxonomy_term');
        $center_query->entityCondition('bundle', 'center');

        $centers_ids = $center_query->execute();

        $detailed_options = array();

        $centers = entity_load('taxonomy_term', array_keys($centers_ids['taxonomy_term']));

        foreach($centers as $center) {
          $label = '';

          $label .= ' ' . $center->name;
          if($detailedFormat) {
            $detailed_options[$center->tid] = array (
              'id' => $center->tid,
              'value' => $center->tid,
              'label' => $label,
            );
          }
          else {
            $options[$center->tid] = $label;
          }

        }

        if($detailedFormat) {
          $options += $detailed_options;
        }

        asort($options);
      }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.