You are here

public function CerFieldChainHandler::add in Corresponding Entity References 7.3

Adds a reference to the given entity.

File

includes/CerFieldChainHandler.inc, line 76
Contains the CerFieldChainHandler object.

Class

CerFieldChainHandler
@class Wraps around every CerFieldHandler object in a chain. In any given chain, there could be many entities that need to be processed -- think about multi-value field collections embedded within other multi-value field collections, and you…

Code

public function add(EntityDrupalWrapper $entity) {
  $owner = $this->entity;
  foreach ($this->chain as $field) {

    // If the current field implements CerEntityContainerInterface, we can
    // create an entity on-the-fly to receive the reference, if there isn't
    // one already.
    if ($field instanceof CerEntityContainerInterface) {
      $items = $field
        ->getHandler($owner);

      // If there is are items which could receive the reference, seek to the
      // last one. Otherwise, create one.
      if (sizeof($items) == 0) {
        $owner = $field
          ->createInnerEntity($owner);
      }
      else {
        $items
          ->seek(-1);
        $owner = $items
          ->current();
      }
    }
  }
  $field
    ->getHandler($owner)
    ->add($entity);
}