-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebform_civicrm_postcode.module
More file actions
83 lines (74 loc) · 2.48 KB
/
webform_civicrm_postcode.module
File metadata and controls
83 lines (74 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Implements hook_form_alter().
*/
function webform_civicrm_postcode_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'webform_client_form_') !== FALSE
&& !empty($form['#node']->webform_civicrm)) {
// attach javascript needed for postcode lookup when civipostcode extension is installed and enabled
if(isPostcodeLookupExtensionEnabled()) {
global $base_url;
drupal_add_library('system', 'ui.autocomplete');
$civiPostcodeFields = getCivipostcodeFieldIds($form['#node']->webform['components']);
// Assign the postcode lookup provider to form, so that we can call the related function in AJAX
$settingsStr = CRM_Core_BAO_Setting::getItem('CiviCRM Postcode Lookup', 'api_details');
$settingsArray = unserialize($settingsStr);
$form['#attached']['js'][] = drupal_get_path('module', 'webform_civicrm_postcode') . '/js/civipostcode_component.js';
$form['#attached']['js'][] = array(
// Pass PHP variables to Drupal.settings.
'data' => array(
'baseUrl' => $base_url,
'civiPostCodeLookupProvider' => $settingsArray['provider'],
'civiPostCodeFields' => $civiPostcodeFields,
),
'type' => 'setting',
);
}
}
}
/**
* Implements hook_webform_component_info().
*
* @return array
*/
function webform_civicrm_postcode_webform_component_info() {
$components = array();
// when civipostcode extension is installed and enabled, civipostcode webform component will be made available
if(isPostcodeLookupExtensionEnabled()) {
$components['civipostcode'] = array(
'label' => t('Civi Postcode'),
'file' => 'includes/civipostcode_component.inc',
);
}
return $components;
}
/**
* check if civipostcodelookup extension is enabled.
*
* @return boolean
*/
function isPostcodeLookupExtensionEnabled() {
civicrm_initialize(true);
$isPostcodeLookupEnabled = CRM_Core_DAO::getFieldValue(
'CRM_Core_DAO_Extension',
'uk.co.vedaconsulting.module.civicrmpostcodelookup',
'is_active',
'full_name'
);
return $isPostcodeLookupEnabled;
}
/**
* Get list of fields which are of type 'civipostcode'
*
* @param array $components
* @return array $civiPostcodeFields
*/
function getCivipostcodeFieldIds($components) {
$civiPostcodeFields = array();
foreach($components as $component) {
if($component['type'] == 'civipostcode') {
$civiPostcodeFields[] = str_replace("_", "-", $component['form_key']);
}
}
return $civiPostcodeFields;
}