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.');
} }
......
...@@ -26,10 +26,17 @@ class DatetimeFieldTest extends WebTestBase { ...@@ -26,10 +26,17 @@ class DatetimeFieldTest extends WebTestBase {
/** /**
* A field to use in this test class. * A field to use in this test class.
* *
* @var \Drupal\Core\Datetime\DrupalDateTime * @var \Drupal\field\Plugin\Core\Entity\Field
*/ */
protected $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' => 'Datetime Field', 'name' => 'Datetime Field',
...@@ -49,22 +56,24 @@ function setUp() { ...@@ -49,22 +56,24 @@ function setUp() {
$this->drupalLogin($web_user); $this->drupalLogin($web_user);
// Create a field with settings to validate. // Create a field with settings to validate.
$this->field = field_create_field(array( $this->field = entity_create('field_entity', array(
'field_name' => drupal_strtolower($this->randomName()), 'field_name' => drupal_strtolower($this->randomName()),
'type' => 'datetime', 'type' => 'datetime',
'settings' => array('datetime_type' => 'date'), 'settings' => array('datetime_type' => 'date'),
)); ));
$this->instance = field_create_instance(array( $this->field->save();
'field_name' => $this->field['field_name'], $this->instance = entity_create('field_instance', array(
'field_name' => $this->field->id(),
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
'settings' => array( 'settings' => array(
'default_value' => 'blank', 'default_value' => 'blank',
), ),
)); ));
$this->instance->save();
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') entity_get_form_display($this->instance->entity_type, $this->instance->bundle, 'default')
->setComponent($this->field['field_name'], array( ->setComponent($this->field->id(), array(
'type' => 'datetime_default', 'type' => 'datetime_default',
)) ))
->save(); ->save();
...@@ -74,8 +83,8 @@ function setUp() { ...@@ -74,8 +83,8 @@ function setUp() {
'label' => 'hidden', 'label' => 'hidden',
'settings' => array('format_type' => 'medium'), 'settings' => array('format_type' => 'medium'),
); );
entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full') entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
->setComponent($this->field['field_name'], $this->display_options) ->setComponent($this->field->id(), $this->display_options)
->save(); ->save();
} }
...@@ -83,12 +92,13 @@ function setUp() { ...@@ -83,12 +92,13 @@ function setUp() {
* Tests date field functionality. * Tests date field functionality.
*/ */
function testDateField() { function testDateField() {
$field_name = $this->field->id();
// 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][date]", '', 'Date element found.'); $this->assertFieldByName("{$field_name}[$langcode][0][value][date]", '', 'Date element found.');
$this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element not found.'); $this->assertNoFieldByName("{$field_name}[$langcode][0][value][time]", '', 'Time element not found.');
// Submit a valid date and ensure it is accepted. // Submit a valid date and ensure it is accepted.
$value = '2012-12-31 00:00:00'; $value = '2012-12-31 00:00:00';
...@@ -100,7 +110,7 @@ function testDateField() { ...@@ -100,7 +110,7 @@ function testDateField() {
$edit = array( $edit = array(
'user_id' => 1, 'user_id' => 1,
'name' => $this->randomName(), 'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][value][date]" => $date->format($date_format), "{$field_name}[$langcode][0][value][date]" => $date->format($date_format),
); );
$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);
...@@ -120,8 +130,8 @@ function testDateField() { ...@@ -120,8 +130,8 @@ function testDateField() {
foreach ($values as $new_value) { foreach ($values as $new_value) {
// Update the entity display settings. // Update the entity display settings.
$this->display_options['settings'] = array($setting => $new_value); $this->display_options['settings'] = array($setting => $new_value);
$display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full') entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
->setComponent($this->instance['field_name'], $this->display_options) ->setComponent($field_name, $this->display_options)
->save(); ->save();
$this->renderTestEntity($id); $this->renderTestEntity($id);
...@@ -138,8 +148,8 @@ function testDateField() { ...@@ -138,8 +148,8 @@ function testDateField() {
// Verify that the plain formatter works. // Verify that the plain formatter works.
$this->display_options['type'] = 'datetime_plain'; $this->display_options['type'] = 'datetime_plain';
$display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full') entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
->setComponent($this->instance['field_name'], $this->display_options) ->setComponent($field_name, $this->display_options)
->save(); ->save();
$expected = $date->format(DATETIME_DATE_STORAGE_FORMAT); $expected = $date->format(DATETIME_DATE_STORAGE_FORMAT);
$this->renderTestEntity($id); $this->renderTestEntity($id);
...@@ -150,16 +160,16 @@ function testDateField() { ...@@ -150,16 +160,16 @@ function testDateField() {
* Tests date and time field. * Tests date and time field.
*/ */
function testDatetimeField() { function testDatetimeField() {
$field_name = $this->field->id();
// Change the field to a datetime field. // Change the field to a datetime field.
$this->field['settings']['datetime_type'] = 'datetime'; $this->field['settings']['datetime_type'] = 'datetime';
field_update_field($this->field); $this->field->save();
// 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][date]", '', 'Date element found.'); $this->assertFieldByName("{$field_name}[$langcode][0][value][date]", '', 'Date element found.');
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.'); $this->assertFieldByName("{$field_name}[$langcode][0][value][time]", '', 'Time element found.');
// Submit a valid date and ensure it is accepted. // Submit a valid date and ensure it is accepted.
$value = '2012-12-31 00:00:00'; $value = '2012-12-31 00:00:00';
...@@ -171,8 +181,8 @@ function testDatetimeField() { ...@@ -171,8 +181,8 @@ function testDatetimeField() {
$edit = array( $edit = array(
'user_id' => 1, 'user_id' => 1,
'name' => $this->randomName(), 'name' => $this->randomName(),
"{$this->field['field_name']}[$langcode][0][value][date]" => $date->format($date_format), "{$field_name}[$langcode][0][value][date]" => $date->format($date_format),
"{$this->field['field_name']}[$langcode][0][value][time]" => $date->format($time_format), "{$field_name}[$langcode][0][value][time]" => $date->format($time_format),
); );
$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);
...@@ -189,8 +199,8 @@ function testDatetimeField() { ...@@ -189,8 +199,8 @@ function testDatetimeField() {
foreach ($values as $new_value) { foreach ($values as $new_value) {
// Update the entity display settings. // Update the entity display settings.
$this->display_options['settings'] = array($setting => $new_value); $this->display_options['settings'] = array($setting => $new_value);
$display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full') entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
->setComponent($this->instance['field_name'], $this->display_options) ->setComponent($field_name, $this->display_options)
->save(); ->save();
$this->renderTestEntity($id); $this->renderTestEntity($id);
...@@ -207,8 +217,8 @@ function testDatetimeField() { ...@@ -207,8 +217,8 @@ function testDatetimeField() {
// Verify that the plain formatter works. // Verify that the plain formatter works.
$this->display_options['type'] = 'datetime_plain'; $this->display_options['type'] = 'datetime_plain';
$display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full') entity_get_display($this->instance->entity_type, $this->instance->bundle, 'full')
->setComponent($this->instance['field_name'], $this->display_options) ->setComponent($field_name, $this->display_options)
->save(); ->save();
$expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT); $expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$this->renderTestEntity($id); $this->renderTestEntity($id);
...@@ -219,14 +229,14 @@ function testDatetimeField() { ...@@ -219,14 +229,14 @@ function testDatetimeField() {
* Tests Date List Widget functionality. * Tests Date List Widget functionality.
*/ */
function testDatelistWidget() { function testDatelistWidget() {
$field_name = $this->field->id();
// Change the field to a datetime field. // Change the field to a datetime field.
$this->field['settings']['datetime_type'] = 'datetime'; $this->field->settings['datetime_type'] = 'datetime';
field_update_field($this->field); $this->field->save();
// Change the widget to a datelist widget. // Change the widget to a datelist widget.
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') entity_get_form_display($this->instance->entity_type, $this->instance->bundle, 'default')
->setComponent($this->instance['field_name'], array( ->setComponent($field_name, array(
'type' => 'datetime_datelist', 'type' => 'datetime_datelist',
'settings' => array( 'settings' => array(
'increment' => 1, 'increment' => 1,
...@@ -239,7 +249,6 @@ function testDatelistWidget() { ...@@ -239,7 +249,6 @@ function testDatelistWidget() {
// Display creation form. // Display creation form.
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
$field_name = $this->field['field_name'];
$langcode = Language::LANGCODE_NOT_SPECIFIED; $langcode = Language::LANGCODE_NOT_SPECIFIED;
$this->assertFieldByXPath("//*[@id=\"edit-$field_name-$langcode-0-value-year\"]", NULL, 'Year element found.'); $this->assertFieldByXPath("//*[@id=\"edit-$field_name-$langcode-0-value-year\"]", NULL, 'Year element found.');
...@@ -258,7 +267,6 @@ function testDatelistWidget() { ...@@ -258,7 +267,6 @@ function testDatelistWidget() {
// Submit a valid date and ensure it is accepted. // Submit a valid date and ensure it is accepted.
$date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15); $date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15);
$date = new DrupalDateTime($date_value);
$edit = array( $edit = array(
'user_id' => 1, 'user_id' => 1,
...@@ -267,7 +275,7 @@ function testDatelistWidget() { ...@@ -267,7 +275,7 @@ function testDatelistWidget() {
// Add the ampm indicator since we are testing 12 hour time. // Add the ampm indicator since we are testing 12 hour time.
$date_value['ampm'] = 'am'; $date_value['ampm'] = 'am';
foreach ($date_value as $part => $value) { foreach ($date_value as $part => $value) {
$edit["{$this->field['field_name']}[$langcode][0][value][$part]"] = $value; $edit["{$field_name}[$langcode][0][value][$part]"] = $value;
} }
$this->drupalPost(NULL, $edit, t('Save')); $this->drupalPost(NULL, $edit, t('Save'));
...@@ -289,13 +297,14 @@ function testDatelistWidget() { ...@@ -289,13 +297,14 @@ function testDatelistWidget() {
function testDefaultValue() { function testDefaultValue() {
// Change the field to a datetime field. // Change the field to a datetime field.
$this->field['settings']['datetime_type'] = 'datetime'; $this->field->settings['datetime_type'] = 'datetime';
field_update_field($this->field); $this->field->save();