You are here

public function ParagraphsItemEntity::save in Paragraphs 7

Save the paragraphs 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 paragraphs item during saving. So do not skip saving the host for creating items.

Parameters

bool $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.

Return value

DrupalEntityControllerInterface The entity controller object for the specified entity type

Throws

Exception In case a valid reference is not their for host entity.

Overrides Entity::save

File

./ParagraphsItemEntity.inc, line 389

Class

ParagraphsItemEntity
Entity implementation for the paragraphs entity.

Code

public function save($skip_host_save = FALSE) {

  // 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
  // paragraphs items anyway (for example, if a new revision is created).
  if ($skip_host_save) {
    return entity_get_controller($this->entityType)
      ->save($this);
  }
  else {
    $host_entity = $this
      ->hostEntity();
    if (!$host_entity) {
      throw new Exception("Unable to save a paragraph 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 paragraphs_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);
  }
}