You are here

function youtube_thumb_delete_all in YouTube Field 8

Same name and namespace in other branches
  1. 7 youtube.inc \youtube_thumb_delete_all()

Deletes all existing thumbnail image files.

This is a submit callback for the "Refresh" option in the configuration form.

1 string reference to 'youtube_thumb_delete_all'
YoutubeSettingsForm::buildForm in src/Form/YoutubeSettingsForm.php
Form constructor.

File

./youtube.module, line 374
Youtube field module adds a field for YouTube videos.

Code

function youtube_thumb_delete_all($form, &$form_state) {
  $youtube_thumb_uri = youtube_build_thumbnail_uri();
  $file_system = \Drupal::service('file_system');
  if (!$file_system
    ->prepareDirectory($youtube_thumb_uri)) {
    return \Drupal::messenger()
      ->addMessage(t('No files deleted.'));
  }
  $files = $file_system
    ->scanDirectory($youtube_thumb_uri, '/^.*\\.(jpg|png)$/');
  foreach ($files as $raw_file) {
    $fid = \Drupal::database()
      ->select('file_managed')
      ->fields('file_managed', [
      'fid',
    ])
      ->condition('uri', '%' . $raw_file->uri, 'LIKE')
      ->execute()
      ->fetchField();
    if ($fid) {
      $managed_file = File::load($fid);
    }
    if (!$managed_file) {
      $file_system
        ->delete($raw_file->uri);
    }
    else {
      $managed_file
        ->delete();
    }
  }
  drupal_flush_all_caches();
}