2007-10-05 16:07:22 +00:00
<?php
2009-04-26 16:30:28 +00:00
/**
* @file
2009-05-13 19:42:18 +00:00
* Install, update and uninstall functions for the block module.
2009-04-26 16:30:28 +00:00
*/
2013-02-18 22:17:49 +00:00
use Drupal\Component\Uuid\Uuid;
2013-05-25 20:12:45 +00:00
use Drupal\Core\Language\Language;
2009-04-26 16:30:28 +00:00
2009-02-03 12:30:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_install().
2009-02-03 12:30:14 +00:00
*/
function block_install() {
2009-05-24 17:39:35 +00:00
// Block should go first so that other modules can alter its output
2009-05-21 21:12:25 +00:00
// during hook_page_alter(). Almost everything on the page is a block,
// so before block module runs, there will not be much to alter.
2012-10-09 20:32:40 +00:00
module_set_weight('block', -5);
2009-02-03 12:30:14 +00:00
}
2011-09-29 03:29:59 +00:00
/**
2011-10-26 07:48:38 +00:00
* @addtogroup updates-7.x-to-8.x
2011-09-29 03:29:59 +00:00
* @{
*/
2012-06-05 12:19:14 +00:00
/**
* Implements hook_update_dependencies().
*/
function block_update_dependencies() {
// Convert role IDs after User module converted {role}.
$dependencies['block'][8002] = array(
'user' => 8002,
);
2012-11-27 22:26:22 +00:00
// Migrate users.data after User module prepared the tables.
$dependencies['block'][8005] = array(
2013-02-18 22:17:49 +00:00
'user' => 8016,
2012-11-27 22:26:22 +00:00
);
2013-09-01 06:20:08 +00:00
// Migrate custom blocks after field storage has been reorganized.
$dependencies['block'][8008] = array(
'field' => 8006,
);
2012-06-05 12:19:14 +00:00
return $dependencies;
}
2011-09-29 03:29:59 +00:00
/**
* Block cache is always enabled in 8.x.
2012-06-02 19:45:56 +00:00
*
* @ingroup config_upgrade
2011-09-29 03:29:59 +00:00
*/
function block_update_8000() {
2012-08-07 19:11:13 +00:00
update_variable_del('block_cache');
2011-09-29 03:29:59 +00:00
}
2012-04-19 17:45:46 +00:00
/**
* Creates table {block_language} for language visibility settings per block.
*/
function block_update_8001() {
$schema = array(
'description' => 'Sets up display criteria for blocks based on langcode.',
'fields' => array(
'module' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => "The block's origin module, from {block}.module.",
),
'delta' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => "The block's unique delta within module, from {block}.delta.",
),
'type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => "Language type name. Applied to filter the block by that type.",
),
'langcode' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'description' => "The machine-readable name of this language from {language}.langcode.",
),
),
'primary key' => array('module', 'delta', 'type', 'langcode'),
);
db_create_table('block_language', $schema);
}
2012-06-05 12:19:14 +00:00
/**
* Replace serial role IDs with machine name strings.
*
* @see user_update_8002()
*/
function block_update_8002() {
// Change serial rid column into string.
$column = array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => "The user's role ID from {users_roles}.rid.",
);
db_change_field('block_role', 'rid', 'rid', $column);
// Rename the built-in serial role IDs into the hardcoded machine names.
db_update('block_role')
->fields(array('rid' => DRUPAL_ANONYMOUS_RID))
->condition('rid', 1)
->execute();
db_update('block_role')
->fields(array('rid' => DRUPAL_AUTHENTICATED_RID))
->condition('rid', 2)
->execute();
}
2012-09-24 09:23:59 +00:00
/**
* Increase {block}.title length to 255 characters.
*/
function block_update_8003() {
db_change_field('block', 'title', 'title', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2013-01-30 04:02:37 +00:00
'description' => 'Custom title for the block. (Empty string will use block default title, - None - will remove the title, text will cause block to use specified title.)',
2012-09-24 09:23:59 +00:00
)
);
}
2012-10-26 16:07:34 +00:00
/**
* Rename default menu names.
*/
function block_update_8004() {
// System menu's new block deltas are prefixed with 'menu-'.
$map = array(
'navigation' => 'menu-tools',
'management' => 'menu-admin',
'user-menu' => 'menu-account',
'main-menu' => 'menu-main',
);
foreach ($map as $old => $new) {
db_update('block')
->condition('module', 'system')
->condition('delta', $old)
->fields(array('delta' => $new))
->execute();
db_update('block_language')
->condition('module', 'system')
->condition('delta', $old)
->fields(array('delta' => $new))
->execute();
db_update('block_role')
->condition('module', 'system')
->condition('delta', $old)
->fields(array('delta' => $new))
->execute();
}
}
2012-11-27 22:26:22 +00:00
/**
* Migrate {users}.data into {users_data}.
*/
function block_update_8005() {
$query = db_select('_d7_users_data', 'ud');
$query->addField('ud', 'uid');
$query->addExpression("'block'", 'module');
$query->addExpression("'block'", 'name');
// Take over the extracted and serialized value in {_d7_users_data} as-is.
$query->addField('ud', 'value');
$query->addExpression('1', 'serialized');
$query->condition('name', 'block');
db_insert('users_data')
->from($query)
->execute();
db_delete('_d7_users_data')
->condition('name', 'block')
->execute();
}
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
/**
* Enable the Custom Block module.
*/
function block_update_8006() {
2013-06-06 08:08:39 +00:00
module_enable(array('custom_block'));
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
}
2013-02-18 22:17:49 +00:00
/**
* Migrate {block_custom} to {custom_block}.
*
* Note this table now resides in custom_block_schema() but for 7.x to 8.x
* upgrades, changes must be made from block module as custom_block module is
* only enabled during upgrade process.
*/
function block_update_8007() {
// Populate the {custom_block} and {custom_block_revision} table.
$results = db_select('block_custom', 'bc')->fields('bc')->execute();
$uuid = new Uuid();
$execute = FALSE;
$block_insert = db_insert('custom_block')->fields(array(
'id',
'uuid',
'info',
'revision_id',
'langcode',
'type'
));
$revision_insert = db_insert('custom_block_revision')->fields(array(
'id',
'revision_id',
'log',
'info'
));
foreach ($results as $block) {
$custom_block = array(
'id' => $block->bid,
'uuid' => $uuid->generate(),
'info' => $block->info,
'revision_id' => $block->bid,
2013-05-25 20:12:45 +00:00
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
2013-02-18 22:17:49 +00:00
'type' => 'basic'
);
$revision = array(
'id' => $block->bid,
'revision_id' => $block->bid,
'info' => $block->info,
'log' => 'Initial value from 7.x to 8.x upgrade'
);
$block_insert->values($custom_block);
$revision_insert->values($revision);
// We have something to execute.
$execute = TRUE;
}
if ($execute) {
$block_insert->execute();
$revision_insert->execute();
}
}
/**
* 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(
2013-09-01 06:20:08 +00:00
'name' => 'block_body',
'entity_type' => 'custom_block',
2013-02-18 22:17:49 +00:00
'type' => 'text_with_summary',
'module' => 'text',
'cardinality' => 1,
2013-08-01 13:40:18 +00:00
'schema' => array(
'columns' => array(
'value' => array(
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
),
'summary' => array(
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
),
'format' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
),
'indexes' => array(
'format' => array('format'),
),
'foreign keys' => array(
'format' => array(
'table' => 'filter_format',
'columns' => array('format' => 'format'),
),
),
),
2013-02-18 22:17:49 +00:00
);
2013-06-06 09:06:46 +00:00
_update_8003_field_create_field($body_field);
2013-02-18 22:17:49 +00:00
2013-09-01 06:20:08 +00:00
2013-02-18 22:17:49 +00:00
$instance = array(
'entity_type' => 'custom_block',
'bundle' => 'basic',
'label' => 'Block body',
'settings' => array('display_summary' => FALSE),
);
2013-06-06 09:06:46 +00:00
_update_8003_field_create_instance($body_field, $instance);
2013-02-18 22:17:49 +00:00
2013-05-19 20:02:16 +00:00
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();
2013-02-18 22:17:49 +00:00
// 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(
2013-05-25 20:12:45 +00:00
Language::LANGCODE_NOT_SPECIFIED => array(
2013-02-18 22:17:49 +00:00
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.
2013-09-01 06:20:08 +00:00
_update_8006_field_write_data_sql('custom_block', 'basic', $block->bid, $block->bid, 'block_body', $data);
2013-02-18 22:17:49 +00:00
$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;
}
}
}
2011-09-29 03:29:59 +00:00
/**
2012-05-17 12:58:49 +00:00
* @} End of "addtogroup updates-7.x-to-8.x".
2011-09-29 03:29:59 +00:00
* The next series of updates should start at 9000.
*/