Commit 2de46154 authored by alexpott's avatar alexpott

Issue #1981306 by aspilicious, andypost, pcambra, swentel, amateescu: Drop...

Issue #1981306 by aspilicious, andypost, pcambra, swentel, amateescu: Drop procedural usage of fields in [a-e] modules.
parent a6a4adc9
...@@ -183,22 +183,22 @@ function custom_block_add_body_field($block_type_id, $label = 'Block body') { ...@@ -183,22 +183,22 @@ function custom_block_add_body_field($block_type_id, $label = 'Block body') {
$field = field_info_field('block_body'); $field = field_info_field('block_body');
$instance = field_info_instance('custom_block', 'block_body', $block_type_id); $instance = field_info_instance('custom_block', 'block_body', $block_type_id);
if (empty($field)) { if (empty($field)) {
$field = array( $field = entity_create('field_entity', array(
'field_name' => 'block_body', 'field_name' => 'block_body',
'type' => 'text_with_summary', 'type' => 'text_with_summary',
'entity_types' => array('custom_block'), 'entity_types' => array('custom_block'),
); ));
$field = field_create_field($field); $field->save();
} }
if (empty($instance)) { if (empty($instance)) {
$instance = array( $instance = entity_create('field_instance', array(
'field_name' => 'block_body', 'field_name' => 'block_body',
'entity_type' => 'custom_block', 'entity_type' => 'custom_block',
'bundle' => $block_type_id, 'bundle' => $block_type_id,
'label' => $label, 'label' => $label,
'settings' => array('display_summary' => FALSE), 'settings' => array('display_summary' => FALSE),
); ));
$instance = field_create_instance($instance); $instance->save();
// Assign widget settings for the 'default' form mode. // Assign widget settings for the 'default' form mode.
entity_get_form_display('custom_block', $block_type_id, 'default') entity_get_form_display('custom_block', $block_type_id, 'default')
......
...@@ -25,14 +25,14 @@ class CustomBlockFieldTest extends CustomBlockTestBase { ...@@ -25,14 +25,14 @@ class CustomBlockFieldTest extends CustomBlockTestBase {
/** /**
* The created field. * The created field.
* *
* @var array * @var \Drupal\field\Plugin\Core\Entity\Field
*/ */
protected $field; protected $field;
/** /**
* The created instance * The created instance.
* *
* @var array * @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/ */
protected $instance; protected $instance;
...@@ -64,21 +64,21 @@ public function testBlockFields() { ...@@ -64,21 +64,21 @@ public function testBlockFields() {
$this->blockType = $this->createCustomBlockType('link'); $this->blockType = $this->createCustomBlockType('link');
// Create a field with settings to validate. // Create a field with settings to validate.
$this->field = array( $this->field = entity_create('field_entity', array(
'field_name' => drupal_strtolower($this->randomName()), 'field_name' => drupal_strtolower($this->randomName()),
'type' => 'link', 'type' => 'link',
'cardinality' => 2, 'cardinality' => 2,
); ));
field_create_field($this->field); $this->field->save();
$this->instance = array( $this->instance = entity_create('field_instance', array(
'field_name' => $this->field['field_name'], 'field_name' => $this->field->id(),
'entity_type' => 'custom_block', 'entity_type' => 'custom_block',
'bundle' => 'link', 'bundle' => 'link',
'settings' => array( 'settings' => array(
'title' => DRUPAL_OPTIONAL, 'title' => DRUPAL_OPTIONAL,
), ),
); ));
field_create_instance($this->instance); $this->instance->save();
entity_get_form_display('custom_block', 'link', 'default') entity_get_form_display('custom_block', 'link', 'default')
->setComponent($this->field['field_name'], array( ->setComponent($this->field['field_name'], array(
'type' => 'link_default', 'type' => 'link_default',
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
*/ */
function comment_uninstall() { function comment_uninstall() {
// Delete comment_body field. // Delete comment_body field.
field_delete_field('comment_body'); field_info_field('comment_body')->delete();
// Remove variables. // Remove variables.
variable_del('comment_block_count'); variable_del('comment_block_count');
......
...@@ -345,26 +345,26 @@ function comment_node_type_delete($info) { ...@@ -345,26 +345,26 @@ function comment_node_type_delete($info) {
function _comment_body_field_create($info) { function _comment_body_field_create($info) {
// Create the field if needed. // Create the field if needed.
if (!field_read_field('comment_body', array('include_inactive' => TRUE))) { if (!field_read_field('comment_body', array('include_inactive' => TRUE))) {
$field = array( $field = entity_create('field_entity', array(
'field_name' => 'comment_body', 'field_name' => 'comment_body',
'type' => 'text_long', 'type' => 'text_long',
'entity_types' => array('comment'), 'entity_types' => array('comment'),
); ));
field_create_field($field); $field->save();
} }
// Create the instance if needed. // Create the instance if needed.
if (!field_read_instance('comment', 'comment_body', 'comment_node_' . $info->type, array('include_inactive' => TRUE))) { if (!field_read_instance('comment', 'comment_body', 'comment_node_' . $info->type, array('include_inactive' => TRUE))) {
entity_invoke_bundle_hook('create', 'comment', 'comment_node_' . $info->type); entity_invoke_bundle_hook('create', 'comment', 'comment_node_' . $info->type);
// Attaches the body field by default. // Attaches the body field by default.
$instance = array( $instance = entity_create('field_instance', array(
'field_name' => 'comment_body', 'field_name' => 'comment_body',
'label' => 'Comment', 'label' => 'Comment',
'entity_type' => 'comment', 'entity_type' => 'comment',
'bundle' => 'comment_node_' . $info->type, 'bundle' => 'comment_node_' . $info->type,
'settings' => array('text_processing' => 1), 'settings' => array('text_processing' => 1),
'required' => TRUE, 'required' => TRUE,
); ));
field_create_instance($instance); $instance->save();
// Assign widget settings for the 'default' form mode. // Assign widget settings for the 'default' form mode.
entity_get_form_display('comment', 'comment_node_' . $info->type, 'default') entity_get_form_display('comment', 'comment_node_' . $info->type, 'default')
......
...@@ -41,7 +41,7 @@ function testCommentDefaultFields() { ...@@ -41,7 +41,7 @@ function testCommentDefaultFields() {
$this->assertTrue(isset($instances['comment_node_' . $type_name]['comment_body']), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name))); $this->assertTrue(isset($instances['comment_node_' . $type_name]['comment_body']), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
// Delete the instance along the way. // Delete the instance along the way.
field_delete_instance($instances['comment_node_' . $type_name]['comment_body']); $instances['comment_node_' . $type_name]['comment_body']->delete();
} }
// Check that the 'comment_body' field is deleted. // Check that the 'comment_body' field is deleted.
......
...@@ -73,7 +73,7 @@ function setUp() { ...@@ -73,7 +73,7 @@ function setUp() {
// Make comment body translatable. // Make comment body translatable.
$field = field_info_field('comment_body'); $field = field_info_field('comment_body');
$field['translatable'] = TRUE; $field['translatable'] = TRUE;
field_update_field($field); $field->save();
$this->assertTrue(field_is_translatable('comment', $field), 'Comment body is translatable.'); $this->assertTrue(field_is_translatable('comment', $field), 'Comment body is translatable.');
} }
......
...@@ -65,7 +65,7 @@ function setupTestFields() { ...@@ -65,7 +65,7 @@ function setupTestFields() {
parent::setupTestFields(); parent::setupTestFields();
$field = field_info_field('comment_body'); $field = field_info_field('comment_body');
$field['translatable'] = TRUE; $field['translatable'] = TRUE;
field_update_field($field); $field->save();
} }
/** /**
......
...@@ -24,7 +24,7 @@ class ContactFieldsTest extends ViewTestBase { ...@@ -24,7 +24,7 @@ class ContactFieldsTest extends ViewTestBase {
/** /**
* Contains the field definition array attached to contact used for this test. * Contains the field definition array attached to contact used for this test.
* *
* @var array * @var \Drupal\field\Plugin\Core\Entity\Field
*/ */
protected $field; protected $field;
...@@ -39,19 +39,17 @@ public static function getInfo() { ...@@ -39,19 +39,17 @@ public static function getInfo() {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$field = array( $this->field = entity_create('field_entity', array(
'field_name' => strtolower($this->randomName()), 'field_name' => strtolower($this->randomName()),
'type' => 'text' 'type' => 'text'
); ));
$this->field->save();
$this->field = field_create_field($field); entity_create('field_instance', array(
'field_name' => $this->field->id(),
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'contact_message', 'entity_type' => 'contact_message',
'bundle' => 'contact_message', 'bundle' => 'contact_message',
); ))->save();
field_create_instance($instance);
$this->container->get('views.views_data')->clear(); $this->container->get('views.views_data')->clear();
} }
...@@ -60,7 +58,6 @@ protected function setUp() { ...@@ -60,7 +58,6 @@ protected function setUp() {
* Tests the views data generation. * Tests the views data generation.
*/ */
public function testViewsData() { public function testViewsData() {
$field_name = $this->field['field_name'];
$table_name = _field_sql_storage_tablename($this->field); $table_name = _field_sql_storage_tablename($this->field);
$data = $this->container->get('views.views_data')->get($table_name); $data = $this->container->get('views.views_data')->get($table_name);
...@@ -68,7 +65,7 @@ public function testViewsData() { ...@@ -68,7 +65,7 @@ public function testViewsData() {
$expected = array('', '_value', '_format'); $expected = array('', '_value', '_format');
$this->assertEqual(count($data), count($expected), 'The expected amount of array keys were found.'); $this->assertEqual(count($data), count($expected), 'The expected amount of array keys were found.');
foreach ($expected as $suffix) { foreach ($expected as $suffix) {
$this->assertTrue(isset($data[$field_name . $suffix])); $this->assertTrue(isset($data[$this->field->id() . $suffix]));
} }
$this->assertTrue(empty($data['table']['join']), 'The field is not joined to the non existent contact message base table.'); $this->assertTrue(empty($data['table']['join']), 'The field is not joined to the non existent contact message base table.');
} }
......
...@@ -54,15 +54,15 @@ function setUp() { ...@@ -54,15 +54,15 @@ function setUp() {
*/ */
function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) { function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) {
$field = $field_name . '_field'; $field = $field_name . '_field';
$this->field = array( $this->$field = entity_create('field_entity', array(
'field_name' => $field_name, 'field_name' => $field_name,
'type' => $type, 'type' => $type,
'cardinality' => $cardinality, 'cardinality' => $cardinality,
); ));
$this->$field = field_create_field($this->field); $this->$field->save();
$instance = $field_name . '_instance'; $instance = $field_name . '_instance';
$this->$instance = array( $this->$instance = entity_create('field_instance', array(
'field_name' => $field_name, 'field_name' => $field_name,
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
...@@ -70,8 +70,8 @@ function createFieldWithInstance($field_name, $type, $cardinality, $label, $inst ...@@ -70,8 +70,8 @@ function createFieldWithInstance($field_name, $type, $cardinality, $label, $inst
'description' => $label, 'description' => $label,
'weight' => mt_rand(0, 127), 'weight' => mt_rand(0, 127),
'settings' => $instance_settings, 'settings' => $instance_settings,
); ));
field_create_instance($this->$instance); $this->$instance->save();
entity_get_form_display('entity_test', 'entity_test', 'default') entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($field_name, array( ->setComponent($field_name, array(
......
...@@ -79,24 +79,24 @@ function testText() { ...@@ -79,24 +79,24 @@ function testText() {
$this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality 1, the 'direct' editor is selected."); $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality 1, the 'direct' editor is selected.");
// Editor selection with text processing, cardinality 1. // Editor selection with text processing, cardinality 1.
$this->field_text_instance['settings']['text_processing'] = 1; $this->field_text_instance->settings['text_processing'] = 1;
field_update_instance($this->field_text_instance); $this->field_text_instance->save();
$this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality 1, the 'form' editor is selected."); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality 1, the 'form' editor is selected.");
// Editor selection without text processing, cardinality 1 (again). // Editor selection without text processing, cardinality 1 (again).
$this->field_text_instance['settings']['text_processing'] = 0; $this->field_text_instance->settings['text_processing'] = 0;
field_update_instance($this->field_text_instance); $this->field_text_instance->save();
$this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing again, cardinality 1, the 'direct' editor is selected."); $this->assertEqual('direct', $this->getSelectedEditor($items, $field_name), "Without text processing again, cardinality 1, the 'direct' editor is selected.");
// Editor selection without text processing, cardinality >1 // Editor selection without text processing, cardinality >1
$this->field_text_field['cardinality'] = 2; $this->field_text_field->cardinality = 2;
field_update_field($this->field_text_field); $this->field_text_field->save();
$items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
$this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality >1, the 'form' editor is selected."); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality >1, the 'form' editor is selected.");
// Editor selection with text processing, cardinality >1 // Editor selection with text processing, cardinality >1
$this->field_text_instance['settings']['text_processing'] = 1; $this->field_text_instance->settings['text_processing'] = 1;
field_update_instance($this->field_text_instance); $this->field_text_instance->save();
$this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality >1, the 'form' editor is selected."); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality >1, the 'form' editor is selected.");
} }
...@@ -133,8 +133,8 @@ function testTextWysiwyg() { ...@@ -133,8 +133,8 @@ function testTextWysiwyg() {
$this->assertEqual('wysiwyg', $this->getSelectedEditor($items, $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected."); $this->assertEqual('wysiwyg', $this->getSelectedEditor($items, $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected.");
// Editor selection with text processing, cardinality >1 // Editor selection with text processing, cardinality >1
$this->field_textarea_field['cardinality'] = 2; $this->field_textarea_field->cardinality = 2;
field_update_field($this->field_textarea_field); $this->field_textarea_field->save();
$items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
$this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
} }
...@@ -163,8 +163,8 @@ function testNumber() { ...@@ -163,8 +163,8 @@ function testNumber() {
$this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality 1, the 'form' editor is selected."); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality 1, the 'form' editor is selected.");
// Editor selection with cardinality >1. // Editor selection with cardinality >1.
$this->field_nr_field['cardinality'] = 2; $this->field_nr_field->cardinality = 2;
field_update_field($this->field_nr_field); $this->field_nr_field->save();
$this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, the 'form' editor is selected."); $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, the 'form' editor is selected.");
} }
......
...@@ -137,8 +137,8 @@ function testEditorSelection() { ...@@ -137,8 +137,8 @@ function testEditorSelection() {
$this->assertEqual('editor', $this->getSelectedEditor($items, $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected."); $this->assertEqual('editor', $this->getSelectedEditor($items, $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected.");
// Editor selection with text processing, cardinality >1 // Editor selection with text processing, cardinality >1
$this->field_textarea_field['cardinality'] = 2; $this->field_textarea_field->cardinality = 2;
field_update_field($this->field_textarea_field); $this->field_textarea_field->save();
$items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
$this->assertEqual('form', $this->getSelectedEditor($items, $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); $this->assertEqual('form', $this->getSelectedEditor($items, $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected.");
} }
......
...@@ -22,6 +22,20 @@ class EmailFieldTest extends WebTestBase { ...@@ -22,6 +22,20 @@ class EmailFieldTest extends WebTestBase {
*/ */
public static $modules = array('node', 'entity_test', 'email', 'field_ui'); public static $modules = array('node', 'entity_test', 'email', 'field_ui');
/**
* A field to use in this test class.
*
* @var \Drupal\field\Plugin\Core\Entity\Field
*/
protected $field;
/**
* The instance used in this test class.
*
* @var \Drupal\field\Plugin\Core\Entity\FieldInstance
*/
protected $instance;
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'E-mail field', 'name' => 'E-mail field',
...@@ -46,21 +60,22 @@ function setUp() { ...@@ -46,21 +60,22 @@ function setUp() {
*/ */
function testEmailField() { function testEmailField() {
// Create a field with settings to validate. // Create a field with settings to validate.
$this->field = array( $field_name = drupal_strtolower($this->randomName());
'field_name' => drupal_strtolower($this->randomName()), $this->field = entity_create('field_entity', array(
'field_name' => $field_name,
'type' => 'email', 'type' => 'email',
); ));
field_create_field($this->field); $this->field->save();
$this->instance = array( $this->instance = entity_create('field_instance', array(
'field_name' => $this->field['field_name'], 'field_name' => $field_name,
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ));
field_create_instance($this->instance); $this->instance->save();
// Create a form display for the default form mode. // Create a form display for the default form mode.
entity_get_form_display('entity_test', 'entity_test', 'default') entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array( ->setComponent($field_name, array(
'type' => 'email_default', 'type' => 'email_default',
'settings' => array( 'settings' => array(
'placeholder' => 'example@example.com', 'placeholder' => 'example@example.com',
...@@ -69,7 +84,7 @@ function testEmailField() { ...@@ -69,7 +84,7 @@ function testEmailField() {
->save(); ->save();
// Create a display for the full view mode. // Create a display for the full view mode.
entity_get_display('entity_test', 'entity_test', 'full') entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], array( ->setComponent($field_name, array(
'type' => 'email_mailto', 'type' => 'email_mailto',
)) ))
->save(); ->save();
...@@ -77,7 +92,7 @@ function testEmailField() { ...@@ -77,7 +92,7 @@ function testEmailField() {
// Display creation form. // Display creation form.
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
$langcode = Language::LANGCODE_NOT_SPECIFIED; $langcode = Language::LANGCODE_NOT_SPECIFIED;
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', 'Widget found.'); $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget found.');
$this->assertRaw('placeholder="example@example.com"'); $this->assertRaw('placeholder="example@example.com"');
// Submit a valid e-mail address and ensure it is accepted. // Submit a valid e-mail address and ensure it is accepted.
...@@ -85,7 +100,7 @@ function testEmailField() { ...@@ -85,7 +100,7 @@ function testEmailField() {
$edit = array( $edit = array(
'user_id' => 1, 'user_id' => 1,
'name' => $this->randomName(), 'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][value]" => $value, "{$field_name}[$langcode][0][value]" => $value,
); );
$this->drupalPost(NULL, $edit, t('Save')); $this->drupalPost(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match); preg_match('|entity_test/manage/(\d+)/edit|', $this->url, $match);
......
...@@ -35,17 +35,15 @@ public function setUp() { ...@@ -35,17 +35,15 @@ public function setUp() {
parent::setUp(); parent::setUp();
// Create an email field and instance for validation. // Create an email field and instance for validation.
$this->field = array( entity_create('field_entity', array(
'field_name' => 'field_email', 'field_name' => 'field_email',
'type' => 'email', 'type' => 'email',
); ))->save();
field_create_field($this->field); entity_create('field_instance', array(
$this->instance = array(
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'field_email', 'field_name' => 'field_email',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ))->save();
field_create_instance($this->instance);
// Create a form display for the default form mode. // Create a form display for the default form mode.
entity_get_form_display('entity_test', 'entity_test', 'default') entity_get_form_display('entity_test', 'entity_test', 'default')
......
...@@ -139,22 +139,23 @@ public function testFieldComponent() { ...@@ -139,22 +139,23 @@ public function testFieldComponent() {
'mode' => 'default', 'mode' => 'default',
)); ));
$field_name = 'test_field';
// Create a field and an instance. // Create a field and an instance.
$field = array( $field = entity_create('field_entity', array(
'field_name' => 'test_field', 'field_name' => $field_name,
'type' => 'test_field' 'type' => 'test_field'
); ));
field_create_field($field); $field->save();
$instance = array( $instance = entity_create('field_instance', array(
'field_name' => $field['field_name'], 'field_name' => $field_name,
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ));
field_create_instance($instance); $instance->save();