Created
October 15, 2014 00:48
-
-
Save bwood/7db9a91fc931fa1f763e to your computer and use it in GitHub Desktop.
openucb-345: starter content rollback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** openberkeley_news_starter.install ***/ | |
/** | |
* Implements hook_disable(). | |
*/ | |
function openberkeley_news_starter_disable() { | |
_openberkeley_core_override_rollback_starter_content(array( | |
/* | |
* It's important to disable in this order because: Menu depends on Pages. | |
* If do things out of order one or more rollbacks | |
* will silently fail on disable. If you use drush migrate-rollback commands | |
* you'll see "skipping migration...due to unfulfilled dependencies" | |
*/ | |
//'OpenBerkeleyNewsStarterMenu', | |
'OpenBerkeleyNewsStarterNodePages', | |
)); | |
variable_set('openberkeley_news_use_starter_content', 0); | |
} | |
/*** openberkeley_core_override.module ***/ | |
/** | |
* Helper function to rollback starter content using Migrate. | |
*/ | |
function _openberkeley_core_override_rollback_starter_content($classes = array()) { | |
// Roll back the migrations to delete the demo content. | |
foreach ($classes as $class) { | |
// Get a handle on our migration class. | |
$migration = Migration::getInstance($class); | |
// Disable message output from migrate ince this import is used within | |
// the context of the Apps module where these messages make no sense. | |
Migration::setDisplayFunction('_panopoly_core_null_message'); | |
// Determine if starter content nodes have changed. | |
// So, first we want to lookup the nid's and hashes of the nodes in the database. | |
//$migration = Migration::getInstance($migration_name); | |
$node_hashes = array(); | |
//TODO: protected function | |
$migration->preImport(); | |
$migration->source->rewind();//Call to a member function getMap() on a non-object in | |
while ($this->source->valid()) { | |
$data_row = $this->source->current(); | |
$this->sourceValues = $data_row; | |
$this->applyMappings(); | |
// Get the imported node. | |
$node_from_import = $this->destinationValues; | |
if (method_exists($migration, 'prepare')) { | |
$migration->prepare($node_from_import, $data_row); | |
} | |
$source_key = array(); | |
foreach ($this->map->sourceKeyMap as $key_name) { | |
$source_key[] = $data_row->$key_name; | |
} | |
$destination_id = $migration->lookupDestinationID($source_key); | |
$node_hashes[$destination_id[0]] = _openberkeley_core_override_get_node_hash($node_from_import); | |
} | |
// Now, we loop over those and do the selective rollback. | |
//$migration = Migration::getInstance($migration_name); | |
$migration = Migration::getInstance($class); | |
$migration->preRollback(); | |
foreach ($node_hashes as $nid => $hash) { | |
$node = node_load($nid); | |
if (_openberkeley_core_override_get_node_hash($node) == $hash) { | |
//$migration->destination->rollback(array($nid)); | |
drupal_set_message("Removing: " . $node->title); | |
// Delete the sourceid from the migration map | |
$source_key = $migration->map->lookupSourceID(array($nid)); | |
$migration->map->delete($source_key); | |
} | |
} | |
$migration->postRollback(); | |
} | |
// deregister the Migration classes since it will not exist after the | |
// module is disabled. | |
foreach ($classes as $class) { | |
Migration::deregisterMigration($class); | |
} | |
} | |
function _openberkeley_core_override_get_node_hash($node) { | |
$cleaned = array(); | |
$base_fields = array('title'); | |
//$config_fields = _helper_to_get_fields_for_this_bundle($node->type); | |
$config_fields = array_keys(field_info_instances('node', $node->type)); | |
$value_blacklist = array('safe_value'); | |
foreach ($base_fields as $name) { | |
$cleaned[$name] = $node->$name; | |
} | |
foreach ($config_fields as $name) { | |
$cleaned[$name] = $node->$name; | |
// @todo: Probably should really use field_get_items(). | |
foreach ($cleaned[$name][LANGUAGE_NONE] as $delta => $value) { | |
foreach ($value as $value_name => $value_value) { | |
if (!in_array($value_name, $value_blacklist)) { | |
$cleaned[$name][LANGUAGE_NONE][$delta][$value_name] = $value_value; | |
} | |
} | |
} | |
} | |
ksort($cleaned); | |
return md5(serialize($cleaned)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment