Commit 66f4183f authored by catch's avatar catch

Issue #1899378 by Berdir: Implement the new entity field API for the telephone field type.

parent d1286748
<?php
/**
* @file
* Contains \Drupal\field\Tests\TelephoneItemTest.
*/
namespace Drupal\telephone\Tests;
use Drupal\Core\Entity\Field\FieldInterface;
use Drupal\Core\Entity\Field\FieldItemInterface;
use Drupal\field\Tests\FieldItemUnitTestBase;
/**
* Tests the new entity API for the telephone field type.
*/
class TelephoneItemTest extends FieldItemUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('telephone');
public static function getInfo() {
return array(
'name' => 'Telephone field item',
'description' => 'Tests the new entity API for the telephone field type.',
'group' => 'Field types',
);
}
public function setUp() {
parent::setUp();
// Create a telephone field and instance for validation.
$this->field = array(
'field_name' => 'field_test',
'type' => 'telephone',
);
field_create_field($this->field);
$this->instance = array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
'widget' => array(
'type' => 'telephone_default',
),
);
field_create_instance($this->instance);
}
/**
* Tests using entity fields of the telephone field type.
*/
public function testTestItem() {
// Verify entity creation.
$entity = entity_create('entity_test', array());
$value = '+0123456789';
$entity->field_test = $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_test instanceof FieldInterface, 'Field implements interface.');
$this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_test->value, $value);
$this->assertEqual($entity->field_test[0]->value, $value);
// Verify changing the field value.
$new_value = '+41' . rand(1000000, 9999999);
$entity->field_test->value = $new_value;
$this->assertEqual($entity->field_test->value, $new_value);
// Read changed entity and assert changed values.
$entity->save();
$entity = entity_load('entity_test', $id);
$this->assertEqual($entity->field_test->value, $new_value);
}
}
<?php
/**
* @file
* Contains \Drupal\telephone\Type\TelephoneItem.
*/
namespace Drupal\telephone\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
/**
* Defines the 'telephone_field' entity field items.
*/
class TelephoneItem extends FieldItemBase {
/**
* Definitions of the contained properties.
*
* @see TelephoneItem::getPropertyDefinitions()
*
* @var array
*/
static $propertyDefinitions;
/**
* Implements ComplexDataInterface::getPropertyDefinitions().
*/
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['value'] = array(
'type' => 'string',
'label' => t('Telephone number'),
);
}
return static::$propertyDefinitions;
}
}
...@@ -15,6 +15,7 @@ function telephone_field_info() { ...@@ -15,6 +15,7 @@ function telephone_field_info() {
'description' => t('This field stores a telephone number in the database.'), 'description' => t('This field stores a telephone number in the database.'),
'default_widget' => 'telephone_default', 'default_widget' => 'telephone_default',
'default_formatter' => 'telephone_link', 'default_formatter' => 'telephone_link',
'field item class' => 'Drupal\telephone\Type\TelephoneItem',
), ),
); );
} }
......
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