You are here

public function ParagraphsItemEntity::setHostEntity in Paragraphs 7

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

Parameters

string $entity_type: The entity type of the host.

object $entity: The host entity.

string $langcode: (optional) The field language code we should use for host entity.

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

Throws

Exception When you try to set the host when the item has already been created.

File

./ParagraphsItemEntity.inc, line 193

Class

ParagraphsItemEntity
Entity implementation for the paragraphs entity.

Code

public function setHostEntity($entity_type, $entity, $langcode = LANGUAGE_NONE, $create_link = TRUE) {
  $this->hostEntityType = $entity_type;
  $this->hostEntity = $entity;
  $this->langcode = $langcode;
  list($this->hostEntityId, $this->hostEntityRevisionId, $this->hostEntityBundle) = 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.
  if (!isset($this->hostEntityId)) {
    $this->hostEntityId = FALSE;
  }

  // We are create a new paragraphs 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,
    );
  }
  $this->fetchedHostEntityDetails = TRUE;
}