You are here

public function FieldCollectionItemEntity::updateHostEntity in Field collection 7

Updates the wrapped host entity object.

Parameters

object $entity: Host entity.

string $host_entity_type: The entity type of the entity the field collection is attached to.

File

./field_collection.entity.inc, line 223

Class

FieldCollectionItemEntity
Class for field_collection_item entities.

Code

public function updateHostEntity($entity, $host_entity_type = NULL) {
  $this
    ->fetchHostDetails($entity);

  // If it isn't possible to retrieve hostEntityType due to the fact that it's
  // not saved in the DB yet then fill in info about the hostEntity manually.
  // This happens when creating a new revision of a field collection entity
  // and it needs to relate to the new revision of the host entity.
  if (!$this->hostEntityType || isset($entity->tid)) {
    $this->hostEntityType = $host_entity_type;
    $this->hostEntity = $entity;
    list($this->hostEntityId, $this->hostEntityRevisionId) = entity_extract_ids($this->hostEntityType, $this->hostEntity);
  }
  list($recieved_id) = entity_extract_ids($this->hostEntityType, $entity);
  if (!empty($this->hostEntityId) && $this
    ->isInUse()) {
    if (is_array($this->hostEntityId)) {
      $current_id = in_array($recieved_id, $this->hostEntityId) ? $recieved_id : FALSE;
    }
    else {
      $current_id = $this->hostEntityId;
    }
  }
  else {
    $current_host = entity_revision_load($this->hostEntityType, $this->hostEntityRevisionId);
    list($current_id) = $current_host ? entity_extract_ids($this->hostEntityType, $current_host) : array(
      $recieved_id,
    );
  }
  if ($current_id == $recieved_id) {
    $this->hostEntity = $entity;
    $delta = $this
      ->delta();
    if (isset($entity->{$this->field_name}[$this
      ->langcode()][$delta]['entity'])) {
      $entity->{$this->field_name}[$this
        ->langcode()][$delta]['entity'] = $this;
    }
  }
  else {
    throw new Exception('The host entity cannot be changed.');
  }
}