You are here

public function BusinessRulesUtil::removeItemReferences in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Util/BusinessRulesUtil.php \Drupal\business_rules\Util\BusinessRulesUtil::removeItemReferences()

Remove the item references after it's deletion.

Parameters

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event.

File

src/Util/BusinessRulesUtil.php, line 801

Class

BusinessRulesUtil
Class BusinessRulesUtil.

Namespace

Drupal\business_rules\Util

Code

public function removeItemReferences(BusinessRulesEvent $event) {

  /** @var \Drupal\business_rules\Entity\BusinessRulesItemBase $item */
  $item = $event
    ->getSubject();
  $conditions = $this
    ->getConditionsUsingItem($item);
  $actions = $this
    ->getActionsUsingItem($item);
  $rules = $this
    ->getBusinessRulesUsingThisItem($item);

  // Variable's references work in a different manner.
  if ($item instanceof Variable) {
    return;
  }

  // Remove item from conditions.

  /** @var \Drupal\business_rules\Entity\Condition $condition */
  foreach ($conditions as $condition) {
    $success_items = $condition
      ->getSuccessItems();

    /** @var \Drupal\business_rules\BusinessRulesItemObject $success_item */
    foreach ($success_items as $success_item) {
      if ($success_item
        ->getId() == $item
        ->id()) {
        $condition
          ->removeSuccessItem($success_item);
      }
    }
    $fail_items = $condition
      ->getFailItems();

    /** @var \Drupal\business_rules\BusinessRulesItemObject $fail_item */
    foreach ($fail_items as $fail_item) {
      if ($fail_item
        ->getId() == $item
        ->id()) {
        $condition
          ->removeFailItem($fail_item);
      }
    }
    $condition
      ->save();
  }

  // Remove item from actions.

  /** @var \Drupal\business_rules\Entity\Action $action */
  foreach ($actions as $action) {
    $action_items = $action
      ->getSettings('items');
    unset($action_items[$item
      ->id()]);
    $action
      ->setSetting('items', $action_items);
    $action
      ->save();
  }

  // Remove item from business rules.

  /** @var \Drupal\business_rules\Entity\BusinessRule $rule */
  foreach ($rules as $rule) {
    $rule_items = $rule
      ->getItems();

    /** @var \Drupal\business_rules\BusinessRulesItemObject $rule_item */
    foreach ($rule_items as $rule_item) {
      if ($rule_item
        ->getId() == $item
        ->id()) {
        $rule
          ->removeItem($rule_item);
      }
    }
    $rule
      ->save();
  }
}