diff --git a/email.module b/email.module
index a33d85c..6a7f7c9 100644
--- a/email.module
+++ b/email.module
@@ -150,7 +150,10 @@ function email_field_widget_info() {
'email_textfield' => array(
'label' => t('Text field'),
'field types' => array('email'),
- 'settings' => array('size' => 60),
+ 'settings' => array(
+ 'size' => 60,
+ 'autocomplete' => FALSE,
+ ),
),
);
}
@@ -169,6 +172,12 @@ function email_field_widget_settings_form($field, $instance) {
'#required' => TRUE,
'#element_validate' => array('_element_validate_integer_positive'),
);
+ $form['autocomplete'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Autocomplete with users\' email addresses'),
+ '#description' => t('Autocomplete with emails from users of this site.'),
+ '#default_value' => $settings['autocomplete'],
+ );
return $form;
}
@@ -183,6 +192,7 @@ function email_field_widget_form(&$form, &$form_state, $field, $instance, $langc
'#size' => $instance['widget']['settings']['size'],
'#prefix' => '
',
'#suffix' => '
',
+ '#autocomplete_path' => empty($instance['widget']['settings']['autocomplete']) ? '' : 'email-autocomplete',
);
return $element;
}
@@ -199,6 +209,12 @@ function email_menu() {
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
+ $items['email-autocomplete'] = array(
+ 'title' => 'Autocomplete for Emails',
+ 'page callback' => '_email_siteusers_autocomplete',
+ 'access arguments' => array('administer users'),
+ 'type' => MENU_CALLBACK,
+ );
$items['admin/config/content/email'] = array(
'title' => 'Email Contact Form Settings',
'description' => 'Administer flood control settings for email contact forms',
@@ -211,6 +227,28 @@ function email_menu() {
}
/**
+ * Autocomplete sit site's users' emails.
+ */
+function _email_siteusers_autocomplete($string = '') {
+ $matches = array();
+ if ($string != '') {
+ $result = $query = db_select('users', 'u')
+ ->fields('u', array('uid', 'mail'))
+ ->where("LOWER(u.mail) LIKE :mail", array(':mail' => strtolower($string) . '%'))
+ ->range(0, 10)
+ ->execute()
+ ->fetchAllKeyed();
+ $users = user_load_multiple(array_keys($result));
+ foreach ($result as $uid => $mail) {
+ if ($users[$uid]) {
+ $matches[$mail] = check_plain($mail . ' (' . format_username($users[$uid]) . ')');
+ }
+ }
+ }
+ drupal_json_output($matches);
+}
+
+/**
* Access callback for the email contact page.
*
* Checks whether the current user has view access to the entity. Access checks