Skip to content

Instantly share code, notes, and snippets.

@joshmiller83
Last active November 19, 2015 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmiller83/c6c2338051a00f9b7fc0 to your computer and use it in GitHub Desktop.
Save joshmiller83/c6c2338051a00f9b7fc0 to your computer and use it in GitHub Desktop.
Using this function you can clone drupal blocks from one theme to another for Drupal 7 only.
<?php
/**
* Clones blocks from one theme to another.
*
* @param $original
* The machine name of the original theme.
*
* @param $new
* The machine name of the new theme.
*/
function _clone_drupal_blocks($original,$new) {
// Get array of all blocks assigned to original theme
$sql = 'SELECT b.`bid`,b.`module`,b.`delta`,b.`theme`,b.`region`,b.`status`
FROM {block} as b WHERE `status`=:status AND theme=:theme';
$result = db_query($sql, array(':status'=>1,':theme'=>$original));
$blocks_orginal = $result->fetchAllAssoc('bid');
// Get array of all blocks assigned to new theme
$result = db_query($sql, array(':status'=>1,':theme'=>$new));
$blocks_new = $result->fetchAllAssoc('bid');
// Iterate over the original blocks to check to see if the new theme has a
// similar one.
foreach ($blocks_orginal as $block_original) {
// This is basically what makes the block unique.
// The theme sas's unique key
$key_original = $block_original->module .
$block_original->delta .
$block_original->region;
$search = false;
// Iterate over the new theme's existing blocks
foreach ($blocks_new as $block_new) {
$key_new = $block_new->module .
$block_new->delta .
$block_new->region;
// Money maker right here.
// If we find the key exists in both arrays, just break out of the iteration
// If this key doesn't match ever, then we need to update the database
if ($key_original == $key_new) {
$search = true;
break;
}
}
// If the $block_original isn't enabled on $new, lets create/update it
if ($search == false) {
$result = db_query($sql.' AND module=:module AND delta=:delta',
array(':status' => 0,
':theme' => $new,
':module' => $block_original->module,
':delta' => $block_original->delta));
$blocks_new_disabled = $result->fetchAllAssoc('bid');
// In database, needs updating
if (!empty($blocks_new_disabled)) {
// Only expecting one result, and that result is keyed on bid.
$bid = key($blocks_new_disabled);
db_update('block')
->fields(array(
'region' => $block_original->region,
'status' => 1,
))
->condition('bid', $bid, '=')
->execute();
// Not in database, new
} else {
// Grab full copy of block to be cloned
$sql2 = 'SELECT *
FROM {block} WHERE bid=:bid';
$result = db_query($sql2,array(':bid'=>$block_original->bid));
$block_original_full = $result->fetchAssoc();
// Insert a new block definition for our theme that includes a complete
// copy of the original block
db_insert('block')
->fields(array(
'theme' => $new,
'status' => 1,
'module' => $block_original->module,
'delta' => $block_original->delta,
'region' => $block_original->region,
'weight' => $block_original_full['weight'],
'custom' => $block_original_full['custom'],
'visibility' => $block_original_full['visibility'],
'pages' => $block_original_full['pages'],
'title' => $block_original_full['title'],
'cache' => $block_original_full['cache'],
))
->execute();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment