You are here

public function Asset::delete in Mini site 8

Delete asset, while also removing empty directories.

If the asset is the last one in the directory, the directory will be recursively removed up to the common asset storage directory.

Overrides AssetInterface::delete

File

src/Asset.php, line 297

Class

Asset
Class Asset.

Namespace

Drupal\minisite

Code

public function delete() {

  // Only delete assets that are stored in the database.
  if (isset($this->id)) {
    Database::getConnection()
      ->delete('minisite_asset')
      ->condition('id', $this
      ->id())
      ->execute();
    $this->id = NULL;
  }

  /** @var \Drupal\Core\File\FileSystem $fs */
  $fs = \Drupal::service('file_system');
  $fs
    ->deleteRecursive($this->urlBag
    ->getUri());

  // Remove parent directories if there is no other files up until common
  // assets directory.
  $dirname = $this->urlBag
    ->getUri();
  while (($dirname = $fs
    ->dirname($dirname)) && $dirname != Minisite::getCommonAssetDir()) {
    if (empty(LegacyWrapper::scanDirectory($dirname, '/.*/'))) {
      $fs
        ->deleteRecursive($dirname);
    }
  }
}