Commit 858674f0 authored by alexpott's avatar alexpott

Issue #2060405 by Wim Leers: Fixed Impossible to uninstall any module using...

Issue #2060405 by Wim Leers: Fixed Impossible to uninstall any module using the file usage service, because it is impossible to delete all file usages by a module.
parent f64687a2
......@@ -38,6 +38,15 @@ function file_help($path, $arg) {
}
}
/**
* Implements hook_modules_uninstalled().
*/
function file_modules_uninstalled($modules) {
foreach ($modules as $module) {
file_usage()->deleteByModule($module);
}
}
/**
* Implements hook_menu().
*/
......
......@@ -46,7 +46,7 @@ public function __construct(Connection $connection, $table = 'file_usage') {
}
/**
* Implements Drupal\file\FileUsage\FileUsageInterface::add().
* {@inheritdoc}
*/
public function add(File $file, $module, $type, $id, $count = 1) {
$this->connection->merge($this->tableName)
......@@ -64,7 +64,7 @@ public function add(File $file, $module, $type, $id, $count = 1) {
}
/**
* Implements Drupal\file\FileUsage\FileUsageInterface::delete().
* {@inheritdoc}
*/
public function delete(File $file, $module, $type = NULL, $id = NULL, $count = 1) {
// Delete rows that have a exact or less value to prevent empty rows.
......@@ -99,7 +99,7 @@ public function delete(File $file, $module, $type = NULL, $id = NULL, $count = 1
}
/**
* Implements Drupal\file\FileUsage\FileUsageInterface::listUsage().
* {@inheritdoc}
*/
public function listUsage(File $file) {
$result = $this->connection->select($this->tableName, 'f')
......@@ -113,4 +113,14 @@ public function listUsage(File $file) {
}
return $references;
}
/**
* {@inheritdoc}
*/
public function deleteByModule($module) {
$this->connection->delete($this->tableName)
->condition('module', $module)
->execute();
}
}
......@@ -72,4 +72,13 @@ public function delete(File $file, $module, $type = NULL, $id = NULL, $count = 1
*
*/
public function listUsage(File $file);
/**
* Removes all records for a specific module; useful for uninstalling modules.
*
* @param string $module
* The name of the module using files.
*/
public function deleteByModule($module);
}
......@@ -52,6 +52,44 @@ function testGetUsage() {
$this->assertEqual($usage['testing']['bar'][2], 2, 'Returned the correct count.');
}
/**
* Tests file_usage()->deleteByModule().
*/
function testDeleteByModule() {
$file = $this->createFile();
db_insert('file_usage')
->fields(array(
'fid' => $file->id(),
'module' => 'testing',
'type' => 'foo',
'id' => 1,
'count' => 1
))
->execute();
db_insert('file_usage')
->fields(array(
'fid' => $file->id(),
'module' => 'testing',
'type' => 'bar',
'id' => 2,
'count' => 2
))
->execute();
db_insert('file_usage')
->fields(array(
'fid' => $file->id(),
'module' => 'file',
'type' => 'bar',
'id' => 2,
'count' => 2
))
->execute();
file_usage()->deleteByModule('testing');
$this->assertIdentical(array('file' => array('bar' => array(2 => '2'))), file_usage()->listUsage($file), 'All records for the "testing" module have been deleted.');
}
/**
* Tests file_usage()->add().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment