function media_browser_plus_taxonomy_term_delete in Media Browser Plus 7.3
Same name and namespace in other branches
- 7.2 media_browser_plus.module \media_browser_plus_taxonomy_term_delete()
Implements hook_taxonomy_term_delete().
See also
media_browser_plus_form_taxonomy_term_confirm_delete_submit()
File
- ./
media_browser_plus.module, line 664 - Media Browser Plus - enhanced file management functions.
Code
function media_browser_plus_taxonomy_term_delete($term) {
static $term_hierarchy_filter;
// Figure out if this is a folder term and if so handle the related files.
$vocabulary = taxonomy_vocabulary_machine_name_load('media_folders');
if (!empty($vocabulary) && $term->vid == $vocabulary->vid) {
// Skip if this term is already handled here by its parent term.
if (isset($term_hierarchy_filter[$term->tid])) {
unset($term_hierarchy_filter[$term->tid]);
return;
}
// Create an array of all the folders to handle.
$folders = array(
'0:' . $term->tid => $term,
);
// Fetch all sub-folders.
$tree = taxonomy_get_tree($term->vid, $term->tid);
foreach ($tree as $subterm) {
$folders[$subterm->depth + 1 . ':' . $subterm->tid] = $subterm;
$term_hierarchy_filter[$subterm->tid] = $subterm->tid;
}
// Ensure the order for processing is right.
krsort($folders);
$all_files_deleted = TRUE;
$used_files = array();
foreach ($folders as $folder) {
// Fetch all files from the folder.
$file_query = new EntityFieldQuery();
$files = $file_query
->entityCondition('entity_type', 'file')
->fieldCondition('field_folder', 'tid', $folder->tid)
->execute();
// If there are files, delete them if possible.
if (!empty($files['file'])) {
$files = file_load_multiple(array_keys($files['file']));
foreach ($files as $file) {
if (($file_usage = file_delete($file)) !== TRUE) {
$all_files_deleted = FALSE;
if (is_array($file_usage)) {
$used_files[$file->fid] = $file_usage;
}
}
}
}
// Also delete the folder when it's empty.
if ($all_files_deleted && ($dir = media_browser_plus_construct_dir_path($folder))) {
foreach (media_get_local_stream_wrappers() as $scheme => $scheme_info) {
$folder_path = file_stream_wrapper_uri_normalize($scheme . '://' . $dir);
if (!@drupal_rmdir($folder_path)) {
drupal_set_message(t('Unable to delete the folder (!path) on the disk', array(
'!path' => $folder_path,
)), 'error');
}
}
}
}
if (!$all_files_deleted) {
$list = array();
foreach ($used_files as $fid => $usage) {
$file = file_load($fid);
$list['items'][] = l($file->filename, 'file/' . $fid . '/usage');
}
drupal_set_message(t("Some of the files in the folder are used and can't be deleted:") . theme('item_list', $list), 'error');
}
// Clear view cache for media browser plus folders.
media_browser_plus_clear_views_cache('media_browser_plus_folders');
}
}