diff --git a/core/modules/field/modules/email/email.info b/core/modules/field/modules/email/email.info new file mode 100644 index 0000000000000000000000000000000000000000..5c3d3ff929c94c3382130e3ad9baf3e8af5a9840 --- /dev/null +++ b/core/modules/field/modules/email/email.info @@ -0,0 +1,7 @@ +name = E-mail +description = Defines a field type for e-mail addresses. +package = Core +version = VERSION +core = 8.x +dependencies[] = field +dependencies[] = text diff --git a/core/modules/field/modules/email/email.install b/core/modules/field/modules/email/email.install new file mode 100644 index 0000000000000000000000000000000000000000..9af9b9a02e8281dfbf33c73d59685be84ead91e1 --- /dev/null +++ b/core/modules/field/modules/email/email.install @@ -0,0 +1,28 @@ +<?php + +/** + * @file + * Install, update and uninstall functions for the E-mail module. + */ + +/** + * Implements hook_field_schema(). + */ +function email_field_schema($field) { + $columns = array( + 'value' => array( + 'type' => 'varchar', + // The maximum length of an e-mail address is 254 characters. RFC 3696 + // specifies a total length of 320 characters, but mentions that + // addresses longer than 256 characters are not normally useful. Erratum + // 1690 was then released which corrected this value to 254 characters. + // @see http://tools.ietf.org/html/rfc3696#section-3 + // @see http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690 + 'length' => 254, + 'not null' => FALSE, + ), + ); + return array( + 'columns' => $columns, + ); +} diff --git a/core/modules/field/modules/email/email.module b/core/modules/field/modules/email/email.module new file mode 100644 index 0000000000000000000000000000000000000000..6d955d6bc6698c33c075afff85dc5d32cf75c305 --- /dev/null +++ b/core/modules/field/modules/email/email.module @@ -0,0 +1,98 @@ +<?php + +/** + * @file + * Defines a simple e-mail field type. + */ + +/** + * Implements hook_help(). + */ +function email_help($path, $arg) { + switch ($path) { + case 'admin/help#email': + $output = ''; + $output .= '<h3>' . t('About') . '</h3>'; + $output .= '<p>' . t('The E-mail module defines a field for storing e-mail addresses, for use with the Field module. E-mail addresses are validated to ensure they match the expected format. See the <a href="@field-help">Field module help page</a> for more information about fields.', array('@field-help' => url('admin/help/field'))) . '</p>'; + return $output; + } +} + +/** + * Implements hook_field_info(). + */ +function email_field_info() { + return array( + 'email' => array( + 'label' => t('E-mail'), + 'description' => t('This field stores an e-mail address in the database.'), + 'default_widget' => 'email_default', + 'default_formatter' => 'text_plain', + ), + ); +} + +/** + * Implements hook_field_is_empty(). + */ +function email_field_is_empty($item, $field) { + return !isset($item['value']) || $item['value'] === ''; +} + +/** + * Implements hook_field_widget_info(). + */ +function email_field_widget_info() { + return array( + 'email_default' => array( + 'label' => t('E-mail'), + 'field types' => array('email'), + ), + ); +} + +/** + * Implements hook_field_widget_form(). + */ +function email_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + $element['value'] = $element + array( + '#type' => 'email', + '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL, + ); + return $element; + +} + +/** + * Implements hook_field_formatter_info(). + */ +function email_field_formatter_info() { + return array( + 'email_mailto' => array( + 'label' => t('Mailto link'), + 'field types' => array('email'), + ), + ); +} + +/** + * Implements hook_field_formatter_info_alter(). + */ +function email_field_formatter_info_alter(&$info) { + $info['text_plain']['field types'][] = 'email'; +} + +/** + * Implements hook_field_formatter_view(). + */ +function email_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $element = array(); + foreach ($items as $delta => $item) { + $element[$delta] = array( + '#type' => 'link', + '#title' => $item['value'], + '#href' => 'mailto:' . $item['value'], + ); + } + return $element; +} diff --git a/core/modules/field/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php b/core/modules/field/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php new file mode 100644 index 0000000000000000000000000000000000000000..932708e24b2eeca70e850d30d19f711e3ca073df --- /dev/null +++ b/core/modules/field/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php @@ -0,0 +1,90 @@ +<?php + +/** + * @file + * Definition of Drupal\email\Tests\EmailFieldTest. + */ + +namespace Drupal\email\Tests; + +use Drupal\simpletest\WebTestBase; + +/** + * Tests e-mail field functionality. + */ +class EmailFieldTest extends WebTestBase { + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('node', 'field_test', 'email', 'field_ui'); + + public static function getInfo() { + return array( + 'name' => 'E-mail field', + 'description' => 'Tests e-mail field functionality.', + 'group' => 'Field types', + ); + } + + function setUp() { + parent::setUp(); + + $this->web_user = $this->drupalCreateUser(array( + 'access field_test content', + 'administer field_test content', + 'administer content types', + )); + $this->drupalLogin($this->web_user); + } + + /** + * Tests e-mail field. + */ + function testEmailField() { + // Create a field with settings to validate. + $this->field = array( + 'field_name' => drupal_strtolower($this->randomName()), + 'type' => 'email', + ); + field_create_field($this->field); + $this->instance = array( + 'field_name' => $this->field['field_name'], + 'entity_type' => 'test_entity', + 'bundle' => 'test_bundle', + 'widget' => array( + 'type' => 'email_default', + ), + 'display' => array( + 'full' => array( + 'type' => 'email_mailto', + ), + ), + ); + field_create_instance($this->instance); + + // Display creation form. + $this->drupalGet('test-entity/add/test_bundle'); + $langcode = LANGUAGE_NOT_SPECIFIED; + $this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget found.'); + + // Submit a valid e-mail address and ensure it is accepted. + $value = 'test@example.com'; + $edit = array( + "{$this->field['field_name']}[$langcode][0][value]" => $value, + ); + $this->drupalPost(NULL, $edit, t('Save')); + preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); + $id = $match[1]; + $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id))); + $this->assertRaw($value); + + // Verify that a mailto link is displayed. + $entity = field_test_entity_test_load($id); + $entity->content = field_attach_view('test_entity', $entity, 'full'); + $this->drupalSetContent(drupal_render($entity->content)); + $this->assertLinkByHref('mailto:test@example.com'); + } +}