Migrate {block_custom}.body and {block_custom}.format to block_body field.
function block_update_8008() {
$sandbox['#finished'] = 0;
if (!isset($sandbox['total'])) {
// Initial invocation.
// First, create the body field.
$body_field = array(
'id' => 'block_body',
'type' => 'text_with_summary',
'entity_types' => array(
'custom_block',
),
'module' => 'text',
'cardinality' => 1,
);
_update_8003_field_create_field($body_field);
$instance = array(
'id' => 'custom_block.basic.block_body',
'entity_type' => 'custom_block',
'bundle' => 'basic',
'label' => 'Block body',
'settings' => array(
'display_summary' => FALSE,
),
);
_update_8003_field_create_instance($body_field, $instance);
module_load_install('entity');
// Assign form settings for the 'default' form mode.
$form_display = _update_8000_entity_get_form_display('custom_block', 'basic', 'default');
$form_display
->set('content.user_picture', array(
'type' => 'text_textarea_with_summary',
))
->save();
// Initialize state for future calls.
$sandbox['last'] = 0;
$sandbox['count'] = 0;
$query = db_select('block_custom', 'bc');
$sandbox['total'] = $query
->countQuery()
->execute()
->fetchField();
$sandbox['body_field_id'] = $body_field['id'];
}
else {
// Subsequent invocations.
$found = FALSE;
if ($sandbox['total']) {
// Operate on each block in turn.
$batch_size = 200;
$query = db_select('block_custom', 'bc');
$query
->fields('bc', array(
'bid',
'body',
'format',
))
->condition('bc.bid', $sandbox['last'], '>')
->orderBy('bc.bid', 'ASC')
->range(0, $batch_size);
$blocks = $query
->execute();
// Load the block, set up 'body' and save the field data.
foreach ($blocks as $block) {
$found = TRUE;
$data = array(
Language::LANGCODE_NOT_SPECIFIED => array(
array(
'format' => $block->format,
'value' => $block->body,
),
),
);
// This is a core update and no contrib modules are enabled yet, so
// we can assume default field storage for a faster update.
_update_8000_field_sql_storage_write('custom_block', 'basic', $block->bid, $block->bid, 'block_body', $data);
$sandbox['last'] = $block->bid;
$sandbox['count'] += 1;
}
$sandbox['#finished'] = min(0.99, $sandbox['count'] / $sandbox['total']);
}
if (!$found) {
// All blocks are processed.
// Remove the now-obsolete body info from block_custom.
db_drop_field('block_custom', 'body');
db_drop_field('block_custom', 'format');
// We're done.
$sandbox['#finished'] = 1;
}
}
}