function tablefield_update_7003 in TableField 7.2
Same name and namespace in other branches
- 7.3 tablefield.install \tablefield_update_7003()
Re-save the default existing table fields and all entities containing them.
File
- ./
tablefield.install, line 114 - Installation options for TableField.
Code
function tablefield_update_7003() {
// Change the default field for each content type.
$instances = field_info_instances();
$field_names = array();
foreach ($instances as $entity_type => $entities) {
foreach ($entities as $bundle => $fields) {
foreach ($fields as $field_name => $instance) {
if (in_array($instance['widget']['type'], array(
'tablefield',
))) {
// Uniquely store the field names in an array for later use.
if (!in_array($instance['field_name'], $field_names)) {
array_push($field_names, $instance['field_name']);
}
// Rationalize the table data.
if (!empty($instance['default_value'][0]['tablefield'])) {
// Remove extraneous data.
$count_cols = $instance['default_value'][0]['tablefield']['rebuild']['count_cols'];
$count_rows = $instance['default_value'][0]['tablefield']['rebuild']['count_rows'];
$caption = $instance['default_value'][0]['tablefield']['caption'];
$rebuild = $instance['default_value'][0]['tablefield']['rebuild'];
$import = $instance['default_value'][0]['tablefield']['import'];
$paste = $instance['default_value'][0]['tablefield']['paste'];
unset($instance['default_value'][0]['tablefield']['caption']);
unset($instance['default_value'][0]['tablefield']['rebuild']);
unset($instance['default_value'][0]['tablefield']['import']);
unset($instance['default_value'][0]['tablefield']['paste']);
foreach ($instance['default_value'][0]['tablefield'] as $key => $value) {
if (preg_match('/cell_(.*)_(.*)/', $key, $cell)) {
// $cell[1] is row count $cell[2] is col count.
if ((int) $cell[1] < $count_rows && (int) $cell[2] < $count_cols) {
$cel = explode('_', ltrim($key, 'cell_'));
if ($cel[1] === 'weight') {
$instance['default_value'][0]['tablefield']['tabledata']['row_' . $cel[0]]['weight'] = $value;
}
else {
$instance['default_value'][0]['tablefield']['tabledata']['row_' . $cel[0]]['col_' . $cel[1]] = $value;
}
unset($instance['default_value'][0]['tablefield'][$key]);
}
}
}
}
// Recreate previous removed data.
$instance['default_value'][0]['tablefield']['caption'] = $caption;
$instance['default_value'][0]['tablefield']['rebuild'] = $rebuild;
$instance['default_value'][0]['tablefield']['import'] = $import;
$instance['default_value'][0]['tablefield']['paste'] = $paste;
field_update_instance($instance);
}
}
}
}
// Change all existing fields to store the data with the new format.
foreach ($field_names as $field_name) {
$tables = array(
'field_data_' . $field_name,
'field_revision_' . $field_name,
);
foreach ($tables as $table) {
$field = $field_name . '_value';
$query = db_select($table, 'n')
->fields('n')
->execute()
->fetchAll();
foreach ($query as $record) {
$instance = unserialize($record->{$field});
// Rationalize the table data.
if (!empty($instance)) {
// Remove extraneous data.
$count_cols = $instance['rebuild']['count_cols'];
$count_rows = $instance['rebuild']['count_rows'];
$caption = $instance['caption'];
$rebuild = $instance['rebuild'];
$import = $instance['import'];
$paste = $instance['paste'];
unset($instance['caption']);
unset($instance['rebuild']);
unset($instance['import']);
unset($instance['paste']);
foreach ($instance as $key => $value) {
if (preg_match('/cell_(.*)_(.*)/', $key, $cell)) {
// $cell[1] is row count $cell[2] is col count.
if ((int) $cell[1] < $count_rows && (int) $cell[2] < $count_cols) {
$cel = explode('_', ltrim($key, 'cell_'));
if ($cel[1] === 'weight') {
$instance['tabledata']['row_' . $cel[0]]['weight'] = $value;
}
else {
$instance['tabledata']['row_' . $cel[0]]['col_' . $cel[1]] = $value;
}
unset($instance[$key]);
}
}
}
}
// Recreate previous removed data.
$instance['caption'] = $caption;
$instance['rebuild'] = $rebuild;
$instance['import'] = $import;
$instance['paste'] = $paste;
// Change the stored data by a per record unique column key combination.
db_update($table)
->fields(array(
$field => serialize($instance),
))
->condition('entity_id', $record->entity_id)
->condition('revision_id', $record->revision_id)
->condition('delta', $record->delta)
->condition('entity_type', $record->entity_type)
->condition('bundle', $record->bundle)
->execute();
}
}
}
field_cache_clear();
drupal_set_message(t('All Table Field fields are now stored with a new data format.'), 'warning');
}