478 lines
16 KiB
Plaintext
478 lines
16 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Install, update, and uninstall functions for the Field module.
|
|
*/
|
|
|
|
use Drupal\Component\Uuid\Uuid;
|
|
use Drupal\field\Plugin\Core\Entity\Field;
|
|
|
|
/**
|
|
* Creates a field by writing directly to the database.
|
|
*
|
|
* @ingroup update_api
|
|
*/
|
|
function _update_7000_field_create_field(&$field) {
|
|
// Merge in default values.`
|
|
$field += array(
|
|
'entity_types' => array(),
|
|
'cardinality' => 1,
|
|
'translatable' => FALSE,
|
|
'locked' => FALSE,
|
|
'settings' => array(),
|
|
'indexes' => array(),
|
|
'deleted' => FALSE,
|
|
'active' => TRUE,
|
|
);
|
|
|
|
// Set storage.
|
|
$field['storage'] = array(
|
|
'type' => 'field_sql_storage',
|
|
'settings' => array(),
|
|
'module' => 'field_sql_storage',
|
|
'active' => TRUE,
|
|
);
|
|
|
|
// Fetch the field schema to initialize columns and indexes. The field module
|
|
// is not guaranteed to be loaded at this point.
|
|
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['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']);
|
|
|
|
// Write the field to the database.
|
|
$record = array(
|
|
'field_name' => $field['field_name'],
|
|
'type' => $field['type'],
|
|
'module' => $field['module'],
|
|
'active' => (int) $field['active'],
|
|
'storage_type' => $field['storage']['type'],
|
|
'storage_module' => $field['storage']['module'],
|
|
'storage_active' => (int) $field['storage']['active'],
|
|
'locked' => (int) $field['locked'],
|
|
'data' => serialize($data),
|
|
'cardinality' => $field['cardinality'],
|
|
'translatable' => (int) $field['translatable'],
|
|
'deleted' => (int) $field['deleted'],
|
|
);
|
|
// We don't use drupal_write_record() here because it depends on the schema.
|
|
$field_id = db_insert('field_config')
|
|
->fields($record)
|
|
->execute();
|
|
|
|
// 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_sql_storage_field_storage_create_field($field_entity);
|
|
|
|
$field['id'] = $field_id;
|
|
}
|
|
|
|
/**
|
|
* Deletes a field stored in SQL storage directly from the database.
|
|
*
|
|
* To protect user data, this function can only be used to delete fields once
|
|
* all information it stored is gone. Delete all data from the
|
|
* field_data_$field_name table before calling by either manually issuing
|
|
* delete queries against it or using _update_7000_field_delete_instance().
|
|
*
|
|
* @param $field_name
|
|
* The field name to delete.
|
|
*
|
|
* @ingroup update_api
|
|
*/
|
|
function _update_7000_field_delete_field($field_name) {
|
|
$table_name = 'field_data_' . $field_name;
|
|
if (db_select($table_name)->range(0, 1)->countQuery()->execute()->fetchField()) {
|
|
$t = get_t();
|
|
throw new Exception($t('This function can only be used to delete fields without data'));
|
|
}
|
|
// Delete all instances.
|
|
db_delete('field_config_instance')
|
|
->condition('field_name', $field_name)
|
|
->execute();
|
|
|
|
// Nuke field data and revision tables.
|
|
db_drop_table($table_name);
|
|
db_drop_table('field_revision_' . $field_name);
|
|
|
|
// Delete the field.
|
|
db_delete('field_config')
|
|
->condition('field_name', $field_name)
|
|
->execute();
|
|
}
|
|
|
|
/**
|
|
* Deletes an instance and all its data of a field stored in SQL Storage.
|
|
*
|
|
* BEWARE: This function deletes user data from the field storage tables.
|
|
*
|
|
* @ingroup update_api
|
|
*/
|
|
function _update_7000_field_delete_instance($field_name, $entity_type, $bundle) {
|
|
// Delete field instance configuration data.
|
|
db_delete('field_config_instance')
|
|
->condition('field_name', $field_name)
|
|
->condition('entity_type', $entity_type)
|
|
->condition('bundle', $bundle)
|
|
->execute();
|
|
|
|
// Nuke data.
|
|
db_delete('field_data_' . $field_name)
|
|
->condition('entity_type', $entity_type)
|
|
->condition('bundle', $bundle)
|
|
->execute();
|
|
db_delete('field_revision_' . $field_name)
|
|
->condition('entity_type', $entity_type)
|
|
->condition('bundle', $bundle)
|
|
->execute();
|
|
}
|
|
|
|
/**
|
|
* Fetches all of the field definitions from the database.
|
|
*
|
|
* Warning: Unlike the field_read_fields() API function, this function returns
|
|
* all fields by default, including deleted and inactive fields, unless
|
|
* specified otherwise in the $conditions parameter.
|
|
*
|
|
* @param $conditions
|
|
* An array of conditions to limit the select query to.
|
|
* @param $key
|
|
* The name of the field property the return array is indexed by. Using
|
|
* anything else than 'id' might cause incomplete results if the $conditions
|
|
* do not filter out deleted fields.
|
|
*
|
|
* @return
|
|
* An array of fields matching $conditions, keyed by the property specified
|
|
* by the $key parameter.
|
|
* @ingroup update_api
|
|
*/
|
|
function _update_7000_field_read_fields(array $conditions = array(), $key = 'id') {
|
|
$fields = array();
|
|
$query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
|
|
->fields('fc');
|
|
foreach ($conditions as $column => $value) {
|
|
$query->condition($column, $value);
|
|
}
|
|
foreach ($query->execute() 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'];
|
|
|
|
$fields[$field[$key]] = $field;
|
|
}
|
|
return $fields;
|
|
}
|
|
|
|
/**
|
|
* Writes a field instance directly to the database.
|
|
*
|
|
* @ingroup update_api
|
|
*/
|
|
function _update_7000_field_create_instance($field, &$instance) {
|
|
// Merge in defaults.
|
|
$instance += array(
|
|
'field_id' => $field['id'],
|
|
'field_name' => $field['field_name'],
|
|
'deleted' => FALSE,
|
|
'description' => '',
|
|
'required' => FALSE,
|
|
);
|
|
|
|
// 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' => serialize($data),
|
|
'deleted' => (int) $instance['deleted'],
|
|
);
|
|
$instance['id'] = db_insert('field_config_instance')
|
|
->fields($record)
|
|
->execute();
|
|
}
|
|
|
|
/**
|
|
* @addtogroup updates-7.x-to-8.x
|
|
* @{
|
|
*/
|
|
|
|
/**
|
|
* Implements hook_update_dependencies().
|
|
*/
|
|
function field_update_dependencies() {
|
|
// Convert Field API to ConfigEntities after:
|
|
$dependencies['field'][8003] = array(
|
|
// - Custom block bodies have been turned to fields.
|
|
'block' => 8008,
|
|
// - User pictures have been turned to fields.
|
|
'user' => 8011,
|
|
// - The {file_usage}.id column has moved to varchar.
|
|
'file' => 8001,
|
|
);
|
|
return $dependencies;
|
|
}
|
|
|
|
/**
|
|
* Empty update - moved into field_update_8003().
|
|
*/
|
|
function field_update_8001() {
|
|
}
|
|
|
|
/**
|
|
* Migrate all instance display settings to configuration.
|
|
*
|
|
* @ingroup config_upgrade
|
|
*/
|
|
function field_update_8002() {
|
|
$displays = array();
|
|
module_load_install('entity');
|
|
|
|
$query = db_select('field_config_instance', 'fc')->fields('fc');
|
|
foreach ($query->execute() as $record) {
|
|
// Unserialize the data array and start investigating the display key
|
|
// which holds the configuration of this instance for all view modes.
|
|
$data = unserialize($record->data);
|
|
|
|
// Skip field instances that were created directly with the new API earlier
|
|
// in the upgrade path.
|
|
if (!isset($data['display'])) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($data['display'] as $view_mode => $display_options) {
|
|
// Determine name and create initial entry in the $displays array if it
|
|
// does not exist yet.
|
|
$display_id = $record->entity_type . '.' . $record->bundle . '.' . $view_mode;
|
|
if (!isset($displays[$display_id])) {
|
|
$displays[$display_id] = _update_8000_entity_get_display($record->entity_type, $record->bundle, $view_mode);
|
|
}
|
|
|
|
// The display object does not store hidden fields.
|
|
if ($display_options['type'] != 'hidden') {
|
|
// We do not need the 'module' key anymore.
|
|
unset($display_options['module']);
|
|
$displays[$display_id]->set("content.$record->field_name", $display_options);
|
|
}
|
|
}
|
|
|
|
// Remove the 'display' key and save the record back into the table.
|
|
unset($data['display']);
|
|
db_update('field_config_instance')
|
|
->condition('id', $record->id)
|
|
->fields(array(
|
|
'data' => serialize($data),
|
|
))
|
|
->execute();
|
|
}
|
|
|
|
// Migration of 'extra_fields' display settings. Avoid calling
|
|
// entity_get_info() by fetching the relevant variables directly in the
|
|
// 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)) {
|
|
$entity_type = $matches[1];
|
|
$bundle = $matches[2];
|
|
if (isset($variable_value['extra_fields']['display'])) {
|
|
foreach ($variable_value['extra_fields']['display'] as $field_name => $field_settings) {
|
|
foreach ($field_settings as $view_mode => $display_options) {
|
|
// Determine name and create initial entry in the $displays array
|
|
// if it does not exist yet.
|
|
$display_id = $entity_type . '.' . $bundle . '.' . $view_mode;
|
|
if (!isset($displays[$display_id])) {
|
|
$displays[$display_id] = _update_8000_entity_get_display($entity_type, $bundle, $view_mode);
|
|
}
|
|
|
|
// Set options in the display.
|
|
$new_options = array('visible' => $display_options['visible']);
|
|
// The display object only stores the weight for 'visible' extra
|
|
// fields.
|
|
if ($display_options['visible']) {
|
|
$new_options['weight'] = $display_options['weight'];
|
|
}
|
|
$displays[$display_id]->set("content.$field_name", $new_options);
|
|
|
|
// Remove the old entry.
|
|
unset($variable_value['extra_fields']['display']);
|
|
variable_set($variable_name, $variable_value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Save the displays to configuration.
|
|
foreach ($displays as $config) {
|
|
$config->save();
|
|
}
|
|
update_config_manifest_add('entity.display', array_keys($displays));
|
|
}
|
|
|
|
/**
|
|
* Convert fields and instances to config.
|
|
*/
|
|
function field_update_8003() {
|
|
$uuid = new Uuid();
|
|
$manifest_ids = array('fields' => array(), 'instances' => array());
|
|
|
|
$state = Drupal::state();
|
|
$deleted_fields = $state->get('field.field.deleted') ?: array();
|
|
$deleted_instances = $state->get('field.instance.deleted') ?: array();
|
|
|
|
$field_uuids = array();
|
|
|
|
// Migrate field definitions.
|
|
$records = db_query("SELECT * FROM {field_config}")->fetchAll(PDO::FETCH_ASSOC);
|
|
foreach ($records as $record) {
|
|
$record['data'] = unserialize($record['data']);
|
|
|
|
$config = array(
|
|
'id' => $record['field_name'],
|
|
'uuid' => $uuid->generate(),
|
|
'type' => $record['type'],
|
|
'module' => $record['module'],
|
|
'active' => $record['active'],
|
|
'settings' => $record['data']['settings'],
|
|
'storage' => array(
|
|
'type' => $record['storage_type'],
|
|
'module' => $record['storage_module'],
|
|
'active' => $record['storage_active'],
|
|
'settings' => $record['data']['storage']['settings'],
|
|
),
|
|
'locked' => $record['locked'],
|
|
'cardinality' => $record['cardinality'],
|
|
'translatable' => $record['translatable'],
|
|
'entity_types' => $record['data']['entity_types'],
|
|
'indexes' => $record['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';
|
|
}
|
|
|
|
// Save in either config or state.
|
|
if (!$record['deleted']) {
|
|
Drupal::config('field.field.' . $config['id'])
|
|
->setData($config)
|
|
->save();
|
|
$manifest_ids['fields'][] = $config['id'];
|
|
}
|
|
else {
|
|
$config['deleted'] = TRUE;
|
|
$deleted_fields[$config['uuid']] = $config;
|
|
// Additionally, rename the data tables for deleted fields. Technically
|
|
// this would belong in an update in field_sql_storage.module, but it is
|
|
// easier to do it now, when the old numeric ID is available.
|
|
if ($config['storage']['type'] == 'field_sql_storage') {
|
|
$field = new Field($config);
|
|
$tables = array(
|
|
"field_deleted_data_{$record['id']}" => _field_sql_storage_tablename($field),
|
|
"field_deleted_revision_{$record['id']}" => _field_sql_storage_revision_tablename($field),
|
|
);
|
|
foreach ($tables as $table_old => $table_new) {
|
|
if (db_table_exists($table_old)) {
|
|
db_rename_table($table_old, $table_new);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Store the UUID with the old field_id so that we can map the instances.
|
|
$field_uuids[$record['id']] = $config['uuid'];
|
|
}
|
|
|
|
// Migrate instance definitions.
|
|
$records = db_query("SELECT * FROM {field_config_instance}")->fetchAll(PDO::FETCH_ASSOC);
|
|
foreach ($records as $record) {
|
|
$record['data'] = unserialize($record['data']);
|
|
|
|
$config = array(
|
|
'id' => $record['entity_type'] . '.' . $record['bundle'] . '.' . $record['field_name'],
|
|
'uuid' => $uuid->generate(),
|
|
'field_uuid' => $field_uuids[$record['field_id']],
|
|
'entity_type' => $record['entity_type'],
|
|
'bundle' => $record['bundle'],
|
|
'label' => $record['data']['label'],
|
|
'description' => $record['data']['description'],
|
|
'required' => $record['data']['required'],
|
|
'default_value' => isset($record['data']['default_value']) ? $record['data']['default_value'] : array(),
|
|
'default_value_function' => isset($record['data']['default_value_function']) ? $record['data']['default_value_function'] : '',
|
|
'settings' => $record['data']['settings'],
|
|
'widget' => $record['data']['widget'],
|
|
'status' => 1,
|
|
'langcode' => 'und',
|
|
);
|
|
|
|
// Save in either config or state.
|
|
if (!$record['deleted']) {
|
|
Drupal::config('field.instance.' . $config['id'])
|
|
->setData($config)
|
|
->save();
|
|
$manifest_ids['instances'][] = $config['id'];
|
|
}
|
|
else {
|
|
$config['deleted'] = TRUE;
|
|
$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_uuid']))
|
|
->condition('type', 'default_image')
|
|
->condition('module', 'image')
|
|
->condition('id', $record['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.
|
|
*/
|
|
|