Disable rules during a migration
During a migration it can be a good idea to disable Rules that send email or some other activity that would be quite "noisy" during a migration. The code sample below shows one way to disable Rules during a migration.
abstract class MyMigration extends Migration {
/**
* Runs before an import starts.
*
* Used to disable any rules which could cause problems during
* the import.
*/
public function preImport() {
parent::preImport();
if (!empty($this->arguments['disable_rules']) && module_exists('rules')) {
if (!is_array($this->arguments['disable_rules'])) {
$this->arguments['disable_rules'] = array($this->arguments['disable_rules']);
}
$rules = db_select('rules_config', 'rc')
->fields('rc', array('name'))
->condition('name', $this->arguments['disable_rules'], 'IN')
->condition('active', 1)
->execute()
->fetchAll();
$this->arguments['disable_rules'] = array();
foreach ($rules as $rule) {
$this->arguments['disable_rules'][] = $rule->name;
}
// Check again to ensure we didn't just eliminate everything.
if (!empty($this->arguments['disable_rules'])) {
db_update('rules_config')
->fields(array('active' => 0))
->condition('name', $this->arguments['disable_rules'], 'IN')
->execute();
rules_clear_cache(TRUE);
}
}
}
/**
* Ran after completion of a migration.
*
* Used to turn any rules that were disabled back on.
*/
public function postImport() {
parent::postImport();
if (!empty($this->arguments['disable_rules']) && module_exists('rules')) {
db_update('rules_config')
->fields(array('active' => 1))
->condition('name', $this->arguments['disable_rules'], 'IN')
->execute();
rules_clear_cache(TRUE);
}
}
}
You define disable_rules inside of your hook_migrate_api() implementation, as an example:
function mymodule_migrate_api() {
$api = array(
'api' => 2,
'groups' => array(
'mygroup' => array(
'title' => t('My Group'),
'disable_rules' => array(
'rules_content_changes',
'rules_noisy_emailer',
),
),
),
'migrations' => array(
'MyMigration' => array(
'class_name' => 'MyMigration',
'group_name' => 'mygroup',
),
),
);
return $api;
}
Putting the 'disable_rules' section in the group tells all migrations to disable those rules. You can override the setting in the individual migration, at which point the group level setting will not be run.
Place this code in a top-level migration class that all of your migration classes inherit. e.g. a new class called MyBlogMigration could be created that extends MyMigration, and that class would automatically disable the rules in question.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion