2

I would like to get all active (i.e., in countryLimit) states & provinces available in my site for use in HTML tag. Can I query for that with a single JavaScript call to CRM.api3?

I've tried:

CRM.api3('Setting', 'get', {

"sequential": 1, "return": ["countryLimit"], "api.StateProvince.get": {}

but that just gives me two unrelated arrays. Can I join them somehow?

1
CRM.api3('StateProvince', 'get', {
  "sequential": 1,
  "return": ["name","country_id.name"],
  "options": {"limit":0}
})

UPDATE: I haven't tried this but Lodash v3 is loaded on most civi pages so you might be able to use filter or forEach to combine the results of the above with the countryLimit array to remove inactive ones.

3
  • Thanks. You've helped me realize I left something out of my question. I need to limit the results by 'active' countries, which I think means in countryLimit. Your answer gives my all the StateProvince records in the database. I need to filter for active countries. – MikeSteigerwald Nov 23 '20 at 18:37
  • Thanks, again. Would that involve a separate api call? I'm really trying to keep all this in a single call. – MikeSteigerwald Nov 24 '20 at 17:04
  • Yes I think so since you'd need to get countryLimit separately. – Demerit Nov 24 '20 at 17:05
0

Adding another answer since this might work for you better. In your extension in hook_civicrm_buildForm do something like this. I'm not sure if this is documented anywhere but the country/state chain will do some autowiring for you if your fields are named in a special way.

function myextension_civicrm_buildForm($formName, &$form) {
  if ($formName == 'The_Form_Name') {
    $form->add('select', 'country', ts('Country'), CRM_Core_PseudoConstant::country());
    $form->addChainSelect('state_province');
    CRM_Core_Region::instance('form-body')->add(array(
     'template' => 'CRM/Myextension/my.tpl',
    ));
  }

Then in CRM/Myextension/my.tpl something like:

<tr><td class="label">{$form.country.label}</td>
<td>{$form.country.html}</td></tr>
<tr><td class="label">{$form.state_province.label}</td>
<td>{$form.state_province.html}</td></tr>

You may have to fiddle a bit but this will automatically apply the countryLimit and connect the two fields.

2
  • Thanks, again, but I'm trying to keep this 100% JavaScript. – MikeSteigerwald Nov 24 '20 at 16:27
  • Ok have updated the javascript one with an idea. – Demerit Nov 24 '20 at 16:46

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.