You are here

function youtube_thumb_delete_all in YouTube Field 7

Same name and namespace in other branches
  1. 8 youtube.module \youtube_thumb_delete_all()

Submit callback; delete all existing thumbnail image files.

See also

youtube_settings_form()

1 string reference to 'youtube_thumb_delete_all'
youtube_settings_form in ./youtube.module
Settings form for the YouTube Field module's configuration page.

File

./youtube.inc, line 195
YouTube field helper functions.

Code

function youtube_thumb_delete_all($form, &$form_state) {
  $youtube_thumb_uri = youtube_build_thumbnail_uri();
  if (!file_prepare_directory($youtube_thumb_uri)) {
    return drupal_set_message(t('No files deleted.'));
  }
  $files = file_scan_directory($youtube_thumb_uri, '/^.*\\.(jpg|png)$/');
  $legacy_files = FALSE;
  foreach ($files as $raw_file) {

    // Check if file is being managed by Drupal.
    $uri = $youtube_thumb_uri . '/' . $raw_file->filename;
    $managed_file = db_select('file_managed', 'fm')
      ->fields('fm')
      ->condition('uri', '%' . $uri, 'LIKE')
      ->execute()
      ->fetchAssoc();
    if (!$managed_file) {

      // Old files exist before module used managed files, notify user.
      $legacy_files = file_unmanaged_delete($raw_file->uri);
    }
    else {
      $file = file_load($managed_file['fid']);

      // file_delete() invokes hooks to refresh associated image files.
      file_delete($file);
    }
  }
  if ($legacy_files) {
    drupal_set_message(t('Note: Some unmanaged files from an older version of the module were deleted. Their associated image style derivatives were not deleted. <a href="@imagestyleflush">Image style flush</a> or `drush image-flush` can be used for that. New thumbnails will be managed files and will not have this issue.', array(
      '@imagestyleflush' => 'https://www.drupal.org/project/imagestyleflush',
    )));
  }
}