You are here

public function FieldCollectionItemEntity::setHostEntity in Field collection 7

Sets the host entity. Only possible during creation of a item.

Parameters

$create_link: (optional) Whether a field-item linking the host entity to the field collection item should be created.

File

./field_collection.entity.inc, line 187

Class

FieldCollectionItemEntity
Class for field_collection_item entities.

Code

public function setHostEntity($entity_type, $entity, $langcode = LANGUAGE_NONE, $create_link = TRUE) {
  if (!empty($this->is_new)) {
    $this->hostEntityType = $entity_type;
    $this->hostEntity = $entity;
    $this->langcode = $langcode;
    list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity);

    // If the host entity is not saved yet, set the id to FALSE. So
    // fetchHostDetails() does not try to load the host entity details.
    // Checking value of $this->hostEntityId.
    if (!isset($this->hostEntityId)) {
      $this->hostEntityId = FALSE;
    }

    // We are create a new field collection for a non-default entity, thus
    // set archived to TRUE.
    if (!entity_revision_is_default($entity_type, $entity)) {
      $this->hostEntityId = FALSE;
      $this->archived = TRUE;
    }
    if ($create_link) {
      $entity->{$this->field_name}[$this->langcode][] = array(
        'entity' => $this,
      );
    }
  }
  else {
    throw new Exception('The host entity may be set only during creation of a field collection item.');
  }
}