Commit 18812513 authored by webchick's avatar webchick

Issue #1312200 by zuuperman, lemark, chx: Fixed field_create_instance() must...

Issue #1312200 by zuuperman, lemark, chx: Fixed field_create_instance() must fill in the id field but does not.
parent 3c66dae8
...@@ -455,7 +455,7 @@ function field_delete_field($field_name) { ...@@ -455,7 +455,7 @@ function field_delete_field($field_name) {
* *
* See: @link field Field API data structures @endlink. * See: @link field Field API data structures @endlink.
*/ */
function field_create_instance($instance) { function field_create_instance(&$instance) {
$field = field_read_field($instance['field_name']); $field = field_read_field($instance['field_name']);
if (empty($field)) { if (empty($field)) {
throw new FieldException(t("Attempt to create an instance of a field @field_name that doesn't exist or is currently inactive.", array('@field_name' => $instance['field_name']))); throw new FieldException(t("Attempt to create an instance of a field @field_name that doesn't exist or is currently inactive.", array('@field_name' => $instance['field_name'])));
...@@ -555,7 +555,7 @@ function field_update_instance($instance) { ...@@ -555,7 +555,7 @@ function field_update_instance($instance) {
* @param $update * @param $update
* Whether this is a new or existing instance. * Whether this is a new or existing instance.
*/ */
function _field_write_instance($instance, $update = FALSE) { function _field_write_instance(&$instance, $update = FALSE) {
$field = field_read_field($instance['field_name']); $field = field_read_field($instance['field_name']);
$field_type = field_info_field_types($field['type']); $field_type = field_info_field_types($field['type']);
...@@ -639,12 +639,12 @@ function _field_write_instance($instance, $update = FALSE) { ...@@ -639,12 +639,12 @@ function _field_write_instance($instance, $update = FALSE) {
// update. // update.
if ($update) { if ($update) {
$record['id'] = $instance['id']; $record['id'] = $instance['id'];
$primary_key = array('id'); drupal_write_record('field_config_instance', $record, array('id'));
} }
else { else {
$primary_key = array(); drupal_write_record('field_config_instance', $record);
$instance['id'] = $record['id'];
} }
drupal_write_record('field_config_instance', $record, $primary_key);
} }
/** /**
......
...@@ -64,6 +64,9 @@ function testCreateFieldInstance() { ...@@ -64,6 +64,9 @@ function testCreateFieldInstance() {
$widget_type = field_info_widget_types($field_type['default_widget']); $widget_type = field_info_widget_types($field_type['default_widget']);
$formatter_type = field_info_formatter_types($field_type['default_formatter']); $formatter_type = field_info_formatter_types($field_type['default_formatter']);
// Check that the ID key is filled in.
$this->assertIdentical($record['id'], $this->instance_definition['id'], 'The instance id is filled in');
// Check that default values are set. // Check that default values are set.
$this->assertIdentical($record['data']['required'], FALSE, t('Required defaults to false.')); $this->assertIdentical($record['data']['required'], FALSE, t('Required defaults to false.'));
$this->assertIdentical($record['data']['label'], $this->instance_definition['field_name'], t('Label defaults to field name.')); $this->assertIdentical($record['data']['label'], $this->instance_definition['field_name'], t('Label defaults to field name.'));
...@@ -140,7 +143,7 @@ function testReadFieldInstance() { ...@@ -140,7 +143,7 @@ function testReadFieldInstance() {
// Read the instance back. // Read the instance back.
$instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']);
$this->assertTrue($this->instance_definition < $instance, t('The field was properly read.')); $this->assertTrue($this->instance_definition == $instance, 'The field was properly read.');
} }
/** /**
......
...@@ -247,7 +247,7 @@ function testFieldFormMultivalueWithRequiredRadio() { ...@@ -247,7 +247,7 @@ function testFieldFormMultivalueWithRequiredRadio() {
'allowed_values' => array('yes' => 'yes', 'no' => 'no'), 'allowed_values' => array('yes' => 'yes', 'no' => 'no'),
), ),
)); ));
field_create_instance(array( $instance = array(
'field_name' => 'required_radio_test', 'field_name' => 'required_radio_test',
'entity_type' => 'test_entity', 'entity_type' => 'test_entity',
'bundle' => 'test_bundle', 'bundle' => 'test_bundle',
...@@ -255,7 +255,8 @@ function testFieldFormMultivalueWithRequiredRadio() { ...@@ -255,7 +255,8 @@ function testFieldFormMultivalueWithRequiredRadio() {
'widget' => array( 'widget' => array(
'type' => 'options_buttons', 'type' => 'options_buttons',
), ),
)); );
field_create_instance($instance);
// Display creation form. // Display creation form.
$this->drupalGet('test-entity/add/test_bundle'); $this->drupalGet('test-entity/add/test_bundle');
......
...@@ -54,7 +54,7 @@ function testDefaultWidgetPropertiesAlter() { ...@@ -54,7 +54,7 @@ function testDefaultWidgetPropertiesAlter() {
'field_name' => 'alter_test_text', 'field_name' => 'alter_test_text',
'type' => 'text', 'type' => 'text',
)); ));
field_create_instance(array( $instance = array(
'field_name' => 'alter_test_text', 'field_name' => 'alter_test_text',
'entity_type' => 'node', 'entity_type' => 'node',
'bundle' => 'article', 'bundle' => 'article',
...@@ -62,7 +62,8 @@ function testDefaultWidgetPropertiesAlter() { ...@@ -62,7 +62,8 @@ function testDefaultWidgetPropertiesAlter() {
'type' => 'text_textfield', 'type' => 'text_textfield',
'size' => 60, 'size' => 60,
), ),
)); );
field_create_instance($instance);
// Test that field_test_field_widget_properties_alter() sets the size to // Test that field_test_field_widget_properties_alter() sets the size to
// 42 and that field_test_field_widget_form_alter() reports the correct // 42 and that field_test_field_widget_form_alter() reports the correct
...@@ -79,22 +80,24 @@ function testDefaultWidgetPropertiesAlter() { ...@@ -79,22 +80,24 @@ function testDefaultWidgetPropertiesAlter() {
'type' => 'list_text' 'type' => 'list_text'
)); ));
// Create instances on users and page nodes. // Create instances on users and page nodes.
field_create_instance(array( $instance = array(
'field_name' => 'alter_test_options', 'field_name' => 'alter_test_options',
'entity_type' => 'user', 'entity_type' => 'user',
'bundle' => 'user', 'bundle' => 'user',
'widget' => array( 'widget' => array(
'type' => 'options_select', 'type' => 'options_select',
) )
)); );
field_create_instance(array( field_create_instance($instance);
$instance = array(
'field_name' => 'alter_test_options', 'field_name' => 'alter_test_options',
'entity_type' => 'node', 'entity_type' => 'node',
'bundle' => 'page', 'bundle' => 'page',
'widget' => array( 'widget' => array(
'type' => 'options_select', 'type' => 'options_select',
) )
)); );
field_create_instance($instance);
// Test that field_test_field_widget_properties_user_alter() replaces // Test that field_test_field_widget_properties_user_alter() replaces
// the widget and that field_test_field_widget_form_alter() reports the // the widget and that field_test_field_widget_form_alter() reports the
......
...@@ -39,11 +39,12 @@ public function setUp() { ...@@ -39,11 +39,12 @@ public function setUp() {
// Add a custom field to the page content type. // Add a custom field to the page content type.
$this->field_name = drupal_strtolower($this->randomName() . '_field_name'); $this->field_name = drupal_strtolower($this->randomName() . '_field_name');
$this->field = field_create_field(array('field_name' => $this->field_name, 'type' => 'text')); $this->field = field_create_field(array('field_name' => $this->field_name, 'type' => 'text'));
$this->instance = field_create_instance(array( $instance = array(
'field_name' => $this->field_name, 'field_name' => $this->field_name,
'entity_type' => 'node', 'entity_type' => 'node',
'bundle' => 'page', 'bundle' => 'page',
)); );
$this->instance = field_create_instance($instance);
} }
/** /**
......
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