/** * Remove field storage information when field data is purged. * * Called from field_purge_data() to allow the field storage * module to delete field data information. * * @param $entity_type * The type of $entity; for example, 'node' or 'user'. * @param $entity * The pseudo-entity whose field data to delete. * @param $field * The (possibly deleted) field whose data is being purged. * @param $instance * The deleted field instance whose data is being purged. */ function hook_field_storage_purge($entity_type, $entity, $field, $instance) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); $table_name = _field_sql_storage_tablename($field); $revision_name = _field_sql_storage_revision_tablename($field); db_delete($table_name)->condition('entity_type', $entity_type)->condition('entity_id', $id)->execute(); db_delete($revision_name)->condition('entity_type', $entity_type)->condition('entity_id', $id)->execute(); }
public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false) { /* @var $node FieldNode */ $object = $node->getValue(); if (!is_array($object)) { $object = array(); } if (!isset($object['type'])) { $context->logCritical(sprintf("%s: has no type", $node->getPath())); } $name = $node->getName(); $type = $object['type']; $typeInfo = field_info_field_types($type); if (empty($typeInfo)) { $context->logCritical(sprintf("%s: type %s does not exist", $node->getPath(), $type)); } if ($this->exists($node, $context)) { $existing = $this->getExistingObject($node, $context); } else { $existing = null; } if (array_key_exists('settings', $object) && !is_array($object['settings'])) { $context->log(sprintf("%s: no settings provided, defaulting with empty array", $node->getPath())); $object['settings'] = array(); } $object['field_name'] = $name; if (empty($object['cardinality'])) { $object['cardinality'] = 1; } // Consistency check, prior to do anything, ensure the database tables // are not already there, this happens, sometimes if (!$existing) { if (empty($object['storage']) || 'field_sql_storage' === $object['storage']['type']) { // Prevents warning on unspecified key if (!isset($object['deleted'])) { $object['deleted'] = false; } $tables = [_field_sql_storage_tablename($object), _field_sql_storage_revision_tablename($object)]; foreach ($tables as $table) { if (db_table_exists($table)) { $context->logDataloss(sprintf("%s: %s: table already exists prior to creating field", $node->getPath(), $table)); // If code has not broken here, then go for deletion db_drop_table($table); } } } } if ($existing) { $doDelete = false; $eType = $existing['type']; // Ensure the cardinality change if any is safe to proceed with $cardinality = $object['cardinality'] - $existing['cardinality']; if (0 !== $cardinality) { if (0 < $cardinality || -1 == $object['cardinality']) { $context->log(sprintf("%s: safe cardinality change", $node->getPath())); } else { // @todo Ensure there is data we can save in field if (false) { $context->log(sprintf("%s: safe cardinality change due to data shape", $node->getPath())); } else { $context->logDataloss(sprintf("%s: unsafe cardinality change", $node->getPath())); } } } if ($type !== $eType) { $doDelete = true; $instances = $this->getInstances($name); if (empty($instances)) { $context->logWarning(sprintf("%s: type change (%s -> %s): no instances", $node->getPath(), $type, $eType)); } else { // @todo Ensure there is data if there is instances if (false) { $context->logWarning(sprintf("%s: type change (%s -> %s): existing instances are empty", $node->getPath(), $type, $eType)); } else { // @todo Safe should ensure schema is the same if (false) { $context->logWarning(sprintf("%s: type change (%s -> %s): field schema is the same", $node->getPath(), $type, $eType)); } else { $context->logDataloss(sprintf("%s: type change (%s -> %s): data loss detected", $node->getPath(), $type, $eType)); } } } } if ($doDelete) { $this->deleteExistingObject($node, $context); field_create_field($object); // @todo Recreate instances } else { field_update_field($object); } } else { field_create_field($object); } }
/** * Temporary helper to re-create equivalent * of content_database_info. */ function date_api_database_info($field) { $data = $field['storage']['details']['sql'][FIELD_LOAD_CURRENT]; $db_info = array('columns' => $data); $current_table = _field_sql_storage_tablename($field); $revision_table = _field_sql_storage_revision_tablename($field); $db_info['table'] = $current_table; return $db_info; }
/** * Purge all data from a field instance. * * @param array $instance * The field instance definition. This may be deleted or inactive. */ public static function purgeInstanceData(array $instance) { $field = static::readFieldByID($instance['field_id']); $data_table = _field_sql_storage_tablename($field); // Ensure the entity caches are cleared for the changed entities. if ($ids = db_query("SELECT entity_id FROM {$data_table} WHERE entity_type = :type AND bundle = :bundle", array(':type' => $instance['entity_type'], ':bundle' => $instance['bundle']))->fetchCol()) { entity_get_controller($instance['entity_type'])->resetCache($ids); db_delete($data_table)->condition('entity_type', $instance['entity_type'])->condition('bundle', $instance['bundle'])->execute(); } $revision_table = _field_sql_storage_revision_tablename($field); if (db_table_exists($revision_table)) { db_delete($revision_table)->condition('entity_type', $instance['entity_type'])->condition('bundle', $instance['bundle'])->execute(); } watchdog('helper', "Purged data for field instance ID {$instance['id']}."); }
public static function changeInstanceField(array $instance, $new_field_name) { $old_field = field_info_field($instance['field_name']); $new_field = field_info_field($new_field_name); if ($old_field['type'] != $new_field['type']) { throw new FieldException("Cannot change field instance because they are not the same field type."); } if ($old_field['storage']['type'] !== 'field_sql_storage') { throw new FieldException("Unable to change field type for field {$old_field['field_name']} using storage {$old_field['storage']['type']}."); } if ($new_field['storage']['type'] !== 'field_sql_storage') { throw new FieldException("Unable to change field type for field {$new_field['field_name']} using storage {$new_field['storage']['type']}."); } if (!field_info_instance($instance['entity_type'], $new_field_name, $instance['bundle'])) { $new_instance = $instance; $new_instance['field_name'] = $new_field_name; field_create_instance($new_instance); watchdog('helper', "Created new field instance: {$instance['entity_type']}.{$instance['bundle']}.{$new_field_name}"); } // Copy data from old field tables to the new field tables. $old_data_table = _field_sql_storage_tablename($old_field); $new_data_table = _field_sql_storage_tablename($new_field); $query = db_select($old_data_table, 'old'); $query->fields('old'); $query->condition('entity_type', $instance['entity_type']); $query->condition('bundle', $instance['bundle']); db_insert($new_data_table)->from($query)->execute(); $old_revision_table = _field_sql_storage_revision_tablename($old_field); if (db_table_exists($old_revision_table)) { $new_revision_table = _field_sql_storage_revision_tablename($new_field); $query = db_select($old_revision_table, 'old'); $query->fields('old'); $query->condition('entity_type', $instance['entity_type']); $query->condition('bundle', $instance['bundle']); db_insert($new_revision_table)->from($query)->execute(); } FieldHelper::deleteInstance($instance); }