function _webform_render_civicrm_contact in Webform CiviCRM Integration 7.5
Same name and namespace in other branches
- 7.3 contact_component.inc \_webform_render_civicrm_contact()
- 7.4 includes/contact_component.inc \_webform_render_civicrm_contact()
Implements _webform_render_component().
File
- includes/
contact_component.inc, line 412
Code
function _webform_render_civicrm_contact($component, $value = NULL, $filter = TRUE) {
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
civicrm_initialize();
list(, $c, ) = explode('_', $component['form_key'], 3);
$hide_fields = array_filter($component['extra']['hide_fields']);
$hide_method = wf_crm_aval($component['extra'], 'hide_method', 'hide');
$no_hide_blank = (int) wf_crm_aval($component['extra'], 'no_hide_blank', 0);
$js = '';
$element = [
'#type' => $component['extra']['widget'] == 'autocomplete' ? 'textfield' : $component['extra']['widget'],
'#weight' => $component['weight'],
'#attributes' => $component['extra']['attributes'],
];
$element['#attributes']['data-hide-method'] = $hide_method;
$element['#attributes']['data-no-hide-blank'] = $no_hide_blank;
if ($component['extra']['widget'] != 'hidden' || $component['extra']['show_hidden_contact']) {
$element += [
'#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#required' => $component['required'],
'#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
'#translatable' => [
'title',
'description',
],
'#theme_wrappers' => [
'webform_element',
],
];
}
// See if there are any fields to fetch via ajax
$other_fields = wf_crm_contact_fields($node, $c);
if ($hide_method == 'hide') {
foreach ($hide_fields as $h) {
unset($other_fields[$h]);
}
}
$defaults = json_encode(wf_crm_get_defaults($node));
$ajax = $other_fields ? 'true' : 'false';
$callback_path = '"' . url('webform-civicrm/js/' . $node->nid . '-' . $component['cid'], [
'alias' => TRUE,
]) . '"';
$onChange = "field.change(function() {\n wfCivi.existingSelect({$c}, {$node->nid}, {$callback_path}, toHide, '{$hide_method}', {$no_hide_blank}, \$(this).val(), {$ajax}, {$defaults});\n });";
// Render component based on type
switch ($component['extra']['widget']) {
case 'autocomplete':
if ($node && isset($node->webform_civicrm)) {
$element['#attached']['js'][] = wf_crm_tokeninput_path();
$element['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/token-input.css';
$settings = '{
hintText: ' . json_encode(filter_xss($component['extra']['search_prompt'])) . ',
noResultsText: ' . json_encode(filter_xss($component['extra']['none_prompt'])) . ',
searchingText: ' . json_encode(t('Searching...')) . '
}';
$js = "wfCivi.existingInit(field, {$c}, {$node->nid}, {$callback_path}, toHide, {$settings});{$onChange}";
}
break;
case 'select':
// Options will be set by wf_crm_fill_contact_value.
$element['#options'] = [];
if ($node && isset($node->webform_civicrm)) {
$js = "{$onChange} wfCivi.existingInit(field, {$c}, {$node->nid}, {$callback_path}, toHide);";
}
break;
case 'hidden':
if (!empty($value[0])) {
$element['#value'] = $value[0];
}
if ($node && isset($node->webform_civicrm)) {
$js = $onChange;
if ($component['extra']['show_hidden_contact']) {
$element['#attached']['css'][] = drupal_get_path('module', 'webform_civicrm') . '/css/token-input.css';
}
if ($hide_fields) {
$js .= "wfCivi.existingInit(field, {$c}, {$node->nid}, {$callback_path}, toHide);";
}
$element['#theme_wrappers'] = [
'static_contact_element',
];
if ($component['required']) {
$element['#element_validate'] = [
'wf_crm_contact_component_required',
];
}
}
break;
case 'textfield':
$element['#description'] = t('Lookup by Contact ID.');
$element['#size'] = 8;
if ($node && isset($node->webform_civicrm)) {
$js = "{$onChange} wfCivi.existingInit(field, {$c}, {$node->nid}, {$callback_path}, toHide);";
}
// Display empty option unless there are no results
if (!$component['extra']['allow_create'] || count($element['#options']) > 1) {
$element['#empty_option'] = $component['extra'][$element['#options'] ? 'search_prompt' : 'none_prompt'];
}
}
// Add inline javascript
if ($js) {
$js = '
(function ($, D) {
D.behaviors.webform_civicrm_' . $node->nid . '_' . $component['cid'] . ' = {
attach: function (context) {
var toHide = ' . json_encode(array_values($hide_fields)) . ';
var field = $(\'.webform-client-form-' . $component['nid'] . ' :input.civicrm-enabled[name$="' . $component['form_key'] . ']"]\', context);
' . $js . '
}
};
})(jQuery, Drupal);';
$element['#attached']['js'][$js] = [
'type' => 'inline',
];
}
// Enforce uniqueness.
if ($component['extra']['unique']) {
$element['#element_validate'][] = 'webform_validate_unique';
}
if ($cid = wf_crm_aval($value, 0)) {
$element['#default_value'] = $cid;
}
wf_crm_fill_contact_value($node, $component, $element);
return $element;
}