You are here

public function MinifyJs::removeMinifiedFile in Minify JS 8.2

Remove minified file.

Helper function removes the file, the entry in the file_managed table and sets the file status as unminified.

Parameters

int $fid: The file id of the file remove.

bool $reset: Reset the cache or not.

Return value

mixed Success of a translated string.

1 call to MinifyJs::removeMinifiedFile()
MinifyJs::restore in src/MinifyJs.php
Remove the minified version of a single file (restore it).

File

src/MinifyJs.php, line 333

Class

MinifyJs
Minify JS Service.

Namespace

Drupal\minifyjs

Code

public function removeMinifiedFile($fid, $reset = FALSE) {

  // Get minified uri from the minifyjs_file table.
  $query = \Drupal::database()
    ->select('minifyjs_file', 'm')
    ->fields('m', [
    'minified_uri',
  ])
    ->condition('m.fid', $fid);

  // Make sure that it exists.
  if ($query
    ->countQuery()
    ->execute()
    ->fetchField() > 0) {
    $file = $query
      ->execute()
      ->fetchObject();

    // Get the fid of the minified table.
    $query = \Drupal::database()
      ->select('file_managed', 'f')
      ->fields('f', [
      'fid',
    ])
      ->condition('f.uri', $file->minified_uri);

    // Make sure that it exists.
    if ($query
      ->countQuery()
      ->execute()
      ->fetchField() > 0) {
      $file = $query
        ->execute()
        ->fetchObject();

      // Remove the file from the file_managed table.
      $file = File::load($file->fid);
      $file
        ->delete();

      // Set the file status to non-minfied.
      \Drupal::database()
        ->update('minifyjs_file')
        ->fields([
        'minified_uri' => '',
        'minified_size' => 0,
        'minified_modified' => 0,
      ])
        ->condition('fid', $fid)
        ->execute();

      // Clear the cache so this change will be reflected in
      // loadAllFiles().
      if ($reset) {
        \Drupal::cache()
          ->delete(MINIFYJS_CACHE_CID);
      }
      return TRUE;
    }
  }
  return t('File not found. Check that the file ID is correct.');
}