You are here

public function ReactionRuleStorage::save in Rules 8.3

Implements Drupal\Core\Entity\EntityStorageInterface::save().

Throws

\Drupal\Core\Entity\EntityMalformedException When attempting to save a configuration entity that has no ID.

Overrides ConfigEntityStorage::save

File

src/Entity/ReactionRuleStorage.php, line 113

Class

ReactionRuleStorage
Storage handler for Reaction Rule configuration entities.

Namespace

Drupal\rules\Entity

Code

public function save(EntityInterface $entity) {

  // We need to get the registered events before the rule is saved, in order
  // to be able to check afterwards if we need to rebuild the container or
  // not.
  $events_before = $this
    ->getRegisteredEvents();
  $return = parent::save($entity);
  $events_after = $this
    ->getRegisteredEvents();

  // Update the state of registered events.
  $this->stateService
    ->set('rules.registered_events', $events_after);

  // After the reaction rule is saved, we may need to rebuild the container,
  // otherwise the reaction rule will not fire. However, we can do an
  // optimization: Only rebuild the container if there is a new event which
  // was not already registered before. Similarly if the rule is being
  // disabled and there are no other active rules with this event, then also
  // rebuild the container.
  foreach ($entity
    ->getEventNames() as $event_name) {
    if (empty($events_before[$event_name]) || empty($events_after[$event_name])) {
      $this->drupalKernel
        ->rebuildContainer();
      break;
    }
  }

  // When a reaction rule is saved (either added, updated or enabled/disabled)
  // the cache for its event(s) needs to be invalidated. These tags are set in
  // RulesComponentRepository::getMultiple()
  Cache::invalidateTags($entity
    ->getEventNames());
  return $return;
}