function ContactComponent::wf_crm_contact_search in Webform CiviCRM Integration 8.5
Returns a list of contacts based on component settings.
Parameters
\Drupal\webform\WebformInterface $node: Node object
array $element: Webform element
array $params: Contact get params (filters)
array $contacts: Existing contact data
string $str: Search string (used during autocomplete)
Return value
array
Overrides ContactComponentInterface::wf_crm_contact_search
File
- src/
ContactComponent.php, line 83
Class
- ContactComponent
- Class ContactComponent
Namespace
Drupal\webform_civicrmCode
function wf_crm_contact_search($node, $element, $params, $contacts, $str = NULL) {
// TODO in Drupal8 now node is a webform - maybe we could check here if it has CiviCRM handler
// if (empty($node->webform_civicrm)) {
// return array();
// }
$limit = $str ? 12 : 500;
$ret = [];
$display_fields = array_values($element['#results_display']);
$search_field = 'display_name';
$sort_field = 'sort_name';
// Search and sort based on the selected display field
if (!in_array('display_name', $display_fields)) {
$search_field = $sort_field = $display_fields[0];
}
$params += [
'rowCount' => $limit,
'sort' => $sort_field,
'return' => $display_fields,
];
if (!empty($params['relationship']['contact'])) {
$c = $params['relationship']['contact'];
$relations = NULL;
if (!empty($contacts[$c]['id'])) {
$relations = $this
->wf_crm_find_relations($contacts[$c]['id'], wf_crm_aval($params['relationship'], 'types'));
$params['id'] = [
'IN' => $relations,
];
}
if (!$relations) {
return $ret;
}
}
unset($params['relationship']);
if ($str) {
$str = str_replace(' ', '%', \CRM_Utils_Type::escape($str, 'String'));
// The contact api takes a quirky format for display_name and sort_name
if (in_array($search_field, [
'sort_name',
'display_name',
])) {
$params[$search_field] = $str;
}
else {
$params[$search_field] = [
'LIKE' => "%{$str}%",
];
}
}
$result = \Drupal::service('webform_civicrm.utils')
->wf_civicrm_api('contact', 'get', $params);
// Autocomplete results
if ($str) {
foreach (wf_crm_aval($result, 'values', []) as $contact) {
if ($name = $this
->wf_crm_format_contact($contact, $display_fields)) {
$ret[] = [
'id' => $contact['id'],
'name' => $name,
];
}
}
if (count($ret) < $limit && $element['#allow_create']) {
// HTML hack to get prompt to show up different than search results
$ret[] = [
'id' => "-{$str}",
'name' => '<em><i>' . Xss::filter($element['#none_prompt']) . '</i></em>',
];
}
}
else {
if (!empty($element['#allow_create'])) {
$ret['-'] = Xss::filter($element['#none_prompt']);
}
foreach (wf_crm_aval($result, 'values', []) as $contact) {
// Select lists will be escaped by FAPI
if ($name = $this
->wf_crm_format_contact($contact, $display_fields, FALSE)) {
$ret[$contact['id']] = $name;
}
}
// If we get exactly $limit results, there are probably more - warn that the list is truncated
if (wf_crm_aval($result, 'count') >= $limit) {
\Drupal::logger('webform_civicrm')
->warning('Maximum contacts exceeded, list truncated on the webform "@title". The webform_civicrm "@field" field cannot display more than @limit contacts because it is a select list. Recommend switching to autocomplete widget in element settings.', [
'@limit' => $limit,
'@field' => $element['#title'],
'@title' => $node
->label(),
]);
if ($node
->access('update') && \Drupal::currentUser()
->hasPermission('access CiviCRM')) {
$warning_message = \Drupal\Core\Render\Markup::create('<strong>' . t('Maximum contacts exceeded, list truncated.') . '</strong><br>' . t('The field "@field" cannot show more than @limit contacts because it is a select list. Recommend switching to autocomplete widget.', [
'@limit' => $limit,
'@field' => $element['#title'],
]));
\Drupal::messenger()
->addMessage($warning_message);
}
}
}
return $ret;
}