You are here

public function FieldCollectionItemEntity::save in Field collection 7

Save the field collection item.

By default, always save the host entity, so modules are able to react upon changes to the content of the host and any 'last updated' dates of entities get updated.

For creating an item a host entity has to be specified via setHostEntity() before this function is invoked. For the link between the entities to be fully established, the host entity object has to be updated to include a reference on this field collection item during saving. So do not skip saving the host for creating items.

Parameters

$skip_host_save: (internal) If TRUE is passed, the host entity is not saved automatically and therefore no link is created between the host and the item or revision updates might be skipped. Use with care.

Overrides Entity::save

File

./field_collection.entity.inc, line 464

Class

FieldCollectionItemEntity
Class for field_collection_item entities.

Code

public function save($skip_host_save = FALSE) {

  // Make sure we have a host entity during creation.
  if (!empty($this->is_new) && !(isset($this->hostEntityId) || isset($this->hostEntity) || isset($this->hostEntityRevisionId))) {
    throw new Exception('Unable to create a field collection item without a given host entity.');
  }

  // Copy the values of translatable fields for a new field collection item.
  if (!empty($this->is_new) && field_collection_item_is_translatable() && $this
    ->langcode() == LANGUAGE_NONE) {
    $this
      ->copyTranslations();
  }

  // Only save directly if we are told to skip saving the host entity. Else,
  // we always save via the host as saving the host might trigger saving
  // field collection items anyway (e.g. if a new revision is created).
  if ($skip_host_save) {
    return entity_get_controller($this->entityType)
      ->save($this);
  }
  $host_entity = $this
    ->hostEntity();
  if (!$host_entity) {
    throw new Exception('Unable to save a field collection item without a valid reference to a host entity.');
  }

  // If this is creating a new revision, also do so for the host entity.
  if (!empty($this->revision) || !empty($this->is_new_revision)) {
    $host_entity->revision = TRUE;
    if (!empty($this->default_revision)) {
      entity_revision_set_default($this->hostEntityType, $host_entity);
    }
  }

  // Set the host entity reference, so the item will be saved with the host.
  // @see field_collection_field_presave()
  $delta = $this
    ->delta();
  if (isset($delta)) {
    $host_entity->{$this->field_name}[$this
      ->langcode()][$delta] = array(
      'entity' => $this,
    );
  }
  else {
    $host_entity->{$this->field_name}[$this
      ->langcode()][] = array(
      'entity' => $this,
    );
  }
  return entity_save($this->hostEntityType, $host_entity);
}