Commit a820153f authored by alexpott's avatar alexpott

Issue #1735118 by swentel, yched, xjm, larowlan, alexpott, tim.plunkett: Convert Field API to CMI.

parent 5a6647be
......@@ -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;
......
......@@ -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;
/**
......
......@@ -48,13 +48,12 @@ function setUp() {
$this->drupalLogin($web_user);
// Create a field with settings to validate.
$this->field = array(
$this->field = field_create_field(array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'datetime',
'settings' => array('datetime_type' => 'date'),
);
field_create_field($this->field);
$this->instance = array(
));
$this->instance = field_create_instance(array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
......@@ -64,8 +63,7 @@ function setUp() {
'settings' => array(
'default_value' => 'blank',
),
);
field_create_instance($this->instance);
));
$this->display_options = array(
'type' => 'datetime_default',
......@@ -304,6 +302,7 @@ function testDefaultValue() {
// Set the default value to 'blank'.
$this->instance['settings']['default_value'] = 'blank';
$this->instance['default_value_function'] = 'datetime_default_value';
field_update_instance($this->instance);
// Display creation form.
......
......@@ -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.
......
......@@ -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).
......
......@@ -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.
......
......@@ -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.
......
......@@ -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;
......
......@@ -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.
......
......@@ -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.
......
......@@ -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.
......
......@@ -28,7 +28,6 @@ function setUp() {
parent::setUp();
$this->installSchema('system', 'variable');
$this->installSchema('field', array('field_config', 'field_config_instance'));
$this->installSchema('entity_test', array('entity_test', 'entity_test_rev'));
// Set default storage backend.
......@@ -58,12 +57,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(
......
......@@ -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.
......
......@@ -11,7 +11,7 @@
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\edit\EditorInterface;
use Drupal\field\FieldInstance;
use Drupal\field\Plugin\Core\Entity\FieldInstance;
/**
......
......@@ -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.
*/
......
......@@ -234,6 +234,9 @@ function hook_field_info_alter(&$info) {
* indexes specified by the field-type module. Some storage engines might
* not support indexes.
* - foreign keys: (optional) An array of Schema API foreign key definitions.
* Note, however, that the field data is not necessarily stored in SQL.
* Also, the possible usage is limited, as you cannot specify another field
* as related, only existing SQL tables, such as {taxonomy_term_data}.
*/
function hook_field_schema($field) {
if ($field['type'] == 'text_long') {
......@@ -1302,7 +1305,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:
......@@ -1370,7 +1373,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();
......@@ -1464,7 +1467,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) {
......@@ -1488,7 +1491,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();
......@@ -1712,13 +1715,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.
*/
......@@ -1735,11 +1738,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)) {
......@@ -1770,11 +1773,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());
......
......@@ -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;
}
......@@ -1531,11 +1531,16 @@ function field_entity_bundle_create($entity_type, $bundle) {
* Implements hook_entity_bundle_rename().
*/
function field_entity_bundle_rename($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 $instance) {
if ($instance->entity_type == $entity_type && $instance->bundle == $bundle_old) {
$id_new = $instance['entity_type'] . '.' . $bundle_new . '.' . $instance['field_name'];
$instance->id = $id_new;
$instance->bundle = $bundle_new;
$instance->allowBundleRename();
$instance->save();
}
}
// Clear the cache.
field_cache_clear();
......
This diff is collapsed.
......@@ -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;
/**
......
......@@ -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
This diff is collapsed.
This diff is collapsed.
......@@ -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,
......
......@@ -142,17 +142,24 @@ 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 = \Drupal::config($config_id)->get();
if ($field_config['active'] && $field_config['storage']['active']) {
$fields[$field_config['uuid']] = $field_config;
}
}
// Get field instances.
foreach (config_get_storage_names_with_prefix('field.instance') as $config_id) {
$instance_config = \Drupal::config($config_id)->get();
$field_uuid = $instance_config['field_uuid'];
// Filter out instances of inactive fields, and instances on unknown
// entity types.
if (isset($fields[$field_uuid])) {
$field = $fields[$field_uuid];
$map[$field['id']]['bundles'][$instance_config['entity_type']][] = $instance_config['bundle'];
$map[$field['id']]['type'] = $field['type'];
}
}
// Save in "static" and persistent caches.
......@@ -181,7 +188,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 +198,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['id']] = $field['uuid'];
}
}
......@@ -229,7 +236,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 +282,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 +316,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 +362,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 +384,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 +500,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 +559,5 @@ public function prepareExtraFields($extra_fields, $entity_type, $bundle) {
return $result;
}
}
<?php
/**
* @file
* Definition of Drupal\field\FieldInstance.
*/
namespace Drupal\field;
/**
* Class for field instance objects.
*/
class FieldInstance implements \ArrayAccess {
/**
* The instance definition, as read from configuration storage.
*
* @var array
*/
public $definition;
/**
* The widget object used for this instance.
*
* @var Drupal\field\Plugin\Type\Widget\WidgetInterface
*/
protected $widget;
/**
* Constructs a FieldInstance object.
*
* @param array $definition
* The instance definition array, as read from configuration storage.
*/
public function __construct(array $definition) {
$this->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);
}
}
/**