Commit d67ea270 authored by catch's avatar catch

Issue #1839078 by dasjo, tim.plunkett, fago: Implement the new entity field...

Issue #1839078 by dasjo, tim.plunkett, fago: Implement the new entity field API for the email field type.
parent d672d203
<?php
/**
* @file
* Contains \Drupal\Core\TypedData\Type\Email.
*/
namespace Drupal\Core\TypedData\Type;
/**
* The Email data type.
*
* The plain value of Email is the email address represented as PHP string.
*/
class Email extends String {
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::validate().
*/
public function validate() {
// @todo Implement validate() method.
}
}
...@@ -28,6 +28,7 @@ function email_field_info() { ...@@ -28,6 +28,7 @@ function email_field_info() {
'description' => t('This field stores an e-mail address in the database.'), 'description' => t('This field stores an e-mail address in the database.'),
'default_widget' => 'email_default', 'default_widget' => 'email_default',
'default_formatter' => 'email_mailto', 'default_formatter' => 'email_mailto',
'field item class' => 'Drupal\email\Type\EmailItem',
), ),
); );
} }
......
<?php
/**
* @file
* Contains \Drupal\email\Tests\EmailItemTest.
*/
namespace Drupal\email\Tests;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\email\Type\EmailItem;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\Core\Entity\Field\FieldInterface;
/**
* Tests the new entity API for the email field type.
*/
class EmailItemTest extends DrupalUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('email', 'entity_test');
public static function getInfo() {
return array(
'name' => 'E-mail field item',
'description' => 'Tests the new entity API for the email field type.',
'group' => 'Field types',
);
}
public function setUp() {
parent::setUp();
$this->enableModules(array('system', 'entity', 'field', 'text', 'options', 'field_sql_storage', 'entity_test', 'email'));
// Create an email field and instance for validation.
$this->field = array(
'field_name' => 'field_email',
'type' => 'email',
);
field_create_field($this->field);
$this->instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_email',
'bundle' => 'entity_test',
'widget' => array(
'type' => 'email_default',
),
);
field_create_instance($this->instance);
}
/**
* Tests using entity fields of the email field type.
*/
public function testEmailItem() {
// Verify entity creation.
$entity = entity_create('entity_test', array());
$value = 'test@example.com';
$entity->field_email = $value;
$entity->name->value = $this->randomName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = entity_load('entity_test', $id);
$this->assertTrue($entity->field_email instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->field_email[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_email->value, $value);
$this->assertEqual($entity->field_email[0]->value, $value);
// Verify changing the email value.
$new_value = $this->randomName();
$entity->field_email->value = $new_value;
$this->assertEqual($entity->field_email->value, $new_value);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_email->value, $new_value);
}
}
<?php
/**
* @file
* Contains \Drupal\email\Type\EmailItem.
*/
namespace Drupal\email\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'email_field' entity field item.
*/
class EmailItem extends FieldItemBase {
/**
* Property definitions of the contained properties.
*
* @see self::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;
/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {
if (!isset(self::$propertyDefinitions)) {
self::$propertyDefinitions['value'] = array(
'type' => 'email',
'label' => t('E-mail value'),
);
}
return self::$propertyDefinitions;
}
}
...@@ -113,6 +113,19 @@ public function testGetAndSet() { ...@@ -113,6 +113,19 @@ public function testGetAndSet() {
$wrapper->setValue(NULL); $wrapper->setValue(NULL);
$this->assertNull($wrapper->getValue(), 'URI wrapper is null-able.'); $this->assertNull($wrapper->getValue(), 'URI wrapper is null-able.');
// Email type.
$value = $this->randomString();
$wrapper = $this->createTypedData(array('type' => 'email'), $value);
$this->assertIdentical($wrapper->getValue(), $value, 'E-mail value was fetched.');
$new_value = 'test@example.com';
$wrapper->setValue($new_value);
$this->assertIdentical($wrapper->getValue(), $new_value, 'E-mail value was changed.');
$this->assertTrue(is_string($wrapper->getString()), 'E-mail value was converted to string');
$wrapper->setValue(NULL);
$this->assertNull($wrapper->getValue(), 'E-mail wrapper is null-able.');
// Binary type. // Binary type.
$wrapper = $this->createTypedData(array('type' => 'binary'), $files[0]->uri); $wrapper = $this->createTypedData(array('type' => 'binary'), $files[0]->uri);
$this->assertTrue(is_resource($wrapper->getValue()), 'Binary value was fetched.'); $this->assertTrue(is_resource($wrapper->getValue()), 'Binary value was fetched.');
......
...@@ -2218,6 +2218,11 @@ function system_data_type_info() { ...@@ -2218,6 +2218,11 @@ function system_data_type_info() {
'class' => '\Drupal\Core\TypedData\Type\Uri', 'class' => '\Drupal\Core\TypedData\Type\Uri',
'primitive type' => Primitive::URI, 'primitive type' => Primitive::URI,
), ),
'email' => array(
'label' => t('Email'),
'class' => '\Drupal\Core\TypedData\Type\Email',
'primitive type' => Primitive::STRING,
),
'binary' => array( 'binary' => array(
'label' => t('Binary'), 'label' => t('Binary'),
'class' => '\Drupal\Core\TypedData\Type\Binary', 'class' => '\Drupal\Core\TypedData\Type\Binary',
......
...@@ -4,4 +4,5 @@ package = Testing ...@@ -4,4 +4,5 @@ package = Testing
version = VERSION version = VERSION
core = 8.x core = 8.x
dependencies[] = field dependencies[] = field
dependencies[] = text
hidden = TRUE hidden = TRUE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment