You are here

public function RulesConditionalElement::setParent in Conditional Rules 7

Same name and namespace in other branches
  1. 8 includes/rules_conditional.core.inc \RulesConditionalElement::setParent()

@todo Remove once http://drupal.org/node/1671344 is resolved.

Overrides RulesPlugin::setParent

File

includes/rules_conditional.core.inc, line 209
Conditional Rules framework implementation.

Class

RulesConditionalElement
Base conditional element plugin implementation.

Code

public function setParent(RulesContainerPlugin $parent) {
  if ($this->parent === $parent) {
    return;
  }

  // Check parent class against the compatible class.
  $pluginInfo = $this
    ->pluginInfo();
  if (empty($pluginInfo['embeddable'])) {
    throw new RulesEvaluationException('This element cannot be embedded.', array(), $this, RulesLog::ERROR);
  }
  elseif (!$parent instanceof $pluginInfo['embeddable']) {
    throw new RulesEvaluationException('The given container is incompatible with this element.', array(), $this, RulesLog::ERROR);
  }
  if (isset($this->parent) && ($key = array_search($this, $this->parent->children, TRUE)) !== FALSE) {

    // Remove element from any previous parent.
    unset($this->parent->children[$key]);
    $this->parent
      ->resetInternalCache();
  }

  // Update parent.
  $this->parent = $parent;
  $parent->children[] = $this;
  $this->parent
    ->resetInternalCache();
}