You are here

protected function CerHandler::addReferenceTo in Corresponding Entity References 7.2

Creates a reference to the remote entity on the local entity. Throws CerException if the local entity already references the remote entity, or if the field cannot hold any more values.

Parameters

object $entity: The remote entity.

1 call to CerHandler::addReferenceTo()
CerHandler::reference in ./handler.inc
Implements CerHandlerInterface::reference().

File

./handler.inc, line 349
Contains base code for CER handlers, which are objects responsible for creating, updating and deleting corresponding references between entities.

Class

CerHandler
@class Generic CER handler with rudimentary language handling.

Code

protected function addReferenceTo($entity) {
  $id = $this
    ->getRemoteEntityID($entity);
  if ($this
    ->references($entity)) {
    throw new CerException(t('Cannot create duplicate reference to remote entity.'));
  }
  elseif ($this
    ->filled($this
    ->getLocalReferenceIDs(), $this->local['field'])) {
    throw new CerException(t('Local field cannot support any more references.'));
  }
  else {
    $languages = field_available_languages($this->local['entity_type'], $this->local['field']);
    foreach ($languages as $language) {
      $this->entity->{$this->local['field_name']}[$language][] = array(
        'target_id' => $id,
      );
    }
  }
}