diff --git a/core/modules/block/block.install b/core/modules/block/block.install index b1fc925..3edf73f 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -263,7 +263,7 @@ function block_update_8008() { 'module' => 'text', 'cardinality' => 1, ); - _update_7000_field_create_field($body_field); + $body_field = _update_7000_field_create_field($body_field); $instance = array( 'field_name' => 'block_body', diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 48bb5a4..2c6d416 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -16,7 +16,7 @@ function comment_uninstall() { variable_del('comment_block_count'); $node_types = array_keys(node_type_get_types()); foreach ($node_types as $node_type) { - field_attach_delete_bundle('comment', 'comment_node_' . $node_type); + field_attach_delete_bundle('comment', 'comment_node_' . $node_type, FALSE); variable_del('comment_' . $node_type); variable_del('comment_anonymous_' . $node_type); variable_del('comment_controls_' . $node_type); diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php index c508911..9671f43 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php @@ -12,7 +12,7 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\datetime\DateHelper; diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php index cd130d3..087a0ce 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php @@ -12,7 +12,7 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\Core\Datetime\DrupalDateTime; /** diff --git a/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php b/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php index 232d019..06bf6c4 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php @@ -48,13 +48,13 @@ function setUp() { $this->drupalLogin($web_user); // Create a field with settings to validate. - $this->field = array( + $this->field_definition = array( 'field_name' => drupal_strtolower($this->randomName()), 'type' => 'datetime', 'settings' => array('datetime_type' => 'date'), ); - field_create_field($this->field); - $this->instance = array( + $this->field = field_create_field($this->field_definition); + $this->instance_definition = array( 'field_name' => $this->field['field_name'], 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', @@ -65,7 +65,7 @@ function setUp() { 'default_value' => 'blank', ), ); - field_create_instance($this->instance); + $this->instance = field_create_instance($this->instance_definition); $this->display_options = array( 'type' => 'datetime_default', @@ -281,13 +281,15 @@ function testDatelistWidget() { function testDefaultValue() { // Change the field to a datetime field. + $this->field = $this->field_definition; $this->field['settings']['datetime_type'] = 'datetime'; field_update_field($this->field); // Set the default value to 'now'. + $this->instance = $this->instance_definition; $this->instance['settings']['default_value'] = 'now'; $this->instance['default_value_function'] = 'datetime_default_value'; - field_update_instance($this->instance); + $this->instance = field_update_instance($this->instance); // Display creation form. $date = new DrupalDateTime(); @@ -303,7 +305,9 @@ function testDefaultValue() { $this->assertNoFieldByName("{$this->field['field_name']}[$langcode][0][value][time]", '', 'Time element found.'); // Set the default value to 'blank'. + $this->instance = $this->instance_definition; $this->instance['settings']['default_value'] = 'blank'; + $this->instance['default_value_function'] = 'datetime_default_value'; field_update_instance($this->instance); // Display creation form. diff --git a/core/modules/edit/lib/Drupal/edit/EditorBase.php b/core/modules/edit/lib/Drupal/edit/EditorBase.php index d2e4d09..a0844d1 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorBase.php +++ b/core/modules/edit/lib/Drupal/edit/EditorBase.php @@ -9,7 +9,7 @@ use Drupal\Component\Plugin\PluginBase; use Drupal\edit\EditorInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines a base editor (Create.js PropertyEditor widget) implementation. diff --git a/core/modules/edit/lib/Drupal/edit/EditorInterface.php b/core/modules/edit/lib/Drupal/edit/EditorInterface.php index 251233a..df4c7f9 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/EditorInterface.php @@ -8,7 +8,7 @@ namespace Drupal\edit; use Drupal\Component\Plugin\PluginInspectionInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines an interface for in-place editors (Create.js PropertyEditor widgets). diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelector.php b/core/modules/edit/lib/Drupal/edit/EditorSelector.php index 131eea2..fe71340 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorSelector.php +++ b/core/modules/edit/lib/Drupal/edit/EditorSelector.php @@ -9,7 +9,7 @@ use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Utility\NestedArray; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Selects an in-place editor (an Editor plugin) for a field. diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php b/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php index 2c180cd..926c7a6 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php @@ -7,7 +7,7 @@ namespace Drupal\edit; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Interface for selecting an in-place editor (an Editor plugin) for a field. diff --git a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php index 3ec8ce2..3e7dd21 100644 --- a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php +++ b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php @@ -9,7 +9,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Component\Plugin\PluginManagerInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\edit\Access\EditEntityFieldAccessCheckInterface; diff --git a/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php b/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php index 2b6b1d8..71e447d 100644 --- a/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php @@ -8,7 +8,7 @@ namespace Drupal\edit; use Drupal\Core\Entity\EntityInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Interface for generating in-place editing metadata for an entity field. diff --git a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php index e224a01..cd60388 100644 --- a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php +++ b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/DirectEditor.php @@ -9,7 +9,7 @@ use Drupal\edit\EditorBase; use Drupal\Component\Annotation\Plugin; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines the "direct" Create.js PropertyEditor widget. diff --git a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php index e9c453f..f2ba364 100644 --- a/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php +++ b/core/modules/edit/lib/Drupal/edit/Plugin/edit/editor/FormEditor.php @@ -9,7 +9,7 @@ use Drupal\edit\EditorBase; use Drupal\Component\Annotation\Plugin; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines the "form" Create.js PropertyEditor widget. diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php index c3a91fb..810309f 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/EditTestBase.php @@ -29,7 +29,6 @@ function setUp() { parent::setUp(); $this->installSchema('system', 'variable'); - $this->installSchema('field', array('field_config', 'field_config_instance')); $this->installSchema('field_test', 'test_entity'); // Set default storage backend. @@ -59,12 +58,12 @@ function setUp() { */ function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) { $field = $field_name . '_field'; - $this->$field = array( + $this->field = array( 'field_name' => $field_name, 'type' => $type, 'cardinality' => $cardinality, ); - $this->$field_name = field_create_field($this->$field); + $this->$field = field_create_field($this->field); $instance = $field_name . '_instance'; $this->$instance = array( diff --git a/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php b/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php index 81387f0..acc0445 100644 --- a/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php +++ b/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/edit/editor/WysiwygEditor.php @@ -9,7 +9,7 @@ use Drupal\edit\EditorBase; use Drupal\Component\Annotation\Plugin; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Defines the "wysiwyg" Create.js PropertyEditor widget. diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php index 3396f89..ca5ad67 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -24,12 +24,6 @@ public static function getInfo() { ); } - protected function setUp() { - parent::setUp(); - - $this->installSchema('field', array('field_config', 'field_config_instance')); - } - /** * Tests basic CRUD operations on EntityDisplay objects. */ diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 1cf252d..609620f 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -1055,7 +1055,7 @@ function hook_field_attach_delete_revision(\Drupal\Core\Entity\EntityInterface $ */ function hook_field_attach_purge(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance) { // find the corresponding data in mymodule and purge it - if ($entity->entityType() == 'node' && $field->field_name == 'my_field_name') { + if ($entity->entityType() == 'node' && $field['field_name'] == 'my_field_name') { mymodule_remove_mydata($entity->nid); } } @@ -1358,7 +1358,7 @@ function hook_field_storage_details_alter(&$details, $field) { * FIELD_LOAD_REVISION to load the version indicated by each entity. * @param $fields * An array listing the fields to be loaded. The keys of the array are field - * IDs, and the values of the array are the entity IDs (or revision IDs, + * UUIDs, and the values of the array are the entity IDs (or revision IDs, * depending on the $age parameter) to add each field to. * @param $options * An associative array of additional options, with the following keys: @@ -1426,7 +1426,7 @@ function hook_field_storage_load($entity_type, $entities, $age, $fields, $option * FIELD_STORAGE_INSERT when inserting a new entity. * @param $fields * An array listing the fields to be written. The keys and values of the - * array are field IDs. + * array are field UUIDs. */ function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $op, $fields) { $id = $entity->id(); @@ -1520,7 +1520,7 @@ function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $ * The entity on which to operate. * @param $fields * An array listing the fields to delete. The keys and values of the - * array are field IDs. + * array are field UUIDs. */ function hook_field_storage_delete(\Drupal\Core\Entity\EntityInterface $entity, $fields) { foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { @@ -1544,7 +1544,7 @@ function hook_field_storage_delete(\Drupal\Core\Entity\EntityInterface $entity, * The entity on which to operate. * @param $fields * An array listing the fields to delete. The keys and values of the - * array are field IDs. + * array are field UUIDs. */ function hook_field_storage_delete_revision(\Drupal\Core\Entity\EntityInterface $entity, $fields) { $vid = $entity->getRevisionId(); @@ -1768,13 +1768,13 @@ function hook_field_storage_delete_instance($instance) { * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or * FIELD_LOAD_REVISION to load the version indicated by each entity. * @param $skip_fields - * An array keyed by field IDs whose data has already been loaded and + * An array keyed by field UUIDs whose data has already been loaded and * therefore should not be loaded again. Add a key to this array to indicate * that your module has already loaded a field. * @param $options * An associative array of additional options, with the following keys: - * - field_id: The field ID that should be loaded. If unset, all fields should - * be loaded. + * - field_id: The field UUID that should be loaded. If unset, all fields + * should be loaded. * - deleted: If TRUE, deleted fields should be loaded as well as non-deleted * fields. If unset or FALSE, only non-deleted fields should be loaded. */ @@ -1791,11 +1791,11 @@ function hook_field_storage_pre_load($entity_type, $entities, $age, &$skip_field * @param \Drupal\Core\Entity\EntityInterface $entity * The entity with fields to save. * @param $skip_fields - * An array keyed by field IDs whose data has already been written and + * An array keyed by field UUIDs whose data has already been written and * therefore should not be written again. The values associated with these * keys are not specified. * @return - * Saved field IDs are set set as keys in $skip_fields. + * Saved field UUIDs are set as keys in $skip_fields. */ function hook_field_storage_pre_insert(\Drupal\Core\Entity\EntityInterface $entity, &$skip_fields) { if ($entity->entityType() == 'node' && $entity->status && _forum_node_check_node_type($entity)) { @@ -1826,11 +1826,11 @@ function hook_field_storage_pre_insert(\Drupal\Core\Entity\EntityInterface $enti * @param \Drupal\Core\Entity\EntityInterface $entity * The entity with fields to save. * @param $skip_fields - * An array keyed by field IDs whose data has already been written and + * An array keyed by field UUIDs whose data has already been written and * therefore should not be written again. The values associated with these * keys are not specified. * @return - * Saved field IDs are set set as keys in $skip_fields. + * Saved field UUIDs are set as keys in $skip_fields. */ function hook_field_storage_pre_update(\Drupal\Core\Entity\EntityInterface $entity, &$skip_fields) { $first_call = &drupal_static(__FUNCTION__, array()); diff --git a/core/modules/field/field.attach.inc b/core/modules/field/field.attach.inc index 69102f4..455f320 100644 --- a/core/modules/field/field.attach.inc +++ b/core/modules/field/field.attach.inc @@ -1170,7 +1170,7 @@ function field_attach_insert(EntityInterface $entity) { $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; + $field_id = $field['uuid']; $field_name = $field['field_name']; if (!empty($entity->$field_name)) { // Collect the storage backend if the field has not been written yet. @@ -1211,7 +1211,7 @@ function field_attach_update(EntityInterface $entity) { $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; + $field_id = $field['uuid']; $field_name = $field['field_name']; // Leave the field untouched if $entity comes with no $field_name property, // but empty the field if it comes as a NULL value or an empty array. @@ -1254,7 +1254,7 @@ function field_attach_delete(EntityInterface $entity) { $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; + $field_id = $field['uuid']; $storages[$field['storage']['type']][$field_id] = $field_id; } @@ -1287,7 +1287,7 @@ function field_attach_delete_revision(EntityInterface $entity) { $storages = array(); foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) { $field = field_info_field_by_id($instance['field_id']); - $field_id = $field['id']; + $field_id = $field['uuid']; $storages[$field['storage']['type']][$field_id] = $field_id; } @@ -1549,11 +1549,17 @@ function field_attach_create_bundle($entity_type, $bundle) { * The new name of the bundle. */ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { - db_update('field_config_instance') - ->fields(array('bundle' => $bundle_new)) - ->condition('entity_type', $entity_type) - ->condition('bundle', $bundle_old) - ->execute(); + $instances = field_read_instances(); + foreach ($instances as $id => $instance) { + if ($instance['entity_type'] == $entity_type && $instance['bundle'] == $bundle_old) { + $new_instance_id = $instance['entity_type'] . '.' . $bundle_new . '.' . $instance['field_name']; + config('field.instance.' . $instance['entity_type'] . '.' . $bundle_old . '.' . $instance['field_name'])->rename('field.instance.' . $new_instance_id); + config('field.instance.' . $instance['entity_type'] . '.' . $bundle_new . '.' . $instance['field_name']) + ->set('bundle', $bundle_new) + ->set('id', $new_instance_id) + ->save(); + } + } // Clear the cache. field_cache_clear(); @@ -1569,7 +1575,7 @@ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { } /** - * Notifies field.module the a bundle was deleted. + * Notifies field.module that a bundle was deleted. * * This deletes the data for the field instances as well as the field instances * themselves. This function actually just marks the data and field instances as @@ -1582,14 +1588,18 @@ function field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { * The entity type to which the bundle is bound. * @param $bundle * The bundle to delete. + * @param $field_cleanup + * If TRUE, the field will be deleted as well if its last instance is being + * deleted. If FALSE, it is the caller's responsibility to handle the case of + * fields left without instances. Defaults to TRUE. */ -function field_attach_delete_bundle($entity_type, $bundle) { +function field_attach_delete_bundle($entity_type, $bundle, $field_cleanup = TRUE) { // First, delete the instances themselves. field_read_instances() must be // used here since field_info_instances() does not return instances for // disabled entity types or bundles. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => 1)); foreach ($instances as $instance) { - field_delete_instance($instance); + field_delete_instance($instance, $field_cleanup); } // Clear the cache. diff --git a/core/modules/field/field.crud.inc b/core/modules/field/field.crud.inc index f6ee8a2..e383d6c 100644 --- a/core/modules/field/field.crud.inc +++ b/core/modules/field/field.crud.inc @@ -6,6 +6,7 @@ */ use Drupal\Core\Entity\EntityInterface; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field\FieldException; /** @@ -28,8 +29,8 @@ * This function does not bind the field to any bundle; use * field_create_instance() for that. * - * @param $field - * A field definition array. The field_name and type properties are required. + * @param array $field + * A field definition. The field_name and type properties are required. * Other properties, if omitted, will be given the following default values: * - cardinality: 1 * - locked: FALSE @@ -48,157 +49,16 @@ * - settings: each omitted setting is given the default value specified in * hook_field_storage_info(). * - * @return - * The $field array with the id property filled in. + * @return \Drupal\field\Plugin\Core\Entity\Field + * The field entity. * * @throws Drupal\field\FieldException * * See: @link field Field API data structures @endlink. */ -function field_create_field($field) { - // Field name is required. - if (empty($field['field_name'])) { - throw new FieldException('Attempt to create an unnamed field.'); - } - // Field type is required. - if (empty($field['type'])) { - throw new FieldException('Attempt to create a field with no type.'); - } - // Field name cannot contain invalid characters. - if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $field['field_name'])) { - throw new FieldException('Attempt to create a field with invalid characters. Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character'); - } - - // Field name cannot be longer than 32 characters. We use drupal_strlen() - // because the DB layer assumes that column widths are given in characters, - // not bytes. - if (drupal_strlen($field['field_name']) > 32) { - throw new FieldException(t('Attempt to create a field with a name longer than 32 characters: %name', - array('%name' => $field['field_name']))); - } - - // Ensure the field name is unique over active and disabled fields. - // We do not care about deleted fields. - $prior_field = field_read_field($field['field_name'], array('include_inactive' => TRUE)); - if (!empty($prior_field)) { - $message = $prior_field['active']? - t('Attempt to create field name %name which already exists and is active.', array('%name' => $field['field_name'])): - t('Attempt to create field name %name which already exists, although it is inactive.', array('%name' => $field['field_name'])); - throw new FieldException($message); - } - - // Disallow reserved field names. This can't prevent all field name - // collisions with existing entity properties, but some is better - // than none. - foreach (entity_get_info() as $type => $info) { - if (in_array($field['field_name'], $info['entity_keys'])) { - throw new FieldException(t('Attempt to create field name %name which is reserved by entity type %type.', array('%name' => $field['field_name'], '%type' => $type))); - } - } - - $field += array( - 'entity_types' => array(), - 'cardinality' => 1, - 'translatable' => FALSE, - 'locked' => FALSE, - 'settings' => array(), - 'storage' => array(), - 'deleted' => 0, - ); - - // Check that the field type is known. - $field_type = field_info_field_types($field['type']); - if (!$field_type) { - throw new FieldException(t('Attempt to create a field of unknown type %type.', array('%type' => $field['type']))); - } - // Create all per-field-type properties (needed here as long as we have - // settings that impact column definitions). - $field['settings'] += field_info_field_settings($field['type']); - $field['module'] = $field_type['module']; - $field['active'] = 1; - - // Provide default storage. - $field['storage'] += array( - 'type' => variable_get('field_storage_default', 'field_sql_storage'), - 'settings' => array(), - ); - // Check that the storage type is known. - $storage_type = field_info_storage_types($field['storage']['type']); - if (!$storage_type) { - throw new FieldException(t('Attempt to create a field with unknown storage type %type.', array('%type' => $field['storage']['type']))); - } - // Provide default storage settings. - $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']); - $field['storage']['module'] = $storage_type['module']; - $field['storage']['active'] = 1; - // Collect storage information. - module_load_install($field['module']); - $schema = (array) module_invoke($field['module'], 'field_schema', $field); - $schema += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); - // 'columns' are hardcoded in the field type. - $field['columns'] = $schema['columns']; - if (array_intersect(array_keys($field['columns']), field_reserved_columns())) { - throw new FieldException(t('Illegal field type columns.')); - } - // 'foreign keys' are hardcoded in the field type. - $field['foreign keys'] = $schema['foreign keys']; - // 'indexes' can be both hardcoded in the field type, and specified in the - // incoming $field definition. - $field += array( - 'indexes' => array(), - ); - $field['indexes'] += $schema['indexes']; - - // The serialized 'data' column contains everything from $field that does not - // have its own column and is not automatically populated when the field is - // read. - $data = $field; - unset($data['columns'], $data['field_name'], $data['type'], $data['active'], $data['module'], $data['storage_type'], $data['storage_active'], $data['storage_module'], $data['locked'], $data['cardinality'], $data['deleted']); - // Additionally, do not save the 'bundles' property populated by - // field_info_field(). - unset($data['bundles']); - - $record = array( - 'field_name' => $field['field_name'], - 'type' => $field['type'], - 'module' => $field['module'], - 'active' => $field['active'], - 'storage_type' => $field['storage']['type'], - 'storage_module' => $field['storage']['module'], - 'storage_active' => $field['storage']['active'], - 'locked' => $field['locked'], - 'data' => $data, - 'cardinality' => $field['cardinality'], - 'translatable' => $field['translatable'], - 'deleted' => $field['deleted'], - ); - - // Store the field and get the id back. - drupal_write_record('field_config', $record); - $field['id'] = $record['id']; - - // Invoke hook_field_storage_create_field after the field is - // complete (e.g. it has its id). - try { - // Invoke hook_field_storage_create_field after - // drupal_write_record() sets the field id. - module_invoke($storage_type['module'], 'field_storage_create_field', $field); - } - catch (Exception $e) { - // If storage creation failed, remove the field_config record before - // rethrowing the exception. - db_delete('field_config') - ->condition('id', $field['id']) - ->execute(); - throw $e; - } - - // Clear caches - field_cache_clear(TRUE); - - // Invoke external hooks after the cache is cleared for API consistency. - module_invoke_all('field_create_field', $field); - +function field_create_field(array $field) { + $field = entity_create('field_entity', $field); + $field->save(); return $field; } @@ -224,78 +84,21 @@ function field_create_field($field) { * @see field_create_field() */ function field_update_field($field) { - // Check that the specified field exists. - $prior_field = field_read_field($field['field_name']); - if (empty($prior_field)) { - throw new FieldException('Attempt to update a non-existent field.'); - } - - // Use the prior field values for anything not specifically set by the new - // field to be sure that all values are set. - $field += $prior_field; - $field['settings'] += $prior_field['settings']; - - // Some updates are always disallowed. - if ($field['type'] != $prior_field['type']) { - throw new FieldException("Cannot change an existing field's type."); - } - if ($field['entity_types'] != $prior_field['entity_types']) { - throw new FieldException("Cannot change an existing field's entity_types property."); - } - if ($field['storage']['type'] != $prior_field['storage']['type']) { - throw new FieldException("Cannot change an existing field's storage type."); - } - - // Collect the new storage information, since what is in - // $prior_field may no longer be right. - module_load_install($field['module']); - $schema = (array) module_invoke($field['module'], 'field_schema', $field); - $schema += array('columns' => array(), 'indexes' => array()); - // 'columns' are hardcoded in the field type. - $field['columns'] = $schema['columns']; - // 'indexes' can be both hardcoded in the field type, and specified in the - // incoming $field definition. - $field += array( - 'indexes' => array(), - ); - $field['indexes'] += $schema['indexes']; - - $has_data = field_has_data($field); - - // See if any module forbids the update by throwing an exception. - foreach (module_implements('field_update_forbid') as $module) { - $function = $module . '_field_update_forbid'; - $function($field, $prior_field, $has_data); + // Module developers can still pass in an array of properties + // into field_update_field(). + if (is_array($field)) { + $field_loaded = entity_load('field_entity', $field['field_name']); + if (empty($field_loaded)) { + throw new FieldException('Attempt to update a non-existent field.'); + } + // Override settings + foreach ($field as $key => $value) { + $field_loaded[$key] = $value; + } + $field = $field_loaded; } - // Tell the storage engine to update the field. Do this before - // saving the new definition since it still might fail. - $storage_type = field_info_storage_types($field['storage']['type']); - module_invoke($storage_type['module'], 'field_storage_update_field', $field, $prior_field, $has_data); - - // Save the new field definition. @todo: refactor with - // field_create_field. - - // The serialized 'data' column contains everything from $field that does not - // have its own column and is not automatically populated when the field is - // read. - $data = $field; - unset($data['columns'], $data['field_name'], $data['type'], $data['locked'], $data['module'], $data['cardinality'], $data['active'], $data['deleted']); - // Additionally, do not save the 'bundles' property populated by - // field_info_field(). - unset($data['bundles']); - - $field['data'] = $data; - - // Store the field and create the id. - $primary_key = array('id'); - drupal_write_record('field_config', $field, $primary_key); - - // Clear caches - field_cache_clear(TRUE); - - // Invoke external hooks after the cache is cleared for API consistency. - module_invoke_all('field_update_field', $field, $prior_field, $has_data); + $field->save(); } /** @@ -325,11 +128,8 @@ function field_read_field($field_name, $include_additional = array()) { * Reads in fields that match an array of conditions. * * @param array $params - * An array of conditions to match against. Keys are columns from the - * 'field_config' table, values are conditions to match. Additionally, - * conditions on the 'entity_type' and 'bundle' columns from the - * 'field_config_instance' table are supported (select fields having an - * instance on a given bundle). + * An array of conditions to match against. Keys are names of properties found + * in field configuration files, values are conditions to match. * @param array $include_additional * The default behavior of this function is to not return fields that are * inactive or have been deleted. Setting @@ -342,64 +142,49 @@ function field_read_field($field_name, $include_additional = array()) { * field ID, otherwise it is keyed by field name. */ function field_read_fields($params = array(), $include_additional = array()) { - $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC)); - $query->fields('fc'); - - // Turn the conditions into a query. - foreach ($params as $key => $value) { - // Allow filtering on the 'entity_type' and 'bundle' columns of the - // field_config_instance table. - if ($key == 'entity_type' || $key == 'bundle') { - if (empty($fci_join)) { - $fci_join = $query->join('field_config_instance', 'fci', 'fc.id = fci.field_id'); - } - $key = 'fci.' . $key; - } - else { - $key = 'fc.' . $key; - } + $fields = array(); - $query->condition($key, $value); + // Check if we need to retrieve deleted fields. + $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($params['deleted']) && $params['deleted']); + if ($include_deleted) { + $deleted_fields = state()->get('field.field.deleted') ?: array(); } + // Add active and storage active parameters. if (!isset($include_additional['include_inactive']) || !$include_additional['include_inactive']) { - $query - ->condition('fc.active', 1) - ->condition('fc.storage_active', 1); + $params['active'] = 1; + $params['storage.active'] = 1; } - $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']); - if (!$include_deleted) { - $query->condition('fc.deleted', 0); + + // Get configuration fields. + $config_fields = entity_load_multiple('field_entity'); + + // Merge deleted fields. + if (!empty($deleted_fields)) { + $config_fields += $deleted_fields; } - $fields = array(); - $results = $query->execute(); - foreach ($results as $record) { - $field = unserialize($record['data']); - $field['id'] = $record['id']; - $field['field_name'] = $record['field_name']; - $field['type'] = $record['type']; - $field['module'] = $record['module']; - $field['active'] = $record['active']; - $field['storage']['type'] = $record['storage_type']; - $field['storage']['module'] = $record['storage_module']; - $field['storage']['active'] = $record['storage_active']; - $field['locked'] = $record['locked']; - $field['cardinality'] = $record['cardinality']; - $field['translatable'] = $record['translatable']; - $field['deleted'] = $record['deleted']; + foreach ($config_fields as $field) { - module_invoke_all('field_read_field', $field); + // Conditions. + foreach ($params as $key => $value) { + if ($key == 'storage.active') { + $checked_value = $field['storage']['active']; + } + else { + $checked_value = $field[$key]; + } + if ($checked_value != $value) { + continue 2; + } + } - // Populate storage information. - module_load_install($field['module']); - $schema = (array) module_invoke($field['module'], 'field_schema', $field); - $schema += array('columns' => array(), 'indexes' => array()); - $field['columns'] = $schema['columns']; + // Invoke read field. + module_invoke_all('field_read_field', $field); $field_name = $field['field_name']; if ($include_deleted) { - $field_name = $field['id']; + $field_name = $field['uuid']; } $fields[$field_name] = $field; } @@ -413,30 +198,9 @@ function field_read_fields($params = array(), $include_additional = array()) { * The field name to delete. */ function field_delete_field($field_name) { - // Delete all non-deleted instances. - $field = field_info_field($field_name); - if (isset($field['bundles'])) { - foreach ($field['bundles'] as $entity_type => $bundles) { - foreach ($bundles as $bundle) { - $instance = field_info_instance($entity_type, $field_name, $bundle); - field_delete_instance($instance, FALSE); - } - } + if ($field = field_info_field($field_name)) { + $field->delete(); } - - // Mark field data for deletion. - module_invoke($field['storage']['module'], 'field_storage_delete_field', $field); - - // Mark the field for deletion. - db_update('field_config') - ->fields(array('deleted' => 1)) - ->condition('field_name', $field_name) - ->execute(); - - // Clear the cache. - field_cache_clear(TRUE); - - module_invoke_all('field_delete_field', $field); } /** @@ -467,60 +231,16 @@ function field_delete_field($field_name) { * View modes not present in the definition are left empty, and the field * will not be displayed in this mode. * - * @return - * The $instance array with the id property filled in. + * @return \Drupal\field\Plugin\Core\Entity\FieldInstance + * The field instance entity. * * @throws Drupal\field\FieldException * * See: @link field Field API data structures @endlink. */ -function field_create_instance(&$instance) { - $field = field_read_field($instance['field_name']); - 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']))); - } - // Check that the required properties exists. - if (empty($instance['entity_type'])) { - throw new FieldException(t('Attempt to create an instance of field @field_name without an entity type.', array('@field_name' => $instance['field_name']))); - } - if (empty($instance['bundle'])) { - throw new FieldException(t('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $instance['field_name']))); - } - // Check that the field can be attached to this entity type. - if (!empty($field['entity_types']) && !in_array($instance['entity_type'], $field['entity_types'])) { - throw new FieldException(t('Attempt to create an instance of field @field_name on forbidden entity type @entity_type.', array('@field_name' => $instance['field_name'], '@entity_type' => $instance['entity_type']))); - } - - // Set the field id. - $instance['field_id'] = $field['id']; - - // Note that we do *not* prevent creating a field on non-existing bundles, - // because that would break the 'Body as field' upgrade for contrib - // node types. - - // TODO: Check that the widget type is known and can handle the field type ? - // TODO: Check that the formatters are known and can handle the field type ? - // TODO: Check that the display view modes are known for the entity type ? - // Those checks should probably happen in _field_write_instance() ? - // Problem : this would mean that a UI module cannot update an instance with a disabled formatter. - - // Ensure the field instance is unique within the bundle. - // We only check for instances of active fields, since adding an instance of - // a disabled field is not supported. - $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']); - if (!empty($prior_instance)) { - $message = t('Attempt to create an instance of field @field_name on bundle @bundle that already has an instance of that field.', array('@field_name' => $instance['field_name'], '@bundle' => $instance['bundle'])); - throw new FieldException($message); - } - - _field_write_instance($instance); - - // Clear caches - field_cache_clear(); - - // Invoke external hooks after the cache is cleared for API consistency. - module_invoke_all('field_create_instance', $instance); - +function field_create_instance(array $instance) { + $instance = entity_create('field_instance', $instance); + $instance->save(); return $instance; } @@ -542,103 +262,26 @@ function field_create_instance(&$instance) { * @see field_create_instance() */ function field_update_instance($instance) { - // Check that the specified field exists. - $field = field_read_field($instance['field_name']); - if (empty($field)) { - throw new FieldException(t('Attempt to update an instance of a nonexistent field @field.', array('@field' => $instance['field_name']))); - } - - // Check that the field instance exists (even if it is inactive, since we - // want to be able to replace inactive widgets with new ones). - $prior_instance = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'], array('include_inactive' => TRUE)); - if (empty($prior_instance)) { - throw new FieldException(t("Attempt to update an instance of field @field on bundle @bundle that doesn't exist.", array('@field' => $instance['field_name'], '@bundle' => $instance['bundle']))); + // Module developers can still pass in an array of properties + // into field_update_instance(). + if (is_array($instance)) { + $instance_loaded = entity_load('field_instance', $instance['entity_type'] . '.' . $instance['bundle'] . '.' . $instance['field_name']); + if (empty($instance_loaded)) { + throw new FieldException('Attempt to update a non-existent instance.'); + } + // Override settings + foreach ($instance as $key => $value) { + if (is_array($value)) { + $instance_loaded[$key] = $value; + } + else { + $instance_loaded[$key] = $value; + } + } + $instance = $instance_loaded; } - $instance['id'] = $prior_instance['id']; - $instance['field_id'] = $prior_instance['field_id']; - - _field_write_instance($instance, TRUE); - - // Clear caches. - field_cache_clear(); - - module_invoke_all('field_update_instance', $instance, $prior_instance); -} - -/** - * Stores an instance record in the field configuration database. - * - * @param $instance - * An instance structure. - * @param $update - * Whether this is a new or existing instance. - */ -function _field_write_instance(&$instance, $update = FALSE) { - $field = field_read_field($instance['field_name']); - $field_type = field_info_field_types($field['type']); - - // Temporary workaround to allow incoming $instance as arrays or classed - // objects. - // @todo remove once the external APIs have been converted to use - // FieldInstance objects. - if (is_object($instance) && get_class($instance) == 'Drupal\field\FieldInstance') { - $instance = $instance->getArray(); - } - - // Set defaults. - $instance += array( - 'settings' => array(), - 'widget' => array(), - 'required' => FALSE, - 'label' => $instance['field_name'], - 'description' => '', - 'deleted' => 0, - ); - - // Set default instance settings. - $instance['settings'] += field_info_instance_settings($field['type']); - - // Set default widget and settings. - $instance['widget'] += array( - // TODO: what if no 'default_widget' specified ? - 'type' => $field_type['default_widget'], - 'settings' => array(), - ); - // If no weight specified, make sure the field sinks at the bottom. - if (!isset($instance['widget']['weight'])) { - $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], 'form'); - $instance['widget']['weight'] = isset($max_weight) ? $max_weight + 1 : 0; - } - // Check widget module. - $widget_type = field_info_widget_types($instance['widget']['type']); - $instance['widget']['module'] = $widget_type['module']; - $instance['widget']['settings'] += field_info_widget_settings($instance['widget']['type']); - - // The serialized 'data' column contains everything from $instance that does - // not have its own column and is not automatically populated when the - // instance is read. - $data = $instance; - unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']); - - $record = array( - 'field_id' => $instance['field_id'], - 'field_name' => $instance['field_name'], - 'entity_type' => $instance['entity_type'], - 'bundle' => $instance['bundle'], - 'data' => $data, - 'deleted' => $instance['deleted'], - ); - // We need to tell drupal_update_record() the primary keys to trigger an - // update. - if ($update) { - $record['id'] = $instance['id']; - drupal_write_record('field_config_instance', $record, array('id')); - } - else { - drupal_write_record('field_config_instance', $record); - $instance['id'] = $record['id']; - } + $instance->save(); } /** @@ -672,9 +315,9 @@ function field_read_instance($entity_type, $field_name, $bundle, $include_additi * Reads in field instances that match an array of conditions. * * @param $param - * An array of properties to use in selecting a field instance. Valid keys - * include any column of the field_config_instance table. If NULL, all - * instances will be returned. + * An array of properties to use in selecting a field instance. Keys are names + * of properties found in field instance configuration files, values are + * conditions to match. * @param $include_additional * The default behavior of this function is to not return field instances that * have been marked deleted, or whose field is inactive. Setting @@ -685,44 +328,77 @@ function field_read_instance($entity_type, $field_name, $bundle, $include_additi * An array of instances matching the arguments. */ function field_read_instances($params = array(), $include_additional = array()) { - $include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive']; - $include_deleted = isset($include_additional['include_deleted']) && $include_additional['include_deleted']; + $instances = array(); - $query = db_select('field_config_instance', 'fci', array('fetch' => PDO::FETCH_ASSOC)); - $query->join('field_config', 'fc', 'fc.id = fci.field_id'); - $query->fields('fci'); + $deleted_fields = state()->get('field.field.deleted'); + $include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive']; + $include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($params['deleted']) && $params['deleted']); - // Turn the conditions into a query. - foreach ($params as $key => $value) { - $query->condition('fci.' . $key, $value); - } - if (!$include_inactive) { - $query - ->condition('fc.active', 1) - ->condition('fc.storage_active', 1); + if ($include_deleted) { + $deleted_instances = state()->get('field.instance.deleted') ?: array(); } - if (!$include_deleted) { - $query->condition('fc.deleted', 0); - $query->condition('fci.deleted', 0); + + // Get configuration instances. + $config_instances = entity_load_multiple('field_instance'); + // Merge deleted instances. + if (!empty($deleted_instances)) { + $config_instances += $deleted_instances; } - $instances = array(); - $results = $query->execute(); + foreach ($config_instances as $instance) { + + $entity_info = entity_get_info($instance['entity_type']); + + // Get data from the field. If the field is marked as deleted, we + // need to get it from the state storage. + $field = entity_load('field_entity', $instance['field_name']); + if ($include_deleted) { + if (empty($field) && isset($deleted_fields[$instance['field_id']])) { + $field = $deleted_fields[$instance['field_id']]; + } + } + if (empty($field)) { + continue; + } + + // @todo do we really need this ? + $instance['active'] = $field['active']; + $instance['locked'] = $field['locked']; + $instance['type'] = $field['type']; + $instance['module'] = $field['module']; - foreach ($results as $record) { - // Filter out instances on unknown entity types (for instance because the - // module exposing them was disabled). - $entity_info = entity_get_info($record['entity_type']); if ($include_inactive || $entity_info) { - $instance = unserialize($record['data']); - $instance['id'] = $record['id']; - $instance['field_id'] = $record['field_id']; - $instance['field_name'] = $record['field_name']; - $instance['entity_type'] = $record['entity_type']; - $instance['bundle'] = $record['bundle']; - $instance['deleted'] = $record['deleted']; + // Conditions. + if (!$include_inactive) { + // @todo Temporary hack. + $params['field.active'] = 1; + $params['field.storage.active'] = 1; + } + if (!$include_deleted) { + $params['deleted'] = 0; + } + foreach ($params as $key => $value) { + switch ($key) { + case 'field.active': + $checked_value = $field['active']; + break; + + case 'field.storage.active': + $checked_value = $field['storage']['active']; + break; + + default: + $checked_value = $instance[$key]; + break; + } + if ($checked_value != $value) { + continue 2; + } + } + // Invoke read instance. module_invoke_all('field_read_instance', $instance); + $instances[] = $instance; } } @@ -732,36 +408,15 @@ function field_read_instances($params = array(), $include_additional = array()) /** * Marks a field instance and its data for deletion. * - * @param $instance - * An instance structure. + * @param \Drupal\field\Plugin\Core\Entity\FieldInstance $instance + * The field instance. * @param $field_cleanup * If TRUE, the field will be deleted as well if its last instance is being * deleted. If FALSE, it is the caller's responsibility to handle the case of * fields left without instances. Defaults to TRUE. */ -function field_delete_instance($instance, $field_cleanup = TRUE) { - // Mark the field instance for deletion. - db_update('field_config_instance') - ->fields(array('deleted' => 1)) - ->condition('field_name', $instance['field_name']) - ->condition('entity_type', $instance['entity_type']) - ->condition('bundle', $instance['bundle']) - ->execute(); - - // Clear the cache. - field_cache_clear(); - - // Mark instance data for deletion. - $field = field_info_field($instance['field_name']); - module_invoke($field['storage']['module'], 'field_storage_delete_instance', $instance); - - // Let modules react to the deletion of the instance. - module_invoke_all('field_delete_instance', $instance); - - // Delete the field itself if we just deleted its last instance. - if ($field_cleanup && count($field['bundles']) == 0) { - field_delete_field($field['field_name']); - } +function field_delete_instance(FieldInstance $instance, $field_cleanup = TRUE) { + $instance->delete($field_cleanup); } /** @@ -858,6 +513,10 @@ function field_purge_batch($batch_size) { $info = entity_get_info(); foreach ($instances as $instance) { $entity_type = $instance['entity_type']; + // @todo EFQ currently fails on conditions on comment bundle. + if ($entity_type == 'comment') { + continue; + } $ids = (object) array( 'entity_type' => $entity_type, 'bundle' => $instance['bundle'], @@ -866,7 +525,7 @@ function field_purge_batch($batch_size) { $field = field_info_field_by_id($instance['field_id']); // Retrieve some entities. $query = $factory->get($entity_type) - ->condition('id:' . $field['id'] . '.deleted', 1) + ->condition('id:' . $field['uuid'] . '.deleted', 1) ->range(0, $batch_size); // If there's no bundle key, all results will have the same bundle. if (!empty($info[$entity_type]['entity_keys']['bundle'])) { @@ -884,7 +543,7 @@ function field_purge_batch($batch_size) { $ids->entity_id = $entity_id; $entities[$entity_id] = _field_create_entity_from_ids($ids); } - field_attach_load($entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field['id'], 'deleted' => 1)); + field_attach_load($entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field['uuid'], 'deleted' => 1)); foreach ($entities as $entity) { // Purge the data for the entity. field_purge_data($entity, $field, $instance); @@ -897,9 +556,9 @@ function field_purge_batch($batch_size) { } // Retrieve all deleted fields. Any that have no instances can be purged. - $fields = field_read_fields(array('deleted' => 1), array('include_deleted' => 1)); - foreach ($fields as $field) { - $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1)); + $deleted_fields = state()->get('field.field.deleted') ?: array(); + foreach ($deleted_fields as $field) { + $instances = field_read_instances(array('field_id' => $field['uuid']), array('include_deleted' => 1)); if (empty($instances)) { field_purge_field($field); } @@ -946,14 +605,14 @@ function field_purge_data(EntityInterface $entity, $field, $instance) { * The instance record to purge. */ function field_purge_instance($instance) { - db_delete('field_config_instance') - ->condition('id', $instance['id']) - ->execute(); - // Notify the storage engine. $field = field_info_field_by_id($instance['field_id']); module_invoke($field['storage']['module'], 'field_storage_purge_instance', $instance); + $deleted_instances = state()->get('field.instance.deleted'); + unset($deleted_instances[$instance['uuid']]); + state()->set('field.instance.deleted', $deleted_instances); + // Clear the cache. field_info_cache_clear(); @@ -971,14 +630,14 @@ function field_purge_instance($instance) { * The field record to purge. */ function field_purge_field($field) { - $instances = field_read_instances(array('field_id' => $field['id']), array('include_deleted' => 1)); + $instances = field_read_instances(array('field_id' => $field['uuid']), array('include_deleted' => 1)); if (count($instances) > 0) { throw new FieldException(t('Attempt to purge a field @field_name that still has instances.', array('@field_name' => $field['field_name']))); } - db_delete('field_config') - ->condition('id', $field['id']) - ->execute(); + $deleted_fields = state()->get('field.field.deleted'); + unset($deleted_fields[$field['uuid']]); + state()->set('field.field.deleted', $deleted_fields); // Notify the storage engine. module_invoke($field['storage']['module'], 'field_storage_purge_field', $field); diff --git a/core/modules/field/field.info.inc b/core/modules/field/field.info.inc index b422b47..4ec8bfe 100644 --- a/core/modules/field/field.info.inc +++ b/core/modules/field/field.info.inc @@ -5,7 +5,7 @@ * Field Info API, providing information about available fields and field types. */ -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field\FieldInfo; /** diff --git a/core/modules/field/field.info.yml b/core/modules/field/field.info.yml index 8cd31f1..f5c4a7d 100644 --- a/core/modules/field/field.info.yml +++ b/core/modules/field/field.info.yml @@ -3,6 +3,4 @@ description: 'Field API to add fields to entities like nodes and users.' package: Core version: VERSION core: 8.x -dependencies: - - field_sql_storage required: true diff --git a/core/modules/field/field.install b/core/modules/field/field.install index 695fd89..8dac7c6 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -5,162 +5,14 @@ * Install, update, and uninstall functions for the Field module. */ +use Drupal\Component\Uuid\Uuid; +use Drupal\field\Plugin\Core\Entity\Field; + /** * Implements hook_schema(). */ function field_schema() { - // Static (meta) tables. - $schema['field_config'] = array( - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'The primary identifier for a field', - ), - 'field_name' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'description' => 'The name of this field. Non-deleted field names are unique, but multiple deleted fields can have the same name.', - ), - 'type' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'description' => 'The type of this field.', - ), - 'module' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '', - 'description' => 'The module that implements the field type.', - ), - 'active' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'description' => 'Boolean indicating whether the module that implements the field type is enabled.', - ), - 'storage_type' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'description' => 'The storage backend for the field.', - ), - 'storage_module' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '', - 'description' => 'The module that implements the storage backend.', - ), - 'storage_active' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'description' => 'Boolean indicating whether the module that implements the storage backend is enabled.', - ), - 'locked' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - 'description' => '@TODO', - ), - 'data' => array( - 'type' => 'blob', - 'size' => 'big', - 'not null' => TRUE, - 'serialize' => TRUE, - 'description' => 'Serialized data containing the field properties that do not warrant a dedicated column.', - ), - 'cardinality' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - 'translatable' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - 'deleted' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - ), - 'primary key' => array('id'), - 'indexes' => array( - 'field_name' => array('field_name'), - // Used by field_read_fields(). - 'active' => array('active'), - 'storage_active' => array('storage_active'), - 'deleted' => array('deleted'), - // Used by field_sync_field_status(). - 'module' => array('module'), - 'storage_module' => array('storage_module'), - 'type' => array('type'), - 'storage_type' => array('storage_type'), - ), - ); - $schema['field_config_instance'] = array( - 'fields' => array( - 'id' => array( - 'type' => 'serial', - 'not null' => TRUE, - 'description' => 'The primary identifier for a field instance', - ), - 'field_id' => array( - 'type' => 'int', - 'not null' => TRUE, - 'description' => 'The identifier of the field attached by this instance', - ), - 'field_name' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '' - ), - 'entity_type' => array( - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '' - ), - 'bundle' => array( - 'type' => 'varchar', - 'length' => 128, - 'not null' => TRUE, - 'default' => '' - ), - 'data' => array( - 'type' => 'blob', - 'size' => 'big', - 'not null' => TRUE, - 'serialize' => TRUE, - ), - 'deleted' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => TRUE, - 'default' => 0, - ), - ), - 'primary key' => array('id'), - 'indexes' => array( - // Used by field_delete_instance(). - 'field_name_bundle' => array('field_name', 'entity_type', 'bundle'), - // Used by field_read_instances(). - 'deleted' => array('deleted'), - ), - ); + $schema['cache_field'] = drupal_get_schema_unprocessed('system', 'cache'); $schema['cache_field']['description'] = 'Cache table for the Field module to store already built field informations.'; @@ -172,7 +24,7 @@ function field_schema() { * * @ingroup update_api */ -function _update_7000_field_create_field(&$field) { +function _update_7000_field_create_field($field) { // Merge in default values.` $field += array( 'entity_types' => array(), @@ -229,12 +81,16 @@ function _update_7000_field_create_field(&$field) { 'deleted' => (int) $field['deleted'], ); // We don't use drupal_write_record() here because it depends on the schema. - $field['id'] = db_insert('field_config') + $record['id'] = db_insert('field_config') ->fields($record) ->execute(); - // Create storage for the field. - field_sql_storage_field_storage_create_field($field); + // Create storage for the field. This requires a field entity, but cannot use + // the regular entity_create() function here. + $field_entity = new Field($field, 'field_entity'); + field_sql_storage_field_storage_create_field($field_entity); + + return $record; } /** @@ -271,7 +127,6 @@ function _update_7000_field_delete_field($field_name) { ->execute(); } - /** * Deletes an instance and all its data of a field stored in SQL Storage. * @@ -382,15 +237,14 @@ function _update_7000_field_create_instance($field, &$instance) { */ /** - * Reassign all list.module fields to be controlled by options.module. + * Implements hook_update_dependencies(). */ -function field_update_8001() { - db_update('field_config') - ->fields(array( - 'module' => 'options', - )) - ->condition('module', 'list') - ->execute(); +function field_update_dependencies() { + // Convert to config after SQL storage has been updated. + $dependencies['field_sql_storage'][8000] = array( + 'field' => 8002, + ); + return $dependencies; } /** @@ -398,7 +252,7 @@ function field_update_8001() { * * @ingroup config_upgrade */ -function field_update_8002() { +function field_update_8001() { $displays = array(); module_load_install('entity'); @@ -442,7 +296,7 @@ function field_update_8002() { // Migration of 'extra_fields' display settings. Avoid calling // entity_get_info() by fetching the relevant variables directly in the - // cariables table. + // variable table. $variables = array_map('unserialize', db_query("SELECT name, value FROM {variable} WHERE name LIKE '%field_bundle_settings_%'")->fetchAllKeyed()); foreach ($variables as $variable_name => $variable_value) { if (preg_match('/field_bundle_settings_(.*)__(.*)/', $variable_name, $matches)) { @@ -484,6 +338,113 @@ function field_update_8002() { } /** + * Convert fields and instances to config. + */ +function field_update_8002() { + $uuid = new Uuid(); + $manifest_ids = array('fields' => array(), 'instances' => array()); + + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $deleted_instances = state()->get('field.instance.deleted') ?: array(); + + $field_uuids = array(); + $fields = db_query("SELECT * FROM {field_config}")->fetchAll(PDO::FETCH_ASSOC); + foreach ($fields as $field) { + $field['data'] = unserialize($field['data']); + + $config = array( + 'id' => $field['field_name'], + 'uuid' => $uuid->generate(), + 'field_name' => $field['field_name'], + 'type' => $field['type'], + 'module' => $field['module'], + 'active' => $field['active'], + 'settings' => $field['data']['settings'], + 'storage' => array( + 'type' => $field['storage_type'], + 'module' => $field['storage_module'], + 'active' => $field['storage_active'], + 'settings' => $field['data']['storage']['settings'], + ), + 'locked' => $field['locked'], + 'cardinality' => $field['cardinality'], + 'translatable' => $field['translatable'], + 'entity_types' => $field['data']['entity_types'], + 'indexes' => $field['data']['indexes'] ?: array(), + 'status' => 1, + 'langcode' => 'und', + ); + + // Reassign all list.module fields to be controlled by options.module. + if ($config['module'] == 'list') { + $config['module'] = 'options'; + } + + if (!$field['deleted']) { + config('field.field.' . $config['id']) + ->setData($config) + ->save(); + $manifest_ids['fields'][] = $config['id']; + } + else { + $deleted_fields[$config['uuid']] = $config; + } + + // Store with the old field_id so that we can map the instance. + $field_uuids[$field['field_name']] = $config['uuid']; + } + + $instances = db_query("SELECT * FROM {field_config_instance}")->fetchAll(PDO::FETCH_ASSOC); + foreach ($instances as $instance) { + $instance['data'] = unserialize($instance['data']); + + $config = array( + 'id' => $instance['entity_type'] . '.' . $instance['bundle'] . '.' . $instance['field_name'], + 'uuid' => $uuid->generate(), + 'field_id' => $field_uuids[$instance['field_id']], + 'field_name' => $instance['field_name'], + 'entity_type' => $instance['entity_type'], + 'bundle' => $instance['bundle'], + 'label' => $instance['data']['label'], + 'description' => $instance['data']['description'], + 'required' => $instance['data']['required'], + 'default_value' => $instance['data']['default_value'], + 'default_value_function' => $instance['data']['default_value_function'], + 'settings' => $instance['data']['settings'], + 'widget' => $instance['data']['widget'], + 'status' => 1, + 'langcode' => 'und', + ); + + if (!$instance['deleted']) { + config('field.instance.' . $config['id']) + ->setData($instance) + ->save(); + $manifest_ids['instances'][] = $config['id']; + } + else { + $deleted_instances[$config['uuid']] = $config; + } + + // Update file_usage table in case this instance has a default image. + if (!empty($config['settings']['default_image'])) { + db_update('file_usage') + ->fields(array('id' => $config['field_id'])) + ->condition('fid', $config['settings']['default_image']) + ->execute(); + } + } + + // Create the manifest files. + update_config_manifest_add('field.field', $manifest_ids['fields']); + update_config_manifest_add('field.instance', $manifest_ids['instances']); + + // Save the deleted fields and instances in state. + state()->set('field.field.deleted', $deleted_fields); + state()->set('field.instance.deleted', $deleted_instances); +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 11cd9bb..0f187d7 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -334,7 +334,8 @@ function field_cron() { * required if there are any active fields of that type. */ function field_system_info_alter(&$info, $file, $type) { - if ($type == 'module' && module_hook($file->name, 'field_info')) { + // It's not safe to call field_read_fields during maintenance mode. + if ($type == 'module' && module_hook($file->name, 'field_info') && !defined('MAINTENANCE_MODE')) { $fields = field_read_fields(array('module' => $file->name), array('include_deleted' => TRUE)); if ($fields) { $info['required'] = TRUE; @@ -536,48 +537,71 @@ function field_modules_disabled($modules) { } /** - * Refreshes the 'active' and 'storage_active' columns for fields. + * Refreshes the 'active' and 'storage[active]' values for fields. */ function field_sync_field_status() { - // Refresh the 'active' and 'storage_active' columns according to the current - // set of enabled modules. - $modules = array_keys(drupal_container()->get('module_handler')->getModuleList()); - foreach ($modules as $module_name) { - field_associate_fields($module_name); + $module_handler = Drupal::service('module_handler'); + + $modules = $module_handler->getModuleList(); + $fields = field_read_fields(array(), array('include_deleted' => 1 ,'include_inactive' => 1)); + $changed = array(); + + // Set the 'module' and 'active' values for the current set of enabled + // modules. + foreach ($modules as $module => $module_info) { + // Collect field types and storage backends exposed by the module. + $field_types = (array) $module_handler->invoke($module, 'field_info'); + $storage_types = (array) $module_handler->invoke($module, 'field_storage_info'); + + if ($field_types || $storage_types) { + foreach ($fields as $id => &$field) { + // Associate field types. + if (isset($field_types[$field['type']]) && ($field['module'] !== $module || !$field['active'])) { + $field['module'] = $module; + $field['active'] = TRUE; + $changed[$id] = $field; + } + // Associate storage backends. + if (isset($storage_types[$field['storage']['type']]) && ($field['storage']['module'] !== $module || !$field['storage']['active'])) { + $field['storage']['module'] = $module; + $field['storage']['active'] = TRUE; + $changed[$id] = $field; + } + } + } } - db_update('field_config') - ->fields(array('active' => 0)) - ->condition('module', $modules, 'NOT IN') - ->execute(); - db_update('field_config') - ->fields(array('storage_active' => 0)) - ->condition('storage_module', $modules, 'NOT IN') - ->execute(); -} -/** - * Allows a module to update the database for fields and columns it controls. - * - * @param $module - * The name of the module to update on. - */ -function field_associate_fields($module) { - // Associate field types. - $field_types = (array) module_invoke($module, 'field_info'); - if ($field_types) { - db_update('field_config') - ->fields(array('module' => $module, 'active' => 1)) - ->condition('type', array_keys($field_types)) - ->execute(); + // Set fields which field type module or storage module is absent to + // 'inactive'. + foreach ($fields as $id => &$field) { + if (!isset($modules[$field['module']]) && $field['active']) { + $field['active'] = FALSE; + $changed[$id] = $field; + } + if (!isset($modules[$field['storage']['module']]) && $field['storage']['active']) { + $field['storage']['active'] = FALSE; + $changed[$id] = $field; + } } - // Associate storage backends. - $storage_types = (array) module_invoke($module, 'field_storage_info'); - if ($storage_types) { - db_update('field_config') - ->fields(array('storage_module' => $module, 'storage_active' => 1)) - ->condition('storage_type', array_keys($storage_types)) - ->execute(); + + // Store the updated field definitions. + $deleted_fields = state()->get('field.field.deleted') ?: array(); + foreach ($changed as $id => $field) { + if ($field['deleted']) { + $deleted_fields[$id] = $field; + } + else { + config('field.field.' . $field['field_name']) + ->set('module', $field['module']) + ->set('active', $field['active']) + ->set('storage.module', $field['storage']['module']) + ->set('storage.active', $field['storage']['active']) + ->save(); + } } + state()->set('field.field.deleted', $deleted_fields); + + field_cache_clear(); } /** @@ -784,6 +808,24 @@ function field_cache_clear() { } /** + * Generate an a table id for deleted fields. + * + * When a field is a deleted, the tables are renamed to {field_data_field_id} + * and {field_revision_field_id}. To make sure we don't end up with table + * names longer than 64 characters, we hash the uuid and return the first + * 6 characters so we end up with a short unique id. + * + * @param $field + * A field definition. + * + * @return string + * A unique id suffix for the deleted table. + */ +function field_generate_table_id($field) { + return substr(hash('sha256', $field['uuid']), 0, 6); +} + +/** * Filters an HTML string to prevent cross-site-scripting (XSS) vulnerabilities. * * Like filter_xss_admin(), but with a shorter list of allowed tags. @@ -1017,7 +1059,6 @@ function field_get_items(EntityInterface $entity, $field_name, $langcode = NULL) * TRUE if the field has data for any entity; FALSE otherwise. */ function field_has_data($field) { - $field = field_info_field_by_id($field['id']); $columns = array_keys($field['columns']); $factory = drupal_container()->get('entity.query'); foreach ($field['bundles'] as $entity_type => $bundle) { diff --git a/core/modules/field/field.multilingual.inc b/core/modules/field/field.multilingual.inc index a9b33ac..d9b6479 100644 --- a/core/modules/field/field.multilingual.inc +++ b/core/modules/field/field.multilingual.inc @@ -208,7 +208,10 @@ function field_content_languages() { * TRUE if the field can be translated. */ function field_is_translatable($entity_type, $field) { - return $field['translatable'] && field_has_translation_handler($entity_type); + // See http://drupal.org/node/1862758#comment-7035586 + if (!empty($field)) { + return $field['translatable'] && field_has_translation_handler($entity_type); + } } /** diff --git a/core/modules/field/field.views.inc b/core/modules/field/field.views.inc index 6c769cd..63eee2f 100644 --- a/core/modules/field/field.views.inc +++ b/core/modules/field/field.views.inc @@ -72,7 +72,7 @@ function field_views_field_label($field_name) { foreach ($instances as $entity_name => $entity_type) { foreach ($entity_type as $bundle) { if (isset($bundle[$field_name])) { - $label_counter[$bundle[$field_name]['label']] = isset($label_counter[$bundle[$field_name]['label']]) ? ++$label_counter[$bundle[$field_name]['label']] : 1; + $label_counter[$bundle[$field_name]['label']] = isset($label_counter[$bundle[$field_name]['label']]) ? ++$label_counter[$bundle[$field_name]->label] : 1; $all_labels[$entity_name][$bundle[$field_name]['label']] = TRUE; } } @@ -296,10 +296,10 @@ function field_views_field_default_views_data($field) { else { $group = t('@group (historical data)', array('@group' => $group_name)); } - $column_real_name = $field['storage']['details']['sql'][$type][$table][$column]; + $column_real_name = $field['storage details']['sql'][$type][$table][$column]; // Load all the fields from the table by default. - $additional_fields = array_values($field['storage']['details']['sql'][$type][$table]); + $additional_fields = array_values($field['storage details']['sql'][$type][$table]); $data[$table][$column_real_name] = array( 'group' => $group, diff --git a/core/modules/field/lib/Drupal/field/FieldInfo.php b/core/modules/field/lib/Drupal/field/FieldInfo.php index 7ff6850..d6150a5 100644 --- a/core/modules/field/lib/Drupal/field/FieldInfo.php +++ b/core/modules/field/lib/Drupal/field/FieldInfo.php @@ -142,17 +142,23 @@ public function getFieldMap() { $map = array(); - $query = db_select('field_config_instance', 'fci'); - $query->join('field_config', 'fc', 'fc.id = fci.field_id'); - $query->fields('fc', array('type')); - $query->fields('fci', array('field_name', 'entity_type', 'bundle')) - ->condition('fc.active', 1) - ->condition('fc.storage_active', 1) - ->condition('fc.deleted', 0) - ->condition('fci.deleted', 0); - foreach ($query->execute() as $row) { - $map[$row->field_name]['bundles'][$row->entity_type][] = $row->bundle; - $map[$row->field_name]['type'] = $row->type; + // Get active fields. + foreach (config_get_storage_names_with_prefix('field.field') as $config_id) { + $field_config = config($config_id)->get(); + if ($field_config['active'] && $field_config['storage']['active']) { + $fields[$field_config['field_name']] = $field_config; + } + } + // Get field instances. + foreach (config_get_storage_names_with_prefix('field.instance') as $config_id) { + $instance_config = config($config_id)->get(); + $field_name = $instance_config['field_name']; + // Filter out instances of inactive fields, and instances on unknown + // entity types. + if (isset($fields[$field_name])) { + $map[$field_name]['bundles'][$instance_config['entity_type']][] = $instance_config['bundle']; + $map[$field_name]['type'] = $fields[$field_name]['type']; + } } // Save in "static" and persistent caches. @@ -181,7 +187,7 @@ public function getFields() { else { // Collect and prepare fields. foreach (field_read_fields(array(), array('include_deleted' => TRUE)) as $field) { - $this->fieldsById[$field['id']] = $this->prepareField($field); + $this->fieldsById[$field['uuid']] = $this->prepareField($field); } // Store in persistent cache. @@ -191,7 +197,7 @@ public function getFields() { // Fill the name/ID map. foreach ($this->fieldsById as $field) { if (!$field['deleted']) { - $this->fieldIdsByName[$field['field_name']] = $field['id']; + $this->fieldIdsByName[$field['field_name']] = $field['uuid']; } } @@ -229,7 +235,7 @@ public function getInstances($entity_type = NULL) { foreach (field_read_instances() as $instance) { $field = $this->getField($instance['field_name']); $instance = $this->prepareInstance($instance, $field['type']); - $this->bundleInstances[$instance['entity_type']][$instance['bundle']][$instance['field_name']] = new FieldInstance($instance); + $this->bundleInstances[$instance['entity_type']][$instance['bundle']][$instance['field_name']] = $instance; } // Store in persistent cache. @@ -275,8 +281,8 @@ public function getField($field_name) { $field = $this->prepareField($field); // Save in the "static" cache. - $this->fieldsById[$field['id']] = $field; - $this->fieldIdsByName[$field['field_name']] = $field['id']; + $this->fieldsById[$field['uuid']] = $field; + $this->fieldIdsByName[$field['field_name']] = $field['uuid']; return $field; } @@ -309,14 +315,14 @@ public function getFieldById($field_id) { // bundle. // Cache miss: read from definition. - if ($fields = field_read_fields(array('id' => $field_id), array('include_deleted' => TRUE))) { + if ($fields = field_read_fields(array('uuid' => $field_id), array('include_deleted' => TRUE))) { $field = current($fields); $field = $this->prepareField($field); // Store in the static cache. - $this->fieldsById[$field['id']] = $field; + $this->fieldsById[$field['uuid']] = $field; if (!$field['deleted']) { - $this->fieldIdsByName[$field['field_name']] = $field['id']; + $this->fieldIdsByName[$field['field_name']] = $field['uuid']; } return $field; @@ -355,10 +361,10 @@ public function getBundleInstances($entity_type, $bundle) { // Extract the field definitions and save them in the "static" cache. foreach ($info['fields'] as $field) { - if (!isset($this->fieldsById[$field['id']])) { - $this->fieldsById[$field['id']] = $field; + if (!isset($this->fieldsById[$field['uuid']])) { + $this->fieldsById[$field['uuid']] = $field; if (!$field['deleted']) { - $this->fieldIdsByName[$field['field_name']] = $field['id']; + $this->fieldIdsByName[$field['field_name']] = $field['uuid']; } } } @@ -377,27 +383,40 @@ public function getBundleInstances($entity_type, $bundle) { } // Cache miss: collect from the definitions. - $instances = array(); - // Collect the fields in the bundle. - $params = array('entity_type' => $entity_type, 'bundle' => $bundle); - $fields = field_read_fields($params); + // Do not return anything for unknown entity types. + if (entity_get_info($entity_type)) { - // This iterates on non-deleted instances, so deleted fields are kept out of - // the persistent caches. - foreach (field_read_instances($params) as $instance) { - $field = $fields[$instance['field_name']]; + // Collect names of fields and instances involved in the bundle, using the + // field map. The field map is already filtered to active, non-deleted + // fields and instances, so those are kept out of the persistent caches. + $config_ids = array(); + foreach ($this->getFieldMap() as $field_name => $field_data) { + if (isset($field_data['bundles'][$entity_type]) && in_array($bundle, $field_data['bundles'][$entity_type])) { + $config_ids[$field_name] = "$entity_type.$bundle.$field_name"; + } + } - $instance = $this->prepareInstance($instance, $field['type']); - $instances[$field['field_name']] = new FieldInstance($instance); + // Load and prepare the corresponding fields and instances entities. + if ($config_ids) { + $loaded_fields = entity_load_multiple('field_entity', array_keys($config_ids)); + $loaded_instances = entity_load_multiple('field_instance', array_values($config_ids)); - // If the field is not in our global "static" list yet, add it. - if (!isset($this->fieldsById[$field['id']])) { - $field = $this->prepareField($field); + foreach ($loaded_instances as $instance) { + $field = $loaded_fields[$instance['field_name']]; + + $instance = $this->prepareInstance($instance, $field['type']); + $instances[$field['field_name']] = $instance; - $this->fieldsById[$field['id']] = $field; - $this->fieldIdsByName[$field['field_name']] = $field['id']; + // If the field is not in our global "static" list yet, add it. + if (!isset($this->fieldsById[$field['uuid']])) { + $field = $this->prepareField($field); + + $this->fieldsById[$field['uuid']] = $field; + $this->fieldIdsByName[$field['field_name']] = $field['uuid']; + } + } } } @@ -480,20 +499,6 @@ public function prepareField($field) { $field['settings'] += field_info_field_settings($field['type']); $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']); - // Add storage details. - $details = (array) module_invoke($field['storage']['module'], 'field_storage_details', $field); - drupal_alter('field_storage_details', $details, $field); - $field['storage']['details'] = $details; - - // Populate the list of bundles using the field. - $field['bundles'] = array(); - if (!$field['deleted']) { - $map = $this->getFieldMap(); - if (isset($map[$field['field_name']])) { - $field['bundles'] = $map[$field['field_name']]['bundles']; - } - } - return $field; } @@ -553,4 +558,5 @@ public function prepareExtraFields($extra_fields, $entity_type, $bundle) { return $result; } + } diff --git a/core/modules/field/lib/Drupal/field/FieldInstance.php b/core/modules/field/lib/Drupal/field/FieldInstance.php deleted file mode 100644 index ba6ea44..0000000 --- a/core/modules/field/lib/Drupal/field/FieldInstance.php +++ /dev/null @@ -1,135 +0,0 @@ -definition = $definition; - } - - /** - * Returns the Widget plugin for the instance. - * - * @return Drupal\field\Plugin\Type\Widget\WidgetInterface - * The Widget plugin to be used for the instance. - */ - public function getWidget() { - if (empty($this->widget)) { - $widget_properties = $this->definition['widget']; - - // Let modules alter the widget properties. - $context = array( - 'entity_type' => $this->definition['entity_type'], - 'bundle' => $this->definition['bundle'], - 'field' => field_info_field($this->definition['field_name']), - 'instance' => $this, - ); - drupal_alter(array('field_widget_properties', 'field_widget_properties_' . $this->definition['entity_type']), $widget_properties, $context); - - $options = array( - 'instance' => $this, - 'type' => $widget_properties['type'], - 'settings' => $widget_properties['settings'], - 'weight' => $widget_properties['weight'], - ); - $this->widget = drupal_container()->get('plugin.manager.field.widget')->getInstance($options); - } - - return $this->widget; - } - - /** - * Implements ArrayAccess::offsetExists(). - */ - public function offsetExists($offset) { - return isset($this->definition[$offset]) || array_key_exists($offset, $this->definition); - } - - /** - * Implements ArrayAccess::offsetGet(). - */ - public function &offsetGet($offset) { - return $this->definition[$offset]; - } - - /** - * Implements ArrayAccess::offsetSet(). - */ - public function offsetSet($offset, $value) { - if (!isset($offset)) { - // Do nothing; $array[] syntax is not supported by this temporary wrapper. - return; - } - $this->definition[$offset] = $value; - - // If the widget or formatter properties changed, the corrsponding plugins - // need to be re-instanciated. - if ($offset == 'widget') { - unset($this->widget); - } - } - - /** - * Implements ArrayAccess::offsetUnset(). - */ - public function offsetUnset($offset) { - unset($this->definition[$offset]); - - // If the widget or formatter properties changed, the corrsponding plugins - // need to be re-instanciated. - if ($offset == 'widget') { - unset($this->widget); - } - } - - /** - * Returns the instance definition as a regular array. - * - * This is used as a temporary BC layer. - * @todo Remove once the external APIs have been converted to use - * FieldInstance objects. - * - * @return array - * The instance definition as a regular array. - */ - public function getArray() { - return $this->definition; - } - - /** - * Handles serialization of Drupal\field\FieldInstance objects. - */ - public function __sleep() { - return array('definition'); - } - -} diff --git a/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php b/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php new file mode 100644 index 0000000..a9ed2da --- /dev/null +++ b/core/modules/field/lib/Drupal/field/FieldInstanceStorageController.php @@ -0,0 +1,30 @@ +get(); + // In case the field has been deleted, the instance will be deleted by then + // already. + if (!empty($config)) { + parent::importDelete($name, $new_config, $old_config); + } + } + +} diff --git a/core/modules/field/lib/Drupal/field/FieldStorageController.php b/core/modules/field/lib/Drupal/field/FieldStorageController.php new file mode 100644 index 0000000..43f24da --- /dev/null +++ b/core/modules/field/lib/Drupal/field/FieldStorageController.php @@ -0,0 +1,17 @@ +id = $values['field_name']; + + // Provide defaults. + $values += array( + 'settings' => array(), + 'cardinality' => 1, + 'translatable' => FALSE, + 'entity_types' => array(), + 'locked' => FALSE, + 'deleted' => 0, + 'storage' => array(), + 'indexes' => array(), + ); + parent::__construct($values, $entity_type); + } + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties(). + */ + public function getExportProperties() { + $names = array( + 'id', + 'uuid', + 'status', + 'langcode', + 'field_name', + 'type', + 'settings', + 'module', + 'active', + 'entity_types', + 'storage', + 'locked', + 'cardinality', + 'translatable', + 'indexes', + ); + $properties = array(); + foreach ($names as $name) { + $properties[$name] = $this->get($name); + } + return $properties; + } + + /** + * Overrides \Drupal\Core\Entity\Entity::save(). + */ + public function save() { + $module_handler = \Drupal::service('module_handler'); + + // Clear the derived data about the field. + unset($this->schema, $this->storageDetails); + + if ($this->isNew()) { + // Field name cannot be longer than 32 characters. We use drupal_strlen() + // because the DB layer assumes that column widths are given in characters, + // not bytes. + if (drupal_strlen($this->field_name) > 32) { + throw new FieldException(t('Attempt to create a field with a name longer than 32 characters: %name', + array('%name' => $this->field_name))); + } + + // Ensure the field name is unique over active and disabled fields. + // We do not care about deleted fields. + $prior_field = field_read_field($this->field_name, array('include_inactive' => TRUE)); + if (!empty($prior_field)) { + $message = $prior_field->active ? + t('Attempt to create field name %name which already exists and is active.', array('%name' => $this->field_name)): + t('Attempt to create field name %name which already exists, although it is inactive.', array('%name' => $this->field_name)); + throw new FieldException($message); + } + + // Disallow reserved field names. This can't prevent all field name + // collisions with existing entity properties, but some is better + // than none. + foreach (\Drupal::service('plugin.manager.entity')->getDefinitions() as $type => $info) { + if (in_array($this->field_name, $info['entity_keys'])) { + throw new FieldException(t('Attempt to create field name %name which is reserved by entity type %type.', array('%name' => $this->field_name, '%type' => $type))); + } + } + + // Check that the field type is known. + $field_type = field_info_field_types($this->type); + if (!$field_type) { + throw new FieldException(t('Attempt to create a field of unknown type %type.', array('%type' => $this->type))); + } + $this->module = $field_type['module']; + $this->active = 1; + + // Create all per-field-type properties (needed here as long as we have + // settings that impact column definitions). + $this->settings += $field_type['settings']; + + // Provide default storage. + $this->storage += array( + 'type' => variable_get('field_storage_default', 'field_sql_storage'), + 'settings' => array(), + ); + // Check that the storage type is known. + $storage_type = field_info_storage_types($this->storage['type']); + if (!$storage_type) { + throw new FieldException(t('Attempt to create a field with unknown storage type %type.', array('%type' => $this->storage['type']))); + } + $this->storage['module'] = $storage_type['module']; + $this->storage['active'] = 1; + // Provide default storage settings. + $this->storage['settings'] += $storage_type['settings']; + + // Notify the storage backend, + $module_handler->invoke($this->storage['module'], 'field_storage_create_field', array($this)); + + $hook = 'field_create_field'; + $hook_args = array($this); + } + else { + $original = \Drupal::service('plugin.manager.entity') + ->getStorageController($this->entityType) + ->loadUnchanged($this->id()); + + // Some updates are always disallowed. + if ($this->type != $original->type) { + throw new FieldException("Cannot change an existing field's type."); + } + if ($this->entity_types != $original->entity_types) { + throw new FieldException("Cannot change an existing field's entity_types property."); + } + if ($this->storage['type'] != $original->storage['type']) { + throw new FieldException("Cannot change an existing field's storage type."); + } + + // Make sure all settings are present. + $this->settings += $original->settings; + + $has_data = field_has_data($this); + + // See if any module forbids the update by throwing an exception. + $module_handler->invokeAll('field_update_forbid', array($this, $original, $has_data)); + + // Tell the storage engine to update the field. Do this before saving the + // new definition since it still might fail. + $module_handler->invoke($this->storage['module'], 'field_storage_update_field', array($this, $original, $has_data)); + + $hook = 'field_update_field'; + $hook_args = array($this, $original, $has_data); + } + + // Save the configuration. + $result = parent::save(); + field_cache_clear(); + + // Invoke external hooks after the cache is cleared for API consistency. + $module_handler->invokeAll($hook, $hook_args); + + return $result; + } + + /** + * Overrides \Drupal\Core\Entity\Entity::delete(). + */ + public function delete() { + if (!$this->deleted) { + $module_handler = \Drupal::service('module_handler'); + + // Delete all non-deleted instances. + foreach ($this->getBundles() as $entity_type => $bundles) { + foreach ($bundles as $bundle) { + // We need to get inactive instances too (e.g. the entity type is not + // known anymore - case of comment.module being uninstalled). + $instance = field_read_instance($entity_type, $this->field_name, $bundle, array('include_inactive' => TRUE)); + field_delete_instance($instance, FALSE); + } + } + + // Mark field data for deletion. + $module_handler->invoke($this->storage['module'], 'field_storage_delete_field', array($this)); + + // Delete the configuration of this field and save the field configuration + // in the key_value table so we can use it later during field_purge_batch(). + // This makes sure a new field can be created immediately with the same + // name. + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $this->deleted = TRUE; + // @todo we should save the raw CMI data, not the entity. + $deleted_fields[$this->uuid] = $this; + state()->set('field.field.deleted', $deleted_fields); + + parent::delete(); + + // Clear the cache. + field_cache_clear(); + + $module_handler->invokeAll('field_delete_field', array($this)); + } + } + + /** + * Returns the field schema. + * + * @return array + * The field schema, as defined by hook_field_schema(). + */ + public function getSchema() { + if (!isset($this->schema)) { + $module_handler = \Drupal::service('module_handler'); + // @todo Use $module_handler->loadInclude() once + // http://drupal.org/node/1941000 is fixed. + module_load_install($this->module); + $schema = (array) $module_handler->invoke($this->module, 'field_schema', array($this)); + $schema += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); + $schema['indexes'] = $this->indexes + $schema['indexes']; + if (array_intersect(array_keys($schema['columns']), field_reserved_columns())) { + throw new FieldException(t('Illegal field type columns.')); + } + $this->schema = $schema; + } + return $this->schema; + } + + /** + * Returns the storage details for the field. + * + * @return array + * The storage details. @todo document. + */ + public function getStorageDetails() { + if (!isset($this->storageDetails)) { + $module_handler = \Drupal::service('module_handler'); + $details = (array) $module_handler->invoke($this->storage['module'], 'field_storage_details', array($this)); + $module_handler->alter('field_storage_details', $details, $this); + $this->storageDetails = $details; + } + return $this->storageDetails; + } + + /** + * Returns the list of bundles where the field has instances. + * + * @return array + * An array keyed by entity type names, whose values are arrays of bundle + * names. + */ + public function getBundles() { + if (empty($this->deleted)) { + $map = field_info_field_map(); + if (isset($map[$this->field_name]['bundles'])) { + return $map[$this->field_name]['bundles']; + } + } + return array(); + } + + /** + * Implements ArrayAccess::offsetExists(). + */ + public function offsetExists($offset) { + return isset($this->{$offset}) || in_array($offset, array('columns', 'foreign keys', 'bundles', 'storage details')); + } + + /** + * Implements ArrayAccess::offsetGet(). + */ + public function &offsetGet($offset) { + switch ($offset) { + case 'columns': + $this->getSchema(); + return $this->schema['columns']; + + case 'foreign keys': + $this->getSchema(); + return $this->schema['foreign keys']; + + case 'bundles': + $bundles = $this->getBundles(); + return $bundles; + + case 'storage details': + $this->getStorageDetails(); + return $this->storageDetails; + } + + return $this->{$offset}; + } + + /** + * Implements ArrayAccess::offsetSet(). + */ + public function offsetSet($offset, $value) { + if (!in_array($offset, array('columns', 'foreign keys', 'bundles', 'storage details'))) { + $this->{$offset} = $value; + } + } + + /** + * Implements ArrayAccess::offsetUnset(). + */ + public function offsetUnset($offset) { + if (!in_array($offset, array('columns', 'foreign keys', 'bundles', 'storage details'))) { + unset($this->{$offset}); + } + } + +} diff --git a/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php new file mode 100644 index 0000000..609d8ae --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/FieldInstance.php @@ -0,0 +1,377 @@ + $values['field_name']))); + } + if (empty($values['bundle'])) { + throw new FieldException(format_string('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $values['field_name']))); + } + + // Provide defaults. + $values += array( + 'label' => $values['field_name'], + 'description' => '', + 'required' => FALSE, + 'default_value' => array(), + 'default_value_function' => '', + 'settings' => array(), + 'widget' => array(), + 'deleted' => 0, + ); + parent::__construct($values, $entity_type); + } + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties(). + */ + public function getExportProperties() { + $names = array( + 'id', + 'uuid', + 'status', + 'langcode', + 'field_name', + 'field_id', + 'entity_type', + 'bundle', + 'label', + 'description', + 'required', + 'default_value', + 'default_value_function', + 'settings', + 'widget', + ); + $properties = array(); + foreach ($names as $name) { + $properties[$name] = $this->get($name); + } + return $properties; + } + + /** + * Overrides \Drupal\Core\Entity\Entity::save(). + */ + public function save() { + $module_handler = \Drupal::service('module_handler'); + $field = field_read_field($this->field_name); + + if ($this->isNew()) { + if (empty($field)) { + throw new FieldException(format_string("Attempt to save an instance of a field @field_name that doesn't exist or is currently inactive.", array('@field_name' => $this->field_name))); + } + // Check that the field can be attached to this entity type. + if (!empty($field->entity_types) && !in_array($this->entity_type, $field->entity_types)) { + throw new FieldException(format_string('Attempt to create an instance of field @field_name on forbidden entity type @entity_type.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type))); + } + + // Ensure the field instance is unique within the bundle. We only check + // for instances of active fields, since adding an instance of a disabled + // field is not supported. + if (field_read_instance($this->entity_type, $this->field_name, $this->bundle)) { + throw new FieldException(format_string('Attempt to create an instance of field @field_name on bundle @bundle that already has an instance of that field.', array('@field_name' => $this->field_name, '@bundle' => $this->bundle))); + } + + // Set the field uuid. + $this->field_id = $field->uuid; + + // Assign the id. + $this->id = $this->entity_type . '.' . $this->bundle . '.' . $this->field_name; + + $hook = 'field_create_instance'; + $hook_args = array($this); + } + else { + $original = \Drupal::service('plugin.manager.entity') + ->getStorageController($this->entityType) + ->loadUnchanged($this->id()); + + // Some updates are always disallowed. + if ($this->entity_type != $original->entity_type) { + throw new FieldException("Cannot change an existing instance's entity_type."); + } + if ($this->bundle != $original->bundle) { + throw new FieldException("Cannot change an existing instance's bundle."); + } + if ($this->field_name != $original->field_name || $this->field_id != $original->field_id) { + throw new FieldException("Cannot change an existing instance's field."); + } + + $hook = 'field_update_instance'; + $hook_args = array($this, $original); + } + + $field_type_info = field_info_field_types($field->type); + + // Set default instance settings. + $this->settings += $field_type_info['instance_settings']; + + // Set default widget and settings. + $this->widget += array( + 'type' => $field_type_info['default_widget'], + 'settings' => array(), + ); + // Check widget module. + if ($widget_type_info = \Drupal::service('plugin.manager.field.widget')->getDefinition($this->widget['type'])) { + $this->widget['module'] = $widget_type_info['module']; + $this->widget['settings'] += $widget_type_info['settings']; + } + // If no weight specified, make sure the field sinks at the bottom. + if (!isset($this->widget['weight'])) { + $max_weight = field_info_max_weight($this->entity_type, $this->bundle, 'form'); + $this->widget['weight'] = isset($max_weight) ? $max_weight + 1 : 0; + } + + // Save the configuration. + $result = parent::save(); + field_cache_clear(); + + // Invoke external hooks after the cache is cleared for API consistency. + $module_handler->invokeAll($hook, $hook_args); + + return $result; + } + + /** + * Overrides \Drupal\Core\Entity\Entity::delete(). + * + * @param $field_cleanup + * (Optionnal) If TRUE, the field will be deleted as well if its last + * instance is being deleted. If FALSE, it is the caller's responsibility to + * handle the case of fields left without instances. Defaults to TRUE. + */ + public function delete($field_cleanup = TRUE) { + if (!$this->deleted) { + $module_handler = \Drupal::service('module_handler'); + + // Delete the configuration of this instance and save the configuration + // in the key_value table so we can use it later during + // field_purge_batch(). + $deleted_instances = state()->get('field.instance.deleted') ?: array(); + $this->deleted = TRUE; + $deleted_instances[$this->uuid] = $this; + state()->set('field.instance.deleted', $deleted_instances); + + parent::delete(); + + // Clear the cache. + field_cache_clear(); + + // Mark instance data for deletion. + $field = field_info_field($this->field_name); + $module_handler->invoke($field->storage['module'], 'field_storage_delete_instance', array($this)); + + // Let modules react to the deletion of the instance. + $module_handler->invokeAll('field_delete_instance', array($this)); + + // Delete the field itself if we just deleted its last instance. + if ($field_cleanup && count($field->getBundles()) == 0) { + $field->delete(); + } + } + } + + /** + * Returns the Widget plugin for the instance. + * + * @return Drupal\field\Plugin\Type\Widget\WidgetInterface + * The Widget plugin to be used for the instance. + */ + public function getWidget() { + if (empty($this->widgetPlugin)) { + $widget_properties = $this->widget; + + // Let modules alter the widget properties. + $context = array( + 'entity_type' => $this->entity_type, + 'bundle' => $this->bundle, + 'field' => field_info_field($this->field_name), + 'instance' => $this, + ); + drupal_alter(array('field_widget_properties', 'field_widget_properties_' . $this->entity_type), $widget_properties, $context); + + $options = array( + 'instance' => $this, + 'type' => $widget_properties['type'], + 'settings' => $widget_properties['settings'], + 'weight' => $widget_properties['weight'], + ); + $this->widgetPlugin = \Drupal::service('plugin.manager.field.widget')->getInstance($options); + } + + return $this->widgetPlugin; + } + + /** + * Implements ArrayAccess::offsetExists(). + */ + public function offsetExists($offset) { + return isset($this->{$offset}); + } + + /** + * Implements ArrayAccess::offsetGet(). + */ + public function &offsetGet($offset) { + return $this->{$offset}; + } + + /** + * Implements ArrayAccess::offsetSet(). + */ + public function offsetSet($offset, $value) { + $this->{$offset} = $value; + } + + /** + * Implements ArrayAccess::offsetUnset(). + */ + public function offsetUnset($offset) { + unset($this->{$offset}); + } + +} + diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php index 04a038d..e93ab96 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterBase.php @@ -10,7 +10,7 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Base class for 'Field formatter' plugin implementations. diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php index 36693b0..4058810 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterInterface.php @@ -8,7 +8,7 @@ namespace Drupal\field\Plugin\Type\Formatter; use Drupal\Core\Entity\EntityInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field\Plugin\PluginSettingsInterface; /** diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php index 424f272..4057c88 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php @@ -13,7 +13,7 @@ use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; use Drupal\Core\Plugin\Discovery\AlterDecorator; use Drupal\field\Plugin\Type\Formatter\FormatterLegacyDiscoveryDecorator; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Plugin type manager for field formatters. diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php index 12118f8..8df4416 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php @@ -11,7 +11,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\EntityInterface; use Drupal\field\Plugin\PluginSettingsBase; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Base class for 'Field widget' plugin implementations. @@ -54,7 +54,7 @@ * @param Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery * The Discovery class that holds access to the widget implementation * definition. - * @param Drupal\field\FieldInstance $instance + * @param Drupal\field\Plugin\Core\Entity\FieldInstance $instance * The field instance to which the widget is associated. * @param array $settings * The widget settings. diff --git a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php index c0ffc95..8711bbd 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php +++ b/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetInterface.php @@ -8,7 +8,7 @@ namespace Drupal\field\Plugin\Type\Widget; use Drupal\Core\Entity\EntityInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Interface definition for field widget plugins. diff --git a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php index 976aa05..dbd3a35 100644 --- a/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php +++ b/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php @@ -168,8 +168,8 @@ public function query($use_groupby = FALSE) { // Go through the list and determine the actual column name from field api. foreach ($options as $column) { $name = $column; - if (isset($this->field_info['storage']['details']['sql'][$rkey][$this->table][$column])) { - $name = $this->field_info['storage']['details']['sql'][$rkey][$this->table][$column]; + if (isset($this->field_info['storage details']['sql'][$rkey][$this->table][$column])) { + $name = $this->field_info['storage details']['sql'][$rkey][$this->table][$column]; } $fields[$column] = $name; @@ -450,7 +450,6 @@ function fakeFieldInstance($formatter, $formatter_settings) { ), // Set the other fields to their default values. - // @see _field_write_instance(). 'required' => FALSE, 'label' => $field_name, 'description' => '', diff --git a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php index 0635f38..e8d11c0 100644 --- a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php @@ -7,6 +7,8 @@ namespace Drupal\field\Tests; +use Drupal\field\Plugin\Core\Entity\FieldInstance; + /** * Unit test class for field bulk delete and batch purge functionality. */ @@ -164,7 +166,7 @@ function testDeleteFieldInstance() { field_delete_instance($instance); // The instance still exists, deleted. - $instances = field_read_instances(array('field_id' => $field['id'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); + $instances = field_read_instances(array('field_id' => $field['uuid'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($instances), 1, 'There is one deleted instance'); $this->assertEqual($instances[0]['bundle'], $bundle, 'The deleted instance is for the correct bundle'); @@ -191,7 +193,7 @@ function testDeleteFieldInstance() { $ids->entity_id = $entity_id; $entities[$entity_id] = _field_create_entity_from_ids($ids); } - field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field['id'], 'deleted' => 1)); + field_attach_load($this->entity_type, $entities, FIELD_LOAD_CURRENT, array('field_id' => $field['uuid'], 'deleted' => 1)); $this->assertEqual(count($found), 10, 'Correct number of entities found after deleting'); foreach ($entities as $id => $entity) { $this->assertEqual($this->entities[$id]->{$field['field_name']}, $entity->{$field['field_name']}, "Entity $id with deleted data loaded correctly"); @@ -247,19 +249,19 @@ function testPurgeInstance() { $this->checkHooksInvocations($hooks, $actual_hooks); // The instance still exists, deleted. - $instances = field_read_instances(array('field_id' => $field['id'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); + $instances = field_read_instances(array('field_id' => $field['uuid'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($instances), 1, 'There is one deleted instance'); // Purge the instance. field_purge_batch($batch_size); // The instance is gone. - $instances = field_read_instances(array('field_id' => $field['id'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); + $instances = field_read_instances(array('field_id' => $field['uuid'], 'deleted' => 1), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($instances), 0, 'The instance is gone'); // The field still exists, not deleted, because it has a second instance. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1, 'include_inactive' => 1)); - $this->assertTrue(isset($fields[$field['id']]), 'The field exists and is not deleted'); + $fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => 1, 'include_inactive' => 1)); + $this->assertTrue(isset($fields[$field['uuid']]), 'The field exists and is not deleted'); } /** @@ -300,8 +302,8 @@ function testPurgeField() { field_purge_batch(0); // The field still exists, not deleted. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1)); - $this->assertTrue(isset($fields[$field['id']]) && !$fields[$field['id']]['deleted'], 'The field exists and is not deleted'); + $fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => 1)); + $this->assertTrue(isset($fields[$field['uuid']]) && !$fields[$field['uuid']]->deleted, 'The field exists and is not deleted'); // Delete the second instance. $bundle = next($this->bundles); @@ -324,14 +326,14 @@ function testPurgeField() { $this->checkHooksInvocations($hooks, $actual_hooks); // The field still exists, deleted. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1)); - $this->assertTrue(isset($fields[$field['id']]) && $fields[$field['id']]['deleted'], 'The field exists and is deleted'); + $fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => 1)); + $this->assertTrue(isset($fields[$field['uuid']]) && $fields[$field['uuid']]->deleted, 'The field exists and is deleted'); // Purge again to purge the instance and the field. field_purge_batch(0); // The field is gone. - $fields = field_read_fields(array('id' => $field['id']), array('include_deleted' => 1, 'include_inactive' => 1)); + $fields = field_read_fields(array('uuid' => $field['uuid']), array('include_deleted' => 1, 'include_inactive' => 1)); $this->assertEqual(count($fields), 0, 'The field is purged.'); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index f7c3a9c..1a20d14 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -41,28 +41,27 @@ function testCreateField() { 'type' => 'test_field', ); field_test_memorize(); - $field_definition = field_create_field($field_definition); + $create_field = field_create_field($field_definition); $mem = field_test_memorize(); - $this->assertIdentical($mem['field_test_field_create_field'][0][0], $field_definition, 'hook_field_create_field() called with correct arguments.'); + $this->assertIdentical($mem['field_test_field_create_field'][0][0]['field_name'], $field_definition['field_name'], 'hook_field_create_field() called with correct arguments.'); + $this->assertIdentical($mem['field_test_field_create_field'][0][0]['type'], $field_definition['type'], 'hook_field_create_field() called with correct arguments.'); - // Read the raw record from the {field_config_instance} table. - $result = db_query('SELECT * FROM {field_config} WHERE field_name = :field_name', array(':field_name' => $field_definition['field_name'])); - $record = $result->fetchAssoc(); - $record['data'] = unserialize($record['data']); + // Read the configuration. + $field = entity_load('field_entity', $field_definition['field_name']); // Ensure that basic properties are preserved. - $this->assertEqual($record['field_name'], $field_definition['field_name'], 'The field name is properly saved.'); - $this->assertEqual($record['type'], $field_definition['type'], 'The field type is properly saved.'); + $this->assertEqual($field['field_name'], $field_definition['field_name'], 'The field name is properly saved.'); + $this->assertEqual($field['type'], $field_definition['type'], 'The field type is properly saved.'); // Ensure that cardinality defaults to 1. - $this->assertEqual($record['cardinality'], 1, 'Cardinality defaults to 1.'); + $this->assertEqual($field['cardinality'], 1, 'Cardinality defaults to 1.'); // Ensure that default settings are present. $field_type = field_info_field_types($field_definition['type']); - $this->assertIdentical($record['data']['settings'], $field_type['settings'], 'Default field settings have been written.'); + $this->assertEqual($field['settings'], $field_type['settings'], 'Default field settings have been written.'); // Ensure that default storage was set. - $this->assertEqual($record['storage_type'], variable_get('field_storage_default'), 'The field type is properly saved.'); + $this->assertEqual($field['storage']['type'], variable_get('field_storage_default'), 'The field type is properly saved.'); // Guarantee that the name is unique. try { @@ -157,11 +156,10 @@ function testCreateField() { function testCreateFieldFail() { $field_name = 'duplicate'; $field_definition = array('field_name' => $field_name, 'type' => 'test_field', 'storage' => array('type' => 'field_test_storage_failure')); - $query = db_select('field_config')->condition('field_name', $field_name)->countQuery(); + $field = entity_load('field_entity', $field_name); - // The field does not appear in field_config. - $count = $query->execute()->fetchField(); - $this->assertEqual($count, 0, 'A field_config row for the field does not exist.'); + // The field does not exist. + $this->assertFalse($field, 'The field does not exist.'); // Try to create the field. try { @@ -172,9 +170,10 @@ function testCreateFieldFail() { $this->assertTrue(TRUE, 'Field creation (correctly) fails.'); } - // The field does not appear in field_config. - $count = $query->execute()->fetchField(); - $this->assertEqual($count, 0, 'A field_config row for the field does not exist.'); + // The field does not exist. + // $field = config('field.field.' . $field_name)->get(); + $field = entity_load('field_entity', $field_name); + $this->assertFalse($field, 'The field does not exist.'); } /** @@ -219,12 +218,6 @@ function testReadFields() { 'bundle' => 'test_bundle', ); field_create_instance($instance_definition); - - // Check that criteria spanning over the field_config_instance table work. - $fields = field_read_fields(array('entity_type' => $instance_definition['entity_type'], 'bundle' => $instance_definition['bundle'])); - $this->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.'); - $fields = field_read_fields(array('entity_type' => $instance_definition['entity_type'], 'field_name' => $instance_definition['field_name'])); - $this->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.'); } /** @@ -238,8 +231,10 @@ function testFieldIndexes() { ); field_create_field($field_definition); $field = field_read_field($field_definition['field_name']); + $schema = $field->getSchema(); $expected_indexes = array('value' => array('value')); - $this->assertEqual($field['indexes'], $expected_indexes, 'Field type indexes saved by default'); + // @todo This is a tested behavior... indexes are supposed to be saved in the field. + $this->assertEqual($schema['indexes'], $expected_indexes, 'Field type indexes saved by default'); // Check that indexes specified by the field definition override the field // type indexes. @@ -252,8 +247,9 @@ function testFieldIndexes() { ); field_create_field($field_definition); $field = field_read_field($field_definition['field_name']); + $schema = $field->getSchema(); $expected_indexes = array('value' => array()); - $this->assertEqual($field['indexes'], $expected_indexes, 'Field definition indexes override field type indexes'); + $this->assertEqual($schema['indexes'], $expected_indexes, 'Field definition indexes override field type indexes'); // Check that indexes specified by the field definition add to the field // type indexes. @@ -266,8 +262,9 @@ function testFieldIndexes() { ); field_create_field($field_definition); $field = field_read_field($field_definition['field_name']); + $schema = $field->getSchema(); $expected_indexes = array('value' => array('value'), 'value_2' => array('value')); - $this->assertEqual($field['indexes'], $expected_indexes, 'Field definition indexes are merged with field type indexes'); + $this->assertEqual($schema['indexes'], $expected_indexes, 'Field definition indexes are merged with field type indexes'); } /** @@ -303,8 +300,8 @@ function testDeleteField() { // Make sure that the field is marked as deleted when it is specifically // loaded. - $field = field_read_field($this->field['field_name'], array('include_deleted' => TRUE)); - $this->assertTrue(!empty($field['deleted']), 'A deleted field is marked for deletion.'); + $deleted_fields = state()->get('field.field.deleted'); + $this->assertTrue(isset($deleted_fields[$field['uuid']]), 'A deleted field is marked for deletion.'); // Make sure that this field's instance is marked as deleted when it is // specifically loaded. @@ -351,17 +348,6 @@ function testDeleteField() { } } - function testUpdateNonExistentField() { - $test_field = array('field_name' => 'does_not_exist', 'type' => 'number_decimal'); - try { - field_update_field($test_field); - $this->fail(t('Cannot update a field that does not exist.')); - } - catch (FieldException $e) { - $this->pass(t('Cannot update a field that does not exist.')); - } - } - function testUpdateFieldType() { $field = array('field_name' => 'field_type', 'type' => 'number_decimal'); $field = field_create_field($field); @@ -384,18 +370,16 @@ function testUpdateField() { // respected. Since cardinality enforcement is consistent across database // systems, it makes a good test case. $cardinality = 4; - $field_definition = array( + $field = field_create_field(array( 'field_name' => 'field_update', 'type' => 'test_field', 'cardinality' => $cardinality, - ); - $field_definition = field_create_field($field_definition); - $instance = array( + )); + $instance = field_create_instance(array( 'field_name' => 'field_update', 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', - ); - $instance = field_create_instance($instance); + )); do { // We need a unique ID for our entity. $cardinality will do. @@ -410,14 +394,14 @@ function testUpdateField() { // Load back and assert there are $cardinality number of values. $entity = field_test_create_entity($id, $id, $instance['bundle']); field_attach_load('test_entity', array($id => $entity)); - $this->assertEqual(count($entity->field_update[LANGUAGE_NOT_SPECIFIED]), $field_definition['cardinality'], 'Cardinality is kept'); + $this->assertEqual(count($entity->field_update[LANGUAGE_NOT_SPECIFIED]), $field['cardinality'], 'Cardinality is kept'); // Now check the values themselves. for ($delta = 0; $delta < $cardinality; $delta++) { $this->assertEqual($entity->field_update[LANGUAGE_NOT_SPECIFIED][$delta]['value'], $delta, 'Value is kept'); } // Increase $cardinality and set the field cardinality to the new value. - $field_definition['cardinality'] = ++$cardinality; - field_update_field($field_definition); + $field['cardinality'] = ++$cardinality; + field_update_field($field); } while ($cardinality < 6); } @@ -444,4 +428,5 @@ function testUpdateFieldForbid() { $this->pass(t("An unchangeable setting cannot be updated.")); } } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php index 6c7df44..2f51aa9 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php @@ -117,7 +117,7 @@ function testFieldAttachLoadMultiple() { $field_names[$i] = 'field_' . $i; $field = array('field_name' => $field_names[$i], 'type' => 'test_field'); $field = field_create_field($field); - $field_ids[$i] = $field['id']; + $field_ids[$i] = $field['uuid']; foreach ($field_bundles_map[$i] as $bundle) { $instance = array( 'field_name' => $field_names[$i], @@ -242,9 +242,9 @@ function testFieldStorageDetailsAlter() { $instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']); // The storage details are indexed by a storage engine type. - $this->assertTrue(array_key_exists('drupal_variables', $field['storage']['details']), 'The storage type is Drupal variables.'); + $this->assertTrue(array_key_exists('drupal_variables', $field['storage details']), 'The storage type is Drupal variables.'); - $details = $field['storage']['details']['drupal_variables']; + $details = $field['storage details']['drupal_variables']; // The field_test storage details are indexed by variable name. The details // are altered, so moon and mars are correct for this test. @@ -253,7 +253,7 @@ function testFieldStorageDetailsAlter() { // Test current and revision storage details together because the columns // are the same. - foreach ((array) $field['columns'] as $column_name => $attributes) { + foreach ($field['columns'] as $column_name => $attributes) { $this->assertEqual($details[FIELD_LOAD_CURRENT]['moon'][$column_name], $column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => 'moon[FIELD_LOAD_CURRENT]'))); $this->assertEqual($details[FIELD_LOAD_REVISION]['mars'][$column_name], $column_name, format_string('Column name %value matches the definition in %bin.', array('%value' => $column_name, '%bin' => 'mars[FIELD_LOAD_REVISION]'))); } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldImportChangeTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldImportChangeTest.php new file mode 100644 index 0000000..871b4ee --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldImportChangeTest.php @@ -0,0 +1,68 @@ + 'Field config change tests', + 'description' => 'Update field and instances during config change method invocation.', + 'group' => 'Field API', + ); + } + + function setUp() { + parent::setUp(); + $this->instance_manifest = 'manifest.field.instance'; + $this->instance_name = 'field.instance.test_entity.test_bundle.field_test_import'; + } + + /** + * Test importing changes. + */ + function testImportChange() { + + // Import default config. + config_install_default_config('module', 'field_test_config'); + + // Assert default test import. + $field = entity_load('field_entity', 'field_test_import'); + $this->assertTrue($field, 'Test import field exists'); + $instance = entity_load('field_instance', 'test_entity.test_bundle.field_test_import'); + $this->assertTrue($instance, 'Test import field instance exists'); + + // Change label. + $active = $this->container->get('config.storage'); + $staging = $this->container->get('config.storage.staging'); + $manifest = $active->read($this->instance_manifest); + $instance = $active->read($this->instance_name); + $new_label = 'Test update import field'; + $instance['label'] = $new_label; + $staging->write($this->instance_name, $instance); + $staging->write($this->instance_manifest, $manifest); + + // Import. + config_import(); + + // Assert updated label. + $instance = entity_load('field_instance', 'test_entity.test_bundle.field_test_import'); + $this->assertEqual($instance['label'], $new_label, 'Instance label updated'); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldImportCreateTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldImportCreateTest.php new file mode 100644 index 0000000..76aa9c3 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldImportCreateTest.php @@ -0,0 +1,86 @@ + 'Field config create tests', + 'description' => 'Create field and instances during config create method invocation.', + 'group' => 'Field API', + ); + } + + function setUp() { + parent::setUp(); + + $this->field_test_import_staging = 'field.field.field_test_import_staging'; + $this->instance_test_import_staging = 'field.instance.test_entity.test_bundle.field_test_import_staging'; + $this->field_manifest = 'manifest.field.field'; + $this->instance_manifest = 'manifest.field.instance'; + } + + /** + * Test importing new fields. + */ + function testImportCreate() { + + $field = entity_load('field_entity', 'field_test_import'); + $this->assertFalse($field); + + // Enable field_test_config module and assert the test import + // field and instance is available on the Test content type. + // This tests creating fields and instances that are provided + // by a module. + module_enable(array('field_test_config')); + $field = entity_load('field_entity', 'field_test_import'); + $this->assertTrue($field, 'Test import field exists'); + $instance = entity_load('field_instance', 'test_entity.test_bundle.field_test_import'); + $this->assertTrue($instance, 'Test import field instance exists'); + + $module_path = drupal_get_path('module', 'field_test_config'); + + // Copy another field and instance to the staging directory + // on the Test content type and run config_import() to test + // importing from the staging directory. + $active = $this->container->get('config.storage'); + $staging = $this->container->get('config.storage.staging'); + $field_manifest = $active->read($this->field_manifest); + $instance_manifest = $active->read($this->instance_manifest); + + // Copy the files. + $copied = file_unmanaged_copy($module_path .'/staging/' . $this->field_test_import_staging . '.yml', 'public://config_staging/' . $this->field_test_import_staging . '.yml'); + $this->assertTrue($copied); + $copied = file_unmanaged_copy($module_path .'/staging/' . $this->instance_test_import_staging . '.yml', 'public://config_staging/' . $this->instance_test_import_staging . '.yml'); + $this->assertTrue($copied); + + // Add to manifest. + $field_manifest['field_test_import_staging'] = array('name' => $this->field_test_import_staging); + $instance_manifest['node.test_import.field_test_import_staging'] = array('name' => $this->instance_test_import_staging); + + // Write to manifest and new config. + $staging->write($this->field_manifest, $field_manifest); + $staging->write($this->instance_manifest, $instance_manifest); + + // Import. + config_import(); + + // Assert the staging field is there. + $field = entity_load('field_entity', 'field_test_import_staging'); + $this->assertTrue($field, 'Test import field from staging exists'); + $instance = entity_load('field_instance', 'test_entity.test_bundle.field_test_import_staging'); + $this->assertTrue($instance, 'Test import field instance from staging exists'); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldImportDeleteTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldImportDeleteTest.php new file mode 100644 index 0000000..f126a0f --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Tests/FieldImportDeleteTest.php @@ -0,0 +1,87 @@ + 'Field config delete tests', + 'description' => 'Delete field and instances during config delete method invocation.', + 'group' => 'Field API', + ); + } + + function setUp() { + parent::setUp(); + + $this->test_import_field_name = 'field.field.test_import'; + $this->test_import_instance_name = 'field.instance.node.test_import.field_test_import'; + $this->field_manifest = 'manifest.field.field'; + $this->instance_manifest = 'manifest.field.instance'; + } + + /** + * Test importing deletions. + */ + function testImportDelete() { + + // Import default config. + config_install_default_config('module', 'field_test_config'); + + // Check the field exists. + $field_test_import = field_info_field('field_test_import'); + + // Delete body field and instance, the test import instance + // from the manifest. + $active = $this->container->get('config.storage'); + $staging = $this->container->get('config.storage.staging'); + $field_manifest = $active->read($this->field_manifest); + unset($field_manifest['field_test_import']); + $instance_manifest = $active->read($this->instance_manifest); + unset($instance_manifest['test_entity.test_bundle.field_test_import']); + $staging->write($this->field_manifest, $field_manifest); + $staging->write($this->instance_manifest, $instance_manifest); + + // Import. + config_import(); + + // Assert the field and instance are gone. + $field = entity_load('field_entity', 'field_test_import', TRUE); + $this->assertFalse($field, 'Test import field is removed.'); + $instance = entity_load('field_instance', 'test_entity.test_bundle.field_test_import', TRUE); + $this->assertFalse($instance, 'Test import field instance is removed.'); + + // Check that import field is in state of deleted fields. + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $this->assertTrue(isset($deleted_fields[$field_test_import->uuid])); + + // Run purge_batch(). + field_purge_batch(10); + + // Check that the deleted fields are removed from state. + $deleted_fields = state()->get('field.field.deleted') ?: array(); + $this->assertTrue(empty($deleted_fields), 'Fields are deleted'); + + // Check all config files are gone. + $active = $this->container->get('config.storage'); + $this->assertIdentical($active->listAll($this->test_import_field_name), array()); + $this->assertIdentical($active->listAll($this->test_import_instance_name), array()); + } +} diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php index ea6f26e..165d8e9 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php @@ -136,22 +136,17 @@ function testFieldPrepare() { // Simulate a stored field definition missing a field setting (e.g. a // third-party module adding a new field setting has been enabled, and // existing fields do not know the setting yet). - $data = db_query('SELECT data FROM {field_config} WHERE field_name = :field_name', array(':field_name' => $field_definition['field_name']))->fetchField(); - $data = unserialize($data); - $data['settings'] = array(); - db_update('field_config') - ->fields(array('data' => serialize($data))) - ->condition('field_name', $field_definition['field_name']) - ->execute(); - - field_cache_clear(); + config('field.field.' . $field_definition['field_name']) + ->set('settings', array()) + ->save(); + field_info_cache_clear(); // Read the field back. $field = field_info_field($field_definition['field_name']); // Check that all expected settings are in place. $field_type = field_info_field_types($field_definition['type']); - $this->assertIdentical($field['settings'], $field_type['settings'], 'All expected default field settings are present.'); + $this->assertEqual($field['settings'], $field_type['settings'], 'All expected default field settings are present.'); } /** @@ -173,25 +168,18 @@ function testInstancePrepare() { // Simulate a stored instance definition missing various settings (e.g. a // third-party module adding instance or widget settings has been enabled, // but existing instances do not know the new settings). - $data = db_query('SELECT data FROM {field_config_instance} WHERE field_name = :field_name AND bundle = :bundle', array(':field_name' => $instance_definition['field_name'], ':bundle' => $instance_definition['bundle']))->fetchField(); - $data = unserialize($data); - $data['settings'] = array(); - $data['widget']['settings'] = 'unavailable_widget'; - $data['widget']['settings'] = array(); - db_update('field_config_instance') - ->fields(array('data' => serialize($data))) - ->condition('field_name', $instance_definition['field_name']) - ->condition('bundle', $instance_definition['bundle']) - ->execute(); - - field_cache_clear(); + $instance = entity_load('field_instance', $instance_definition['entity_type'] . '.' . $instance_definition['bundle'] . '.' . $instance_definition['field_name']); + $instance['settings'] = array(); + $instance['widget']['settings'] = 'unavailable_widget'; + $instance['widget']['settings'] = array(); + field_update_instance($instance); // Read the instance back. $instance = field_info_instance($instance_definition['entity_type'], $instance_definition['field_name'], $instance_definition['bundle']); // Check that all expected instance settings are in place. $field_type = field_info_field_types($field_definition['type']); - $this->assertIdentical($instance['settings'], $field_type['instance_settings'] , 'All expected instance settings are present.'); + $this->assertEqual($instance['settings'], $field_type['instance_settings'] , 'All expected instance settings are present.'); // Check that the default widget is used and expected settings are in place. $widget = $instance->getWidget(); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php index ee30cf1..9d8390f 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php @@ -8,6 +8,7 @@ namespace Drupal\field\Tests; use Drupal\field\FieldException; +use Drupal\field\Plugin\Core\Entity\FieldInstance; class FieldInstanceCrudTest extends FieldUnitTestBase { @@ -48,26 +49,24 @@ function setUp() { function testCreateFieldInstance() { field_create_instance($this->instance_definition); - // Read the raw record from the {field_config_instance} table. - $result = db_query('SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = :bundle', array(':field_name' => $this->instance_definition['field_name'], ':bundle' => $this->instance_definition['bundle'])); - $record = $result->fetchAssoc(); - $record['data'] = unserialize($record['data']); + // Read the configuration. + $instance = entity_load('field_instance', $this->instance_definition['entity_type'] . '.' . $this->instance_definition['bundle'] . '.' . $this->instance_definition['field_name']); $field_type = field_info_field_types($this->field['type']); $widget_type = field_info_widget_types($field_type['default_widget']); // Check that the ID key is filled in. - $this->assertIdentical($record['id'], $this->instance_definition['id'], 'The instance id is filled in'); + //$this->assertIdentical($record['id'], $this->instance_definition['id'], 'The instance id is filled in'); // Check that default values are set. - $this->assertIdentical($record['data']['required'], FALSE, 'Required defaults to false.'); - $this->assertIdentical($record['data']['label'], $this->instance_definition['field_name'], 'Label defaults to field name.'); - $this->assertIdentical($record['data']['description'], '', 'Description defaults to empty string.'); - $this->assertIdentical($record['data']['widget']['type'], $field_type['default_widget'], 'Default widget has been written.'); + $this->assertEqual($instance['required'], FALSE, 'Required defaults to false.'); + $this->assertIdentical($instance['label'], $this->instance_definition['field_name'], 'Label defaults to field name.'); + $this->assertIdentical($instance['description'], '', 'Description defaults to empty string.'); + $this->assertIdentical($instance['widget']['type'], $field_type['default_widget'], 'Default widget has been written.'); // Check that default settings are set. - $this->assertIdentical($record['data']['settings'], $field_type['instance_settings'] , 'Default instance settings have been written.'); - $this->assertIdentical($record['data']['widget']['settings'], $widget_type['settings'] , 'Default widget settings have been written.'); + $this->assertEqual($instance['settings'], $field_type['instance_settings'] , 'Default instance settings have been written.'); + $this->assertIdentical($instance['widget']['settings'], $widget_type['settings'] , 'Default widget settings have been written.'); // Guarantee that the field/bundle combination is unique. try { @@ -132,7 +131,9 @@ function testReadFieldInstance() { // Read the instance back. $instance = field_read_instance('test_entity', $this->instance_definition['field_name'], $this->instance_definition['bundle']); - $this->assertTrue($this->instance_definition == $instance, 'The field was properly read.'); + $this->assertTrue($this->instance_definition['field_name'] == $instance['field_name'], 'The field was properly read.'); + $this->assertTrue($this->instance_definition['entity_type'] == $instance['entity_type'], 'The field was properly read.'); + $this->assertTrue($this->instance_definition['bundle'] == $instance['bundle'], 'The field was properly read.'); } /** @@ -206,7 +207,9 @@ function testDeleteFieldInstance() { // Make sure the field is deleted when its last instance is deleted. field_delete_instance($another_instance); - $field = field_read_field($another_instance['field_name'], array('include_deleted' => TRUE)); - $this->assertTrue(!empty($field['deleted']), 'A deleted field is marked for deletion after all its instances have been marked for deletion.'); + $deleted_fields = state()->get('field.field.deleted'); + $this->assertTrue(isset($deleted_fields[$another_instance['field_id']]), 'A deleted field is marked for deletion.'); + $field = field_read_field(array('field_name' => $another_instance['field_name'])); + $this->assertFalse($field, 'The field marked to be deleted is not found anymore in the configuration.'); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php index 94b068d..3e1bf38 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldUnitTestBase.php @@ -35,8 +35,7 @@ */ function setUp() { parent::setUp(); - $this->installSchema('system', array('sequences', 'variable')); - $this->installSchema('field', array('field_config', 'field_config_instance')); + $this->installSchema('system', array('sequences', 'variable', 'config_snapshot')); $this->installSchema('entity_test', 'entity_test'); $this->installSchema('field_test', array('test_entity', 'test_entity_revision', 'test_entity_bundle')); @@ -60,7 +59,7 @@ function createFieldWithInstance($suffix = '') { $this->$field_name = drupal_strtolower($this->randomName() . '_field_name' . $suffix); $this->$field = array('field_name' => $this->$field_name, 'type' => 'test_field', 'cardinality' => 4); $this->$field = field_create_field($this->$field); - $this->$field_id = $this->{$field}['id']; + $this->$field_id = $this->{$field}['uuid']; $this->$instance = array( 'field_name' => $this->$field_name, 'entity_type' => 'test_entity', diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php index 609d70f..1980a3f 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php @@ -43,21 +43,21 @@ function setUp() { $this->entity_type = 'test_entity'; - $field = array( + $this->field_definition = array( 'field_name' => $this->field_name, 'type' => 'test_field', 'cardinality' => 4, 'translatable' => TRUE, ); - field_create_field($field); + field_create_field($this->field_definition); $this->field = field_read_field($this->field_name); - $instance = array( + $this->instance_definition = array( 'field_name' => $this->field_name, 'entity_type' => $this->entity_type, 'bundle' => 'test_bundle', ); - field_create_instance($instance); + field_create_instance($this->instance_definition); $this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle'); for ($i = 0; $i < 3; ++$i) { @@ -75,7 +75,7 @@ function setUp() { function testFieldAvailableLanguages() { // Test 'translatable' fieldable info. field_test_entity_info_translatable('test_entity', FALSE); - $field = $this->field; + $field = clone($this->field); $field['field_name'] .= '_untranslatable'; // Enable field translations for the entity. @@ -249,9 +249,9 @@ function testTranslatableFieldSaveLoad() { // Test default values. $field_name_default = drupal_strtolower($this->randomName() . '_field_name'); - $field = $this->field; + $field = $this->field_definition; $field['field_name'] = $field_name_default; - $instance = $this->instance; + $instance = $this->instance_definition; $instance['field_name'] = $field_name_default; $default = rand(1, 127); $instance['default_value'] = array(array('value' => $default)); diff --git a/core/modules/field/tests/modules/field_test/field_test.storage.inc b/core/modules/field/tests/modules/field_test/field_test.storage.inc index a53b941..b8b8549 100644 --- a/core/modules/field/tests/modules/field_test/field_test.storage.inc +++ b/core/modules/field/tests/modules/field_test/field_test.storage.inc @@ -89,7 +89,7 @@ function field_test_field_storage_load($entity_type, $entities, $age, $fields, $ foreach ($fields as $field_id => $ids) { $field = field_info_field_by_id($field_id); $field_name = $field['field_name']; - $field_data = $data[$field['id']]; + $field_data = $data[$field['uuid']]; $sub_table = $load_current ? 'current' : 'revisions'; $delta_count = array(); foreach ($field_data[$sub_table] as $row) { @@ -206,7 +206,7 @@ function field_test_field_storage_delete(EntityInterface $entity, $fields) { function field_test_field_storage_purge(EntityInterface $entity, $field, $instance) { $data = _field_test_storage_data(); - $field_data = &$data[$field['id']]; + $field_data = &$data[$field['uuid']]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as $key => $row) { if ($row->type == $entity->entityType() && $row->entity_id == $entity->id()) { @@ -249,7 +249,7 @@ function field_test_field_storage_query($field_id, $conditions, $count, &$cursor $field = field_info_field_by_id($field_id); $field_columns = array_keys($field['columns']); - $field_data = $data[$field['id']]; + $field_data = $data[$field['uuid']]; $sub_table = $load_current ? 'current' : 'revisions'; // We need to sort records by entity type and entity id. usort($field_data[$sub_table], '_field_test_field_storage_query_sort_helper'); @@ -267,7 +267,7 @@ function field_test_field_storage_query($field_id, $conditions, $count, &$cursor break; } - if ($row->field_id == $field['id']) { + if ($row->field_id == $field['uuid']) { $match = TRUE; $condition_deleted = FALSE; // Add conditions. @@ -372,7 +372,7 @@ function field_test_field_storage_create_field($field) { $data = _field_test_storage_data(); - $data[$field['id']] = array( + $data[$field['uuid']] = array( 'current' => array(), 'revisions' => array(), ); @@ -386,7 +386,7 @@ function field_test_field_storage_create_field($field) { function field_test_field_storage_delete_field($field) { $data = _field_test_storage_data(); - $field_data = &$data[$field['id']]; + $field_data = &$data[$field['uuid']]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as &$row) { $row->deleted = TRUE; @@ -403,7 +403,7 @@ function field_test_field_storage_delete_instance($instance) { $data = _field_test_storage_data(); $field = field_info_field($instance['field_name']); - $field_data = &$data[$field['id']]; + $field_data = &$data[$field['uuid']]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as &$row) { if ($row->bundle == $instance['bundle']) { @@ -432,8 +432,8 @@ function field_test_field_attach_rename_bundle($bundle_old, $bundle_new) { $instances = field_read_instances(array('bundle' => $bundle_new), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); foreach ($instances as $field_name => $instance) { $field = field_info_field_by_id($instance['field_id']); - if ($field['storage']['type'] == 'field_test_storage') { - $field_data = &$data[$field['id']]; + if ($field && $field['storage']['type'] == 'field_test_storage') { + $field_data = &$data[$field['uuid']]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as &$row) { if ($row->bundle == $bundle_old) { @@ -454,9 +454,9 @@ function field_test_field_attach_delete_bundle($entity_type, $bundle, $instances $data = _field_test_storage_data(); foreach ($instances as $field_name => $instance) { - $field = field_info_field($field_name); - if ($field['storage']['type'] == 'field_test_storage') { - $field_data = &$data[$field['id']]; + $field = field_info_field($instance['field_name']); + if ($field && $field['storage']['type'] == 'field_test_storage') { + $field_data = &$data[$field['uuid']]; foreach (array('current', 'revisions') as $sub_table) { foreach ($field_data[$sub_table] as &$row) { if ($row->bundle == $bundle_old) { diff --git a/core/modules/field/tests/modules/field_test_config/config/field.field.field_test_import.yml b/core/modules/field/tests/modules/field_test_config/config/field.field.field_test_import.yml new file mode 100644 index 0000000..afb69fa --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/config/field.field.field_test_import.yml @@ -0,0 +1,21 @@ +id: field_test_import +uuid: fb38277f-1fd4-49d5-8d09-9d7037fdcce9 +langcode: und +field_name: field_test_import +type: text +settings: + max_length: '255' +module: text +active: 1 +entity_types: { } +storage: + type: field_sql_storage + settings: { } + module: field_sql_storage + active: 1 +locked: false +cardinality: '1' +translatable: false +indexes: + format: + - format diff --git a/core/modules/field/tests/modules/field_test_config/config/field.instance.test_entity.test_bundle.field_test_import.yml b/core/modules/field/tests/modules/field_test_config/config/field.instance.test_entity.test_bundle.field_test_import.yml new file mode 100644 index 0000000..d584998 --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/config/field.instance.test_entity.test_bundle.field_test_import.yml @@ -0,0 +1,21 @@ +id: test_entity.test_bundle.field_test_import +uuid: 392b4e9d-6157-412e-9603-3d622512f498 +langcode: und +field_name: field_test_import +field_id: fb38277f-1fd4-49d5-8d09-9d7037fdcce9 +entity_type: test_entity +bundle: test_bundle +label: 'Test import field' +description: '' +required: 0 +default_value: '' +default_value_function: '' +settings: + text_processing: '0' + user_register_form: false +widget: + weight: '-2' + type: text_textfield + module: text + settings: + size: '60' diff --git a/core/modules/field/tests/modules/field_test_config/field_test_config.info.yml b/core/modules/field/tests/modules/field_test_config/field_test_config.info.yml new file mode 100644 index 0000000..57e0849 --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/field_test_config.info.yml @@ -0,0 +1,6 @@ +name: 'Field API configuration tests' +description: 'Support module for the Field API configuration tests.' +core: 8.x +package: Testing +version: VERSION +hidden: TRUE diff --git a/core/modules/field/tests/modules/field_test_config/field_test_config.module b/core/modules/field/tests/modules/field_test_config/field_test_config.module new file mode 100644 index 0000000..3d7730d --- /dev/null +++ b/core/modules/field/tests/modules/field_test_config/field_test_config.module @@ -0,0 +1,6 @@ + TRUE, 'include_inactive' => TRUE)); - drupal_load('module', 'field_sql_storage'); - foreach ($fields as $field) { + // It's not safe to call field_read_fields() during maintenance mode. + // It's also not safe to call entity_load_multiple() during + // hook_schema() invocations. + if (!defined('MAINTENANCE_MODE')) { + // @todo we also need deleted fields, they have a schema. + $field_names = config_get_storage_names_with_prefix('field.field'); + foreach ($field_names as $name) { + $field = config($name)->get(); if ($field['storage']['type'] == 'field_sql_storage') { + $field = new Field($field, 'field_entity'); $schema += _field_sql_storage_schema($field); } } } + return $schema; } @@ -104,7 +111,7 @@ function field_sql_storage_update_8000(&$sandbox) { $field_langcode = array( 'type' => 'varchar', 'length' => 32, - 'not null' => true, + 'not null' => TRUE, 'default' => '', ); diff --git a/core/modules/field_sql_storage/field_sql_storage.module b/core/modules/field_sql_storage/field_sql_storage.module index e604ac6..9a0c09a 100644 --- a/core/modules/field_sql_storage/field_sql_storage.module +++ b/core/modules/field_sql_storage/field_sql_storage.module @@ -45,7 +45,7 @@ function field_sql_storage_field_storage_info() { */ function _field_sql_storage_tablename($field) { if ($field['deleted']) { - return "field_deleted_data_{$field['id']}"; + return "field_deleted_data_" . field_generate_table_id($field); } else { return "field_data_{$field['field_name']}"; @@ -63,7 +63,7 @@ function _field_sql_storage_tablename($field) { */ function _field_sql_storage_revision_tablename($field) { if ($field['deleted']) { - return "field_deleted_revision_{$field['id']}"; + return "field_deleted_revision_" . field_generate_table_id($field); } else { return "field_revision_{$field['field_name']}"; @@ -178,15 +178,16 @@ function _field_sql_storage_schema($field) { ), ); - $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array()); + $schema = $field->getSchema(); + // Add field columns. - foreach ($field['columns'] as $column_name => $attributes) { + foreach ($schema['columns'] as $column_name => $attributes) { $real_name = _field_sql_storage_columnname($field['field_name'], $column_name); $current['fields'][$real_name] = $attributes; } // Add indexes. - foreach ($field['indexes'] as $index_name => $columns) { + foreach ($schema['indexes'] as $index_name => $columns) { $real_name = _field_sql_storage_indexname($field['field_name'], $index_name); foreach ($columns as $column_name) { // Indexes can be specified as either a column name or an array with @@ -204,7 +205,7 @@ function _field_sql_storage_schema($field) { } // Add foreign keys. - foreach ($field['foreign keys'] as $specifier => $specification) { + foreach ($schema['foreign keys'] as $specifier => $specification) { $real_name = _field_sql_storage_indexname($field['field_name'], $specifier); $current['foreign keys'][$real_name]['table'] = $specification['table']; foreach ($specification['columns'] as $column => $referenced) { @@ -234,7 +235,18 @@ function field_sql_storage_field_storage_create_field($field) { foreach ($schema as $name => $table) { db_create_table($name, $table); } - drupal_get_schema(NULL, TRUE); + // Do not rebuild the schema right now, since the field definition has not + // been saved yet. This will be done in hook_field_create_field(). +} + +/** + * Implements hook_field_create_field(). + */ +function field_sql_storage_field_create_field($field) { + // Rebuild the schema now that the field has been saved. + if ($field['storage']['type'] == 'field_sql_storage') { + drupal_get_schema(NULL, TRUE); + } } /** @@ -294,8 +306,12 @@ function field_sql_storage_field_storage_update_field($field, $prior_field, $has // priors that exist unchanged. $table = _field_sql_storage_tablename($prior_field); $revision_table = _field_sql_storage_revision_tablename($prior_field); - foreach ($prior_field['indexes'] as $name => $columns) { - if (!isset($field['indexes'][$name]) || $columns != $field['indexes'][$name]) { + + $schema = $field->getSchema(); + $prior_schema = $prior_field->getSchema(); + + foreach ($prior_schema['indexes'] as $name => $columns) { + if (!isset($schema['indexes'][$name]) || $columns != $schema['indexes'][$name]) { $real_name = _field_sql_storage_indexname($field['field_name'], $name); db_drop_index($table, $real_name); db_drop_index($revision_table, $real_name); @@ -303,8 +319,8 @@ function field_sql_storage_field_storage_update_field($field, $prior_field, $has } $table = _field_sql_storage_tablename($field); $revision_table = _field_sql_storage_revision_tablename($field); - foreach ($field['indexes'] as $name => $columns) { - if (!isset($prior_field['indexes'][$name]) || $columns != $prior_field['indexes'][$name]) { + foreach ($schema['indexes'] as $name => $columns) { + if (!isset($prior_schema['indexes'][$name]) || $columns != $prior_schema['indexes'][$name]) { $real_name = _field_sql_storage_indexname($field['field_name'], $name); $real_columns = array(); foreach ($columns as $column_name) { diff --git a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php index 5ea9c88..9d6177f 100644 --- a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php +++ b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Tests/FieldSqlStorageTest.php @@ -406,10 +406,10 @@ function testFieldStorageDetails() { $instance = field_info_instance($this->instance['entity_type'], $this->instance['field_name'], $this->instance['bundle']); // The storage details are indexed by a storage engine type. - $this->assertTrue(array_key_exists('sql', $field['storage']['details']), 'The storage type is SQL.'); + $this->assertTrue(array_key_exists('sql', $field['storage details']), 'The storage type is SQL.'); // The SQL details are indexed by table name. - $details = $field['storage']['details']['sql']; + $details = $field['storage details']['sql']; $this->assertTrue(array_key_exists($current, $details[FIELD_LOAD_CURRENT]), 'Table name is available in the instance array.'); $this->assertTrue(array_key_exists($revision, $details[FIELD_LOAD_REVISION]), 'Revision table name is available in the instance array.'); diff --git a/core/modules/field_ui/field_ui.admin.inc b/core/modules/field_ui/field_ui.admin.inc index 566959b..35fc624 100644 --- a/core/modules/field_ui/field_ui.admin.inc +++ b/core/modules/field_ui/field_ui.admin.inc @@ -5,7 +5,7 @@ * Administrative interface for custom field type creation. */ -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\FieldInstance; use Drupal\field_ui\FieldOverview; use Drupal\field_ui\DisplayOverview; @@ -609,14 +609,15 @@ function field_ui_field_settings_form_submit($form, &$form_state) { // Merge incoming form values into the existing field. $field = field_info_field($field_values['field_name']); + foreach ($field_values as $key => $value) { + $field[$key] = $value; + } $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; $instance = field_info_instance($entity_type, $field['field_name'], $bundle); // Update the field. - $field = array_merge($field, $field_values); - try { field_update_field($field); drupal_set_message(t('Updated field %label field settings.', array('%label' => $instance['label']))); @@ -859,14 +860,6 @@ function field_ui_field_edit_form($form, &$form_state, $instance) { '#type' => 'value', '#value' => $instance['widget']['type'], ); - $form['instance']['widget']['module'] = array( - '#type' => 'value', - '#value' => $widget_type['module'], - ); - $form['instance']['widget']['active'] = array( - '#type' => 'value', - '#value' => !empty($field['instance']['widget']['active']) ? 1 : 0, - ); // Add additional field instance settings from the field module. $additions = module_invoke($field['module'], 'field_instance_settings_form', $field, $instance, $form_state); @@ -936,7 +929,10 @@ function field_ui_default_value_widget($field, $instance, &$form, &$form_state) // Insert the widget. Since we do not use the "official" instance definition, // the whole flow cannot use field_invoke_method(). - $items = (array) $instance['default_value']; + $items = array(); + if (!empty($instance['default_value'])) { + $items = (array) $instance['default_value']; + } $element += $instance->getWidget()->form($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state); return $element; diff --git a/core/modules/file/file.install b/core/modules/file/file.install index 27a2438..472f7e5 100644 --- a/core/modules/file/file.install +++ b/core/modules/file/file.install @@ -125,8 +125,8 @@ function file_schema() { ), 'id' => array( 'description' => 'The primary key of the object using the file.', - 'type' => 'int', - 'unsigned' => TRUE, + 'type' => 'varchar', + 'length' => 64, 'not null' => TRUE, 'default' => 0, ), @@ -234,6 +234,21 @@ function file_requirements($phase) { } /** + * Implements hook_update_dependencies(). + */ +function file_update_dependencies() { + // The update hook to convert the id column in the column table needs to be + // the first one because user update hooks will be called during system + // update hooks due to other dependencies and the user picture conversion + // might need to save a default image and the field it's stored in, does + // not have an integer id anymore. + $dependencies['system'][8001] = array( + 'file' => 8001, + ); + return $dependencies; +} + +/** * Converts default_file_main variable to config. * * @ingroup config_upgrade @@ -245,3 +260,17 @@ function file_update_8000() { 'file_icon_directory'=>'icon.directory', )); } + +/** + * Convert the id column in file_usage to store UUID's. + */ +function file_update_8001() { + $spec = array( + 'description' => 'The primary key of the object using the file.', + 'type' => 'varchar', + 'length' => 64, + 'not null' => TRUE, + 'default' => '', + ); + db_change_field('file_usage', 'id', 'id', $spec); +} diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldDisplayTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldDisplayTest.php index 81e87d0..dfb661c 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldDisplayTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldDisplayTest.php @@ -24,7 +24,7 @@ public static function getInfo() { * Tests normal formatter display on node display. */ function testNodeDisplay() { - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $type_name = 'article'; $field_settings = array( 'display_field' => '1', diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldPathTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldPathTest.php index d963b89..0ae7c26 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldPathTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldPathTest.php @@ -23,7 +23,7 @@ public static function getInfo() { * Tests the normal formatter display on node display. */ function testUploadPath() { - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $type_name = 'article'; $field = $this->createFileField($field_name, $type_name); $test_file = $this->getTestFile('text'); diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldRSSContentTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldRSSContentTest.php index 12f4f08..1d8dc37 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldRSSContentTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldRSSContentTest.php @@ -24,7 +24,7 @@ public static function getInfo() { * Tests RSS enclosure formatter display for RSS feeds. */ function testFileFieldRSSContent() { - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $type_name = 'article'; $field_settings = array( 'display_field' => '1', diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldRevisionTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldRevisionTest.php index 47d19d1..1b5145f 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldRevisionTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldRevisionTest.php @@ -32,7 +32,7 @@ public static function getInfo() { */ function testRevisions() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name); $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, $type_name); diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php index 7a40a1c..b63d5cc 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php @@ -27,7 +27,7 @@ public static function getInfo() { */ function testRequired() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name, array(), array('required' => '1')); $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, $type_name); @@ -75,7 +75,7 @@ function testRequired() { */ function testFileMaxSize() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name, array(), array('required' => '1')); $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, $type_name); @@ -127,7 +127,7 @@ function testFileMaxSize() { */ function testFileExtension() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name); $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, $type_name); diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php index 05b0cad..a141904 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php @@ -32,7 +32,7 @@ public static function getInfo() { */ function testSingleValuedWidget() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name); $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, $type_name); @@ -88,8 +88,8 @@ function testSingleValuedWidget() { */ function testMultiValuedWidget() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); - $field_name2 = strtolower($this->randomName()); + $field_name = 'test_file_field'; + $field_name2 = 'test_file_field_2'; $this->createFileField($field_name, $type_name, array('cardinality' => 3)); $this->createFileField($field_name2, $type_name, array('cardinality' => 3)); @@ -205,7 +205,7 @@ function testMultiValuedWidget() { */ function testPrivateFileSetting() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name); $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, $type_name); @@ -261,6 +261,9 @@ function testPrivateFileComment() { $this->drupalPost(NULL, $edit, t('Save field settings')); $this->drupalPost(NULL, array(), t('Save settings')); + // Manually clear cache on the tester side. + field_info_cache_clear(); + // Create node. $text_file = $this->getTestFile('text'); $edit = array( diff --git a/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php b/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php index 1e6eef5..2ec9dfa 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileItemTest.php @@ -54,9 +54,6 @@ public function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'file_test', 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'options_select', - ), ); field_create_instance($instance); file_put_contents('public://example.txt', $this->randomName()); diff --git a/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php b/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php index 42c0b09..5355ef0 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FilePrivateTest.php @@ -38,7 +38,7 @@ public function setUp() { */ function testPrivateFile() { $type_name = 'article'; - $field_name = strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name, array('uri_scheme' => 'private')); // Create a field with no view access - see field_test_field_access(). diff --git a/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php b/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php index 4d56b7b..bfeaead 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileTokenReplaceTest.php @@ -31,7 +31,7 @@ function testFileTokenReplacement() { // Create file field. $type_name = 'article'; - $field_name = 'field_' . strtolower($this->randomName()); + $field_name = 'test_file_field'; $this->createFileField($field_name, $type_name); $field = field_info_field($field_name); $instance = field_info_instance('node', $field_name, $type_name); diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index b1e63de..3173cfb 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -22,10 +22,6 @@ function forum_install() { * Implements hook_enable(). */ function forum_enable() { - // If we enable forum at the same time as taxonomy we need to call - // field_associate_fields() as otherwise the field won't be enabled until - // hook modules_enabled is called which takes place after hook_enable events. - field_associate_fields('taxonomy'); // Create the forum vocabulary if it does not exist. // @todo Change Forum module so forum.settings can contain the vocabulary's @@ -55,7 +51,7 @@ function forum_enable() { } // Create the 'taxonomy_forums' field if it doesn't already exist. - if (!field_info_field('taxonomy_forums')) { + if (!field_read_field('taxonomy_forums', array('include_inactive' => TRUE))) { $field = array( 'field_name' => 'taxonomy_forums', 'type' => 'taxonomy_term_reference', diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php index 224586d..0acbcab 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php @@ -35,7 +35,6 @@ function setUp() { parent::setUp(); $this->installSchema('system', array('variable', 'url_alias')); - $this->installSchema('field', array('field_config', 'field_config_instance')); $this->installSchema('user', array('users')); $this->installSchema('language', array('language')); $this->installSchema('entity_test', array('entity_test')); diff --git a/core/modules/image/image.module b/core/modules/image/image.module index b2816de..0496c6b 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -432,12 +432,12 @@ function image_field_update_field($field, $prior_field, $has_data) { if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); - file_usage()->add($file_new, 'image', 'default_image', $field['id']); + file_usage()->add($file_new, 'image', 'default_image', $field['uuid']); } // Is there an old file? if ($fid_old && ($file_old = file_load($fid_old))) { - file_usage()->delete($file_old, 'image', 'default_image', $field['id']); + file_usage()->delete($file_old, 'image', 'default_image', $field['uuid']); } } @@ -500,11 +500,11 @@ function image_field_update_instance($instance, $prior_instance) { if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); - file_usage()->add($file_new, 'image', 'default_image', $instance['id']); + file_usage()->add($file_new, 'image', 'default_image', $instance['uuid']); } // Delete the old file, if present. if ($fid_old && ($file_old = file_load($fid_old))) { - file_usage()->delete($file_old, 'image', 'default_image', $instance['id']); + file_usage()->delete($file_old, 'image', 'default_image', $instance['uuid']); } } diff --git a/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php b/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php index a8058f7..674ec0c 100644 --- a/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/Condition/NodeConditionTest.php @@ -30,8 +30,6 @@ protected function setUp() { $this->installSchema('node', 'node_type'); $this->installSchema('node', 'node'); $this->installSchema('node', 'node_revision'); - $this->installSchema('field', 'field_config'); - $this->installSchema('field', 'field_config_instance'); } /** diff --git a/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php b/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php index 925828c..aa6c06e 100644 --- a/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php +++ b/core/modules/number/lib/Drupal/number/Tests/NumberFieldTest.php @@ -63,11 +63,6 @@ function testNumberDecimalField() { 'placeholder' => '0.00' ), ), - 'display' => array( - 'default' => array( - 'type' => 'number_decimal', - ), - ), ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'default') diff --git a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php index ca9d605..11ab663 100644 --- a/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php +++ b/core/modules/options/lib/Drupal/options/Tests/OptionsFieldTest.php @@ -36,7 +36,7 @@ function setUp() { $this->field_name = 'test_options'; - $this->field = array( + $this->field_definition = array( 'field_name' => $this->field_name, 'type' => 'list_integer', 'cardinality' => 1, @@ -44,7 +44,7 @@ function setUp() { 'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'), ), ); - $this->field = field_create_field($this->field); + $this->field = field_create_field($this->field_definition); $this->instance = array( 'field_name' => $this->field_name, @@ -109,8 +109,7 @@ function testUpdateAllowedValues() { // Options are reset when a new field with the same name is created. field_delete_field($this->field_name); unset($this->field['id']); - $this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three'); - $this->field = field_create_field($this->field); + field_create_field($this->field_definition); $this->instance = array( 'field_name' => $this->field_name, 'entity_type' => 'entity_test', @@ -119,7 +118,7 @@ function testUpdateAllowedValues() { 'type' => 'options_buttons', ), ); - $this->instance = field_create_instance($this->instance); + field_create_instance($this->instance); $entity = entity_create('entity_test', array()); $form = entity_get_form($entity); $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists'); diff --git a/core/modules/options/options.module b/core/modules/options/options.module index 793b07e..77d75a4 100644 --- a/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -252,7 +252,7 @@ function options_field_update_field($field, $prior_field, $has_data) { function options_allowed_values($field, $instance = NULL, EntityInterface $entity = NULL) { $allowed_values = &drupal_static(__FUNCTION__, array()); - if (!isset($allowed_values[$field['id']])) { + if (!isset($allowed_values[$field['uuid']])) { $function = $field['settings']['allowed_values_function']; // If $cacheable is FALSE, then the allowed values are not statically // cached. See options_test_dynamic_values_callback() for an example of @@ -266,14 +266,14 @@ function options_allowed_values($field, $instance = NULL, EntityInterface $entit } if ($cacheable) { - $allowed_values[$field['id']] = $values; + $allowed_values[$field['uuid']] = $values; } else { return $values; } } - return $allowed_values[$field['id']]; + return $allowed_values[$field['uuid']]; } /** @@ -388,7 +388,7 @@ function options_field_update_forbid($field, $prior_field, $has_data) { */ function _options_values_in_use($field, $values) { if ($values) { - $field = field_info_field_by_id($field['id']); + $field = field_info_field_by_id($field['uuid']); $factory = drupal_container()->get('entity.query'); foreach ($field['bundles'] as $entity_type => $bundle) { $result = $factory->get($entity_type) @@ -477,6 +477,9 @@ function options_field_widget_info() { function options_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { // Abstract over the actual field columns, to allow different field types to // reuse those widgets. + + // Reset internal pointer since we're dealing with objects now. + reset($field['columns']); $value_key = key($field['columns']); $type = str_replace('options_', '', $instance['widget']['type']); diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php index fb7019e..33802db 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/lib/Drupal/serialization/Tests/EntitySerializationTest.php @@ -57,7 +57,6 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - $this->installSchema('field', array('field_config', 'field_config_instance')); $this->installSchema('entity_test', array('entity_test_mulrev', 'entity_test_mulrev_property_revision', 'entity_test_mulrev_property_data')); // Auto-create a field for testing. diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php b/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php index 3aeab60..c4efde8 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php @@ -108,10 +108,7 @@ function testEnableModulesInstall() { */ function testEnableModulesInstallContainer() { // Install Node module. - // @todo field_sql_storage and field should technically not be necessary - // for an entity query. $this->enableModules(array('field_sql_storage', 'field', 'node')); - $this->installSchema('field', array('field_config', 'field_config_instance')); $this->installSchema('node', array('node_type', 'node')); // Perform an entity query against node. diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php index c0f181a..a997a34 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php @@ -27,11 +27,6 @@ public static function getInfo() { ); } - function setUp() { - parent::setUp(); - $this->installSchema('field', array('field_config', 'field_config_instance')); - } - /** * Tests simple JOIN statements. */ diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php index b747e79..c3f236b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php @@ -25,7 +25,6 @@ public function setUp() { parent::setUp(); $this->installSchema('user', 'users'); $this->installSchema('system', 'sequences'); - $this->installSchema('field', array('field_config', 'field_config_instance')); $this->installSchema('entity_test', 'entity_test'); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php index 09e7a7a..5e08eed 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php @@ -41,8 +41,6 @@ public static function getInfo() { protected function setUp() { parent::setUp(); - // Install field module schema. - $this->installSchema('field', array('field_config', 'field_config_instance')); // The users table is needed for creating dummy user accounts. $this->installSchema('user', array('users')); // Register entity_test text field. diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php index c0de21b..7974ef2 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/FieldUpgradePathTest.php @@ -87,4 +87,16 @@ public function testEntityDisplayUpgrade() { $this->assertEqual($displays['teaser']['content']['language'], $expected['teaser']); } + /** + * Tests upgrade of fields and instances to config. + */ + function testFieldUpgradeToConfig() { + $this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.')); + + // Assert the body field and instance on the article are converted. + $body_field = field_info_field('body'); + $this->assertNotNull($body_field, 'The body field has been found.'); + $body_field_instance = field_info_instance('node', 'body', 'article'); + $this->assertNotNull($body_field_instance, 'The body field instance on the article content type has been found.'); + } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php index 04070c9..23eb3da 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php @@ -51,7 +51,7 @@ public function testUserPictureUpgrade() { // Check file usage for the default image. $usage = file_usage()->listUsage($file); $field = field_info_field('user_picture'); - $this->assertEqual(1, $usage['image']['default_image'][$field['id']]); + $this->assertTrue(isset($usage['image']['default_image'][$field['uuid']])); $this->assertEqual($instance['settings']['max_resolution'], '800x800', 'User picture maximum resolution has been migrated.'); $this->assertEqual($instance['settings']['max_filesize'], '700 KB', 'User picture maximum filesize has been migrated.'); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php index d5de5fa..a81dffd 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php @@ -53,7 +53,7 @@ function setUp() { ), ) ); - field_create_field($this->field); + $this->field = field_create_field($this->field); $this->instance = array( 'field_name' => $this->field_name, 'entity_type' => 'test_entity', diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php index d73302b..b25ba51 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php @@ -175,15 +175,15 @@ function testUninstallReinstall() { // Fields and field instances attached to taxonomy term bundles should be // removed when the module is uninstalled. $this->field_name = drupal_strtolower($this->randomName() . '_field_name'); - $this->field = array('field_name' => $this->field_name, 'type' => 'text', 'cardinality' => 4); - $this->field = field_create_field($this->field); - $this->instance = array( + $this->field_definition = array('field_name' => $this->field_name, 'type' => 'text', 'cardinality' => 4); + field_create_field($this->field_definition); + $this->instance_definition = array( 'field_name' => $this->field_name, 'entity_type' => 'taxonomy_term', 'bundle' => $this->vocabulary->id(), 'label' => $this->randomName() . '_label', ); - field_create_instance($this->instance); + field_create_instance($this->instance_definition); module_disable(array('taxonomy')); require_once DRUPAL_ROOT . '/core/includes/install.inc'; @@ -196,8 +196,7 @@ function testUninstallReinstall() { // an instance of this field on the same bundle name should be successful. $this->vocabulary->enforceIsNew(); taxonomy_vocabulary_save($this->vocabulary); - unset($this->field['id']); - field_create_field($this->field); - field_create_instance($this->instance); + field_create_field($this->field_definition); + field_create_instance($this->instance_definition); } } diff --git a/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php b/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php index 716ead7..2000ffd 100644 --- a/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php +++ b/core/modules/text/lib/Drupal/text/Tests/Formatter/TextPlainUnitTest.php @@ -45,9 +45,6 @@ public static function getInfo() { function setUp() { parent::setUp(); - $this->installSchema('field', 'field_config'); - $this->installSchema('field', 'field_config_instance'); - // @todo Add helper methods for all of the following. $this->entity_type = 'test_entity'; diff --git a/core/modules/translation_entity/translation_entity.admin.inc b/core/modules/translation_entity/translation_entity.admin.inc index 2537c84..6478205 100644 --- a/core/modules/translation_entity/translation_entity.admin.inc +++ b/core/modules/translation_entity/translation_entity.admin.inc @@ -6,20 +6,21 @@ */ use Drupal\Core\Entity\EntityInterface; -use Drupal\field\FieldInstance; +use Drupal\field\Plugin\Core\Entity\Field; +use Drupal\field\Plugin\Core\Entity\FieldInstance; /** * Returns a form element to configure field synchronization. * - * @param array $field + * @param \Drupal\field\Plugin\Core\Entity\Field $field * A field definition array. - * @param \Drupal\Field\FieldInstance $instance + * @param Drupal\field\Plugin\Core\Entity\FieldInstance $instance * A field instance definition object. * * @return array * A form element to configure field synchronization. */ -function translation_entity_field_sync_widget(array $field, FieldInstance $instance) { +function translation_entity_field_sync_widget(Field $field, FieldInstance $instance) { $element = array(); if (!empty($field['settings']['column_groups']) && count($field['settings']['column_groups']) > 1) { diff --git a/core/modules/user/user.install b/core/modules/user/user.install index c7fd16b..c292d74 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -315,10 +315,6 @@ function user_install_picture_field() { 'uri_scheme' => 'public', 'default_image' => FALSE, ), - 'storage' => array( - 'type' => 'field_sql_storage', - 'settings' => array(), - ), ); $field = field_create_field($field); @@ -716,7 +712,7 @@ function user_update_8011() { 'settings' => array(), ), ); - _update_7000_field_create_field($field); + $field = _update_7000_field_create_field($field); $instance = array( 'field_name' => 'user_picture', diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php index 5d2b524..172678d 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/RelationshipJoinTestBase.php @@ -27,7 +27,6 @@ */ protected function setUpFixtures() { $this->installSchema('user', array('users', 'users_roles', 'role_permission')); - $this->installSchema('field', array('field_config', 'field_config_instance')); $this->installConfig(array('user')); parent::setUpFixtures();