diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php index 6322522..3ca2920 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php @@ -282,7 +282,7 @@ function testFieldAttachCache() { $this->assertEqual($cache->data[$this->field_name_2][$langcode], $values, 'Cached: correct cache entry on load'); // Update with different values, and check that the cache entry is wiped. - $values = $this->_generateTestFieldValues($this->field_name_2['cardinality']); + $values = $this->_generateTestFieldValues($this->field_2['cardinality']); $entity = entity_create($entity_type, array( 'type' => $entity_type, 'id' => $entity->id(), @@ -302,7 +302,7 @@ function testFieldAttachCache() { 'type' => $entity_type, 'id' => $entity->id(), )); - $values = $this->_generateTestFieldValues($this->field_name_2['cardinality']); + $values = $this->_generateTestFieldValues($this->field_2['cardinality']); $entity->{$this->field_name} = $values; $entity->setNewRevision(); $entity->save(); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php index 1035212..2e56066 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php @@ -220,13 +220,13 @@ function testFieldAttachSaveEmptyDataDefaultValue() { $this->createFieldWithInstance('', $entity_type); // Add a default value function. - $this->instance['default_value_function'] = 'field_test_default_value'; + /// @todo: What's this? + //$this->instance['default_value_function'] = 'field_test_default_value'; $this->instance->save(); // Verify that fields are populated with default values. $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1)); - $default = field_test_default_value($entity_init, $this->field, $this->instance); - $this->assertEqual($entity_init->{$this->field_name}->getValue(), $default, 'Default field value correctly populated.'); + $this->assertEqual($entity_init->{$this->field_name}->value, 99, 'Default field value correctly populated.'); // Insert: Field is NULL. $entity = clone($entity_init); @@ -236,7 +236,7 @@ function testFieldAttachSaveEmptyDataDefaultValue() { $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'Insert: NULL field results in no value saved'); // Verify that prepopulated field values are not overwritten by defaults. - $value = array(array('value' => $default[0]['value'] - mt_rand(1, 127))); + $value = array(array('value' => 99 - mt_rand(1, 127))); $entity = entity_create($entity_type, array('type' => $entity_init->bundle(), $this->field_name => $value)); $this->assertEqual($entity->{$this->field_name}->getValue(), $value, 'Prepopulated field value correctly maintained.'); } diff --git a/core/modules/field/tests/modules/field_test/field_test.field.inc b/core/modules/field/tests/modules/field_test/field_test.field.inc index 3ac75fa..88ffe87 100644 --- a/core/modules/field/tests/modules/field_test/field_test.field.inc +++ b/core/modules/field/tests/modules/field_test/field_test.field.inc @@ -9,50 +9,6 @@ use Drupal\field\FieldException; use Drupal\field\FieldInterface; -/** - * Implements hook_field_info(). - */ -function field_test_field_info() { - return array( - 'test_field' => array( - 'label' => t('Test field'), - 'description' => t('Dummy field type used for tests.'), - 'settings' => array( - 'test_field_setting' => 'dummy test string', - 'changeable' => 'a changeable field setting', - 'unchangeable' => 'an unchangeable field setting', - ), - 'instance_settings' => array( - 'test_instance_setting' => 'dummy test string', - 'test_hook_field_load' => FALSE, - ), - 'default_widget' => 'test_field_widget', - 'default_formatter' => 'field_test_default', - 'class' => 'Drupal\field_test\Type\TestItem', - ), - 'shape' => array( - 'label' => t('Shape'), - 'description' => t('Another dummy field type.'), - 'settings' => array( - 'foreign_key_name' => 'shape', - ), - 'instance_settings' => array(), - 'default_widget' => 'test_field_widget', - 'default_formatter' => 'field_test_default', - 'class' => 'Drupal\field_test\Type\ShapeItem', - ), - 'hidden_test_field' => array( - 'no_ui' => TRUE, - 'label' => t('Hidden from UI test field'), - 'description' => t('Dummy hidden field type used for tests.'), - 'settings' => array(), - 'instance_settings' => array(), - 'default_widget' => 'test_field_widget', - 'default_formatter' => 'field_test_default', - 'class' => 'Drupal\field_test\Type\HiddenTestItem', - ), - ); -} /** * Implements hook_field_widget_info_alter(). @@ -91,29 +47,6 @@ function field_test_field_load($entity_type, $entities, $field, $instances, $lan } } -/** - * Implements hook_field_insert(). - */ -function field_test_field_insert(EntityInterface $entity, $field, $instance, $items) { - $args = func_get_args(); - field_test_memorize(__FUNCTION__, $args); -} - -/** - * Implements hook_field_update(). - */ -function field_test_field_update(EntityInterface $entity, $field, $instance, $items) { - $args = func_get_args(); - field_test_memorize(__FUNCTION__, $args); -} - -/** - * Implements hook_field_delete(). - */ -function field_test_field_delete(EntityInterface $entity, $field, $instance, $items) { - $args = func_get_args(); - field_test_memorize(__FUNCTION__, $args); -} /** * Implements hook_field_validate(). @@ -139,47 +72,10 @@ function field_test_field_validate(EntityInterface $entity = NULL, $field, $inst * Implements hook_field_is_empty(). */ function field_test_field_is_empty($item, $field_type) { - if ($field_type == 'test_field') { - return empty($item['value']); - } return empty($item['shape']) && empty($item['color']); } /** - * Implements hook_field_settings_form(). - */ -function field_test_field_settings_form($field, $instance) { - $settings = $field['settings']; - - $form['test_field_setting'] = array( - '#type' => 'textfield', - '#title' => t('Field test field setting'), - '#default_value' => $settings['test_field_setting'], - '#required' => FALSE, - '#description' => t('A dummy form element to simulate field setting.'), - ); - - return $form; -} - -/** - * Implements hook_field_instance_settings_form(). - */ -function field_test_field_instance_settings_form($field, $instance) { - $settings = $instance['settings']; - - $form['test_instance_setting'] = array( - '#type' => 'textfield', - '#title' => t('Field test field instance setting'), - '#default_value' => $settings['test_instance_setting'], - '#required' => FALSE, - '#description' => t('A dummy form element to simulate field instance setting.'), - ); - - return $form; -} - -/** * Form element validation handler for 'test_field_widget_multiple' widget. */ function field_test_widget_multiple_validate($element, &$form_state) { @@ -192,13 +88,6 @@ function field_test_widget_multiple_validate($element, &$form_state) { } /** - * Sample 'default value' callback. - */ -function field_test_default_value(EntityInterface $entity, $field, $instance) { - return array(array('value' => 99)); -} - -/** * Implements hook_field_access(). */ function field_test_field_access($op, FieldInterface $field, $entity_type, $entity, $account) { diff --git a/core/modules/field/tests/modules/field_test/field_test.install b/core/modules/field/tests/modules/field_test/field_test.install index a079829..99db5f2 100644 --- a/core/modules/field/tests/modules/field_test/field_test.install +++ b/core/modules/field/tests/modules/field_test/field_test.install @@ -13,50 +13,3 @@ function field_test_install() { module_set_weight('field_test', 1); } -/** - * Implements hook_field_schema(). - */ -function field_test_field_schema($field) { - if ($field['type'] == 'test_field') { - return array( - 'columns' => array( - 'value' => array( - 'type' => 'int', - 'size' => 'medium', - 'not null' => FALSE, - ), - ), - 'indexes' => array( - 'value' => array('value'), - ), - ); - } - else { - $foreign_keys = array(); - // The 'foreign keys' key is not always used in tests. - if (!empty($field['settings']['foreign_key_name'])) { - $foreign_keys['foreign keys'] = array( - // This is a dummy foreign key definition, references a table that - // doesn't exist, but that's not a problem. - $field['settings']['foreign_key_name'] => array( - 'table' => $field['settings']['foreign_key_name'], - 'columns' => array($field['settings']['foreign_key_name'] => 'id'), - ), - ); - } - return array( - 'columns' => array( - 'shape' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => FALSE, - ), - 'color' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => FALSE, - ), - ), - ) + $foreign_keys; - } -} diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/HiddenTestItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/HiddenTestItem.php similarity index 53% rename from core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/HiddenTestItem.php rename to core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/HiddenTestItem.php index 7a9dd30..ba3d698 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/HiddenTestItem.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/HiddenTestItem.php @@ -2,13 +2,24 @@ /** * @file - * Contains \Drupal\field_test\Type\HiddenTestItem. + * Contains \Drupal\field_test\Plugin\field\field_type\HiddenTestItem. */ -namespace Drupal\field_test\Type; +namespace Drupal\field_test\Plugin\field\field_type; + +use Drupal\Core\Entity\Annotation\FieldType; +use Drupal\Core\Annotation\Translation; /** - * Defines the 'test_field' entity field item. + * Defines the 'hidden_test_field' entity field item. + * + * @FieldType( + * id = "hidden_test_field", + * label = @Translation("Hidden from UI test field"), + * description = @Translation("Dummy hidden field type used for tests."), + * default_widget = "test_field_widget", + * default_formatter = "field_test_default" + * ) */ class HiddenTestItem extends TestItem { diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/ShapeItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/ShapeItem.php similarity index 28% rename from core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/ShapeItem.php rename to core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/ShapeItem.php index 4c0d4c9..cf94bcb 100644 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/ShapeItem.php +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/ShapeItem.php @@ -2,17 +2,31 @@ /** * @file - * Contains \Drupal\field_test\Type\ShapeItem. + * Contains \Drupal\field_test\Plugin\field\field_type\ShapeItem. */ -namespace Drupal\field_test\Type; +namespace Drupal\field_test\Plugin\field\field_type; -use Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem; +use Drupal\Core\Entity\Annotation\FieldType; +use Drupal\Core\Annotation\Translation; +use Drupal\field\FieldInterface; +use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase; /** * Defines the 'shape_field' entity field item. + * + * @FieldType( + * id = "shape_field", + * label = @Translation("Shape"), + * description = @Translation("Another dummy field type."), + * settings = { + * "foreign_key_name" = "shape" + * }, + * default_widget = "test_field_widget", + * default_formatter = "field_test_default" + * ) */ -class ShapeItem extends LegacyConfigFieldItem { +class ShapeItem extends ConfigFieldItemBase { /** * Property definitions of the contained properties. @@ -24,7 +38,7 @@ class ShapeItem extends LegacyConfigFieldItem { static $propertyDefinitions; /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). + * {@inheritdoc} */ public function getPropertyDefinitions() { @@ -41,4 +55,36 @@ public function getPropertyDefinitions() { return static::$propertyDefinitions; } + /** + * {@inheritdoc} + */ + public static function schema(FieldInterface $field) { + $foreign_keys = array(); + // The 'foreign keys' key is not always used in tests. + if ($field->getFieldSetting('foreign_key_name')) { + $foreign_keys['foreign keys'] = array( + // This is a dummy foreign key definition, references a table that + // doesn't exist, but that's not a problem. + $field['settings']['foreign_key_name'] => array( + 'table' => $field->getFieldSetting('foreign_key_name'), + 'columns' => array($field->getFieldSetting('foreign_key_name') => 'id'), + ), + ); + } + return array( + 'columns' => array( + 'shape' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => FALSE, + ), + 'color' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => FALSE, + ), + ), + ) + $foreign_keys; + } + } diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/TestItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/TestItem.php new file mode 100644 index 0000000..b2aa0f6 --- /dev/null +++ b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Plugin/field/field_type/TestItem.php @@ -0,0 +1,125 @@ + 'integer', + 'label' => t('Test integer value'), + ); + } + return static::$propertyDefinitions; + } + + /** + * {@inheritdoc} + */ + public static function schema(FieldInterface $field) { + return array( + 'columns' => array( + 'value' => array( + 'type' => 'int', + 'size' => 'medium', + 'not null' => FALSE, + ), + ), + 'indexes' => array( + 'value' => array('value'), + ), + ); + } + + /** + * {@inheritdoc} + */ + public function applyDefaultValue($notify = TRUE) { + $this->setValue(array('value' => 99), $notify); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, array &$form_state, $has_data) { + $form['test_field_setting'] = array( + '#type' => 'textfield', + '#title' => t('Field test field setting'), + '#default_value' => $this->getFieldSetting('test_field_setting'), + '#required' => FALSE, + '#description' => t('A dummy form element to simulate field setting.'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function instanceSettingsForm(array $form, array &$form_state) { + $form['test_instance_setting'] = array( + '#type' => 'textfield', + '#title' => t('Field test field instance setting'), + '#default_value' => $this->getFieldSetting('test_instance_setting'), + '#required' => FALSE, + '#description' => t('A dummy form element to simulate field instance setting.'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function prepareCache() { + if ($this->getFieldSetting('test_hook_field_load')) { + $this->additional_key = 'additional_value'; + } + } + +} diff --git a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/TestItem.php b/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/TestItem.php deleted file mode 100644 index 20c34e3..0000000 --- a/core/modules/field/tests/modules/field_test/lib/Drupal/field_test/Type/TestItem.php +++ /dev/null @@ -1,40 +0,0 @@ - 'integer', - 'label' => t('Test integer value'), - ); - } - return static::$propertyDefinitions; - } - -}