array(
'label' => 'Email',
'description' => t('This field stores and renders email addresses.'),
'default_widget' => 'email_textfield',
'default_formatter' => 'email_default',
'instance_settings' => array(
'form_title' => 'Contact person by email',
'allow_without_formatter' => FALSE,
),
'property_type' => 'text',
),
);
}
/**
* Implements hook_migrate_api().
*/
function email_migrate_api() {
return array(
'api' => 2,
'field handlers' => array('MigrateEmailFieldHandler'),
);
}
/**
* Implements hook_field_validate().
*
* Possible error codes:
* - 'email_invalid': The email address is not valid
*/
function email_field_validate($obj_type, $object, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
$message = t('"%mail" is not a valid email address', array('%mail' => $item['email']));
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => "email_invalid",
'message' => $message,
);
}
}
}
/**
* Implements hook_field_widget_error().
*/
function email_field_widget_error($element, $error, $form, &$form_state) {
form_error($element, $error['message']);
}
/**
* Implements hook_content_is_empty().
*/
function email_field_is_empty($item, $field) {
if (empty($item['email'])) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_field_settings_form().
*/
function email_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$form['form_title'] = array(
'#type' => 'textfield',
'#title' => t('Email form title'),
'#default_value' => $settings['form_title'],
'#description' => t('The page title for the contact form. You can use token replacement.'),
);
$form['allow_without_formatter'] = array(
'#type' => 'checkbox',
'#title' => t('Allow form to be used without a formatter.'),
'#default_value' => $settings['allow_without_formatter'],
'#description' => t('If unchecked, visiting the URL for this field\'s email form will show a page not found unless the field display settings are configured to use the email form.'),
);
if (module_exists('token')) {
$form['tokens'] = array(
'#theme' => 'token_tree',
'#token_types' => array($instance['entity_type']),
'#dialog' => TRUE,
);
}
return $form;
}
/**
* Implements hook_field_formatter_info().
*
*/
/**
* Implements hook_field_formatter_info().
*
*/
function email_field_formatter_info() {
$formats = array(
'email_default' => array(
'label' => t('Default email link'),
'description' => t('Display the email address as a mailto link.'),
'field types' => array('email'),
),
'email_contact' => array(
'label' => t('Email contact form'),
'description' => t('Display a link to a contact form.'),
'field types' => array('email'),
'settings' => array(
'text' => t('Contact person by email'),
'title' => t('Email Contact Form'),
),
),
'email_contact_url' => array(
'label' => t('Contact form link (plain URL)'),
'description' => t('A raw URL to the contact form for this field.'),
'field types' => array('email'),
),
'email_plain' => array(
'label' => t('Email plain text'),
'description' => t('Display the email address as plain text.'),
'field types' => array('email'),
),
);
if (module_exists('spamspan')) {
$formats += array(
'email_spamspan' => array(
'label' => t('Email SpamSpan'),
'field types' => array('email'),
),
);
}
return $formats;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function email_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'email_contact') {
$element['text'] = array(
'#type' => 'textfield',
'#title' => t('Link text'),
'#description' => t("Change the wording of the link text. Can use token replacement. Defaults to 'Contact person by email'."),
'#size' => 60,
'#default_value' => $settings['text'],
'#required' => TRUE,
);
if (module_exists('token')) {
$element['tokens'] = array(
'#theme' => 'token_tree',
'#token_types' => array($instance['entity_type']),
);
}
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function email_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
if ($display['type'] == 'email_contact') {
$summary[] = t('Link text: @text', array('@text' => $settings['text']));
}
return implode('
', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function email_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
switch ($display['type']) {
case 'email_default':
foreach ($items as $delta => $item) {
$output = l($item['email'], 'mailto:' . $item['email']);
$element[$delta] = array('#markup' => $output);
}
break;
case 'email_contact':
$ids = entity_extract_ids($object_type, $object);
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => l(token_replace(t($settings['text']), array($object_type => $object), array('clear' => TRUE)), 'email/' . $object_type . '/' . $ids[0] . '/' . $instance['field_name']));
// Since email is always sent to first item's email, break after any email address found.
break;
}
break;
case 'email_contact_url':
$ids = entity_extract_ids($object_type, $object);
// As with email_contact, this only works with the first item.
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => 'email/' . $object_type . '/' . $ids[0] . '/' . $instance['field_name']);
break;
}
break;
case 'email_plain':
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => check_plain($item['email']));
}
break;
case 'email_spamspan':
foreach ($items as $delta => $item) {
if (module_exists('spamspan')) {
$element[$delta] = array('#markup' => spamspan($item['email']));
} else {
$output = l($item['email'], 'mailto:' . $item['email']);
$element[$delta] = array('#markup' => $output);
}
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_info().
*/
function email_field_widget_info() {
return array(
'email_textfield' => array(
'label' => t('Text field'),
'field types' => array('email'),
'settings' => array('size' => 60),
),
);
}
/**
* Implements hook_field_widget_settings_form().
*/
function email_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $settings['size'],
'#required' => TRUE,
'#element_validate' => array('_element_validate_integer_positive'),
);
return $form;
}
/**
* Implements hook_field_widget_form().
*/
function email_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
$element = $base;
$element['email'] = $base + array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['email']) ? $items[$delta]['email'] : NULL,
'#size' => $instance['widget']['settings']['size'],
'#prefix' => '