You are here

public function Asset::save in Mini site 8

Save asset to the database.

If internal $id is set, the asset will be updated, otherwise it will be created.

Return value

int|null ID of the created or updated asset. NULL if asset has not been saved to the database.

Overrides AssetInterface::save

File

src/Asset.php, line 261

Class

Asset
Class Asset.

Namespace

Drupal\minisite

Code

public function save() {
  $fields = [
    'entity_type' => $this->entityType,
    'entity_bundle' => $this->entityBundle,
    'entity_id' => $this->entityId,
    'entity_language' => $this->entityLanguage,
    'field_name' => $this->fieldName,
    'source' => $this
      ->getUri(),
    'alias' => !empty($this
      ->getAlias()) ? $this
      ->getAlias() : '',
    'filemime' => $this
      ->getMimeType(),
    'filesize' => $this
      ->getSize(),
  ];
  if (!empty($this->id)) {
    $fields['id'] = $this->id;
  }
  if (empty($fields['id'])) {
    $id = Database::getConnection()
      ->insert('minisite_asset')
      ->fields($fields)
      ->execute();
  }
  else {
    $id = Database::getConnection()
      ->update('minisite_asset')
      ->fields($fields)
      ->condition('id', $fields['id'])
      ->execute();
  }
  $this
    ->setId($id);
  return $id;
}