function RulesEntityController::save

File

includes/rules.core.inc, line 147

Class

RulesEntityController
Make sure loaded rule configs are instantiated right.

Code

public function save($rules_config, DatabaseTransaction $transaction = NULL) {
    $transaction = isset($transaction) ? $transaction : db_transaction();
    // Load the stored entity, if any.
    if (!isset($rules_config->original) && $rules_config->{$this->idKey}) {
        $rules_config->original = entity_load_unchanged($this->entityType, $rules_config->{$this->idKey});
    }
    $original = isset($rules_config->original) ? $rules_config->original : NULL;
    $return = parent::save($rules_config, $transaction);
    $this->storeTags($rules_config);
    if ($rules_config instanceof RulesTriggerableInterface) {
        $this->storeEvents($rules_config);
    }
    $this->storeDependencies($rules_config);
    // See if there are any events that have been removed.
    if ($original && $rules_config->plugin == 'reaction rule') {
        foreach (array_diff($original->events(), $rules_config->events()) as $event_name) {
            // Check if the event handler implements the event dispatcher interface.
            $handler = rules_get_event_handler($event_name, $rules_config->getEventSettings($event_name));
            if (!$handler instanceof RulesEventDispatcherInterface) {
                continue;
            }
            // Only stop an event dispatcher if there are no rules for it left.
            if (!rules_config_load_multiple(FALSE, array(
                'event' => $event_name,
                'plugin' => 'reaction rule',
                'active' => TRUE,
            )) && $handler->isWatching()) {
                $handler->stopWatching();
            }
        }
    }
    return $return;
}