FancyFileDeleteOrphanFileFilter.php in Fancy File Delete 2.0.x
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;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Database\Connection;
class FancyFileDeleteOrphanFileFilter extends FilterPluginBase implements ContainerFactoryPluginInterface {
protected $database;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->database = $database;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'));
}
protected function valueForm(&$form, FormStateInterface $form_state) {
$form['expose_button']['#access'] = FALSE;
$form['more']['#access'] = FALSE;
$form['orphan_text'] = [
'#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 = $this->database
->query($query)
->fetchAll();
if (isset($results) && 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, '=');
}
}
}