FancyFileDeleteOrphanFileFilter.php in Fancy File Delete 8
File
src/Plugin/views/filter/FancyFileDeleteOrphanFileFilter.php
View source
<?php
namespace Drupal\fancy_file_delete\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
class FancyFileDeleteOrphanFileFilter extends FilterPluginBase {
protected function valueForm(&$form, FormStateInterface $form_state) {
$form['expose_button']['#access'] = FALSE;
$form['more']['#access'] = FALSE;
$form['orphan_text'] = array(
'#type' => 'item',
'#markup' => $this
->t('This is just a custom query filter no need for any configuration.'),
);
}
public function query() {
$table = $this
->ensureMyTable();
$query = "SELECT fm.* FROM file_managed AS fm LEFT OUTER JOIN file_usage\n AS fu ON (fm.fid = fu.fid) LEFT OUTER JOIN node AS n ON (fu.id = n.nid)\n WHERE fu.type = 'node' AND n.nid IS NULL";
$results = \Drupal::database()
->query($query)
->fetchAll();
if (count($results) > 0) {
foreach ($results as $result) {
$files[] = $result->fid;
}
$this->query
->addWhere($this->options['group'], $table . '.fid', $files, 'IN');
}
else {
$this->query
->addWhere($this->options['group'], $table . '.fid', NULL, '=');
}
}
}