Skip to content
Snippets Groups Projects
Commit 0ff5e6d2 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2068343 by slashrsm: Convert file SQL queries to the Entity Query API.

parent 3c1b5bc2
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -8,7 +8,12 @@
namespace Drupal\file\Plugin\views\argument;
use Drupal\Component\Annotation\PluginID;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\views\Plugin\views\argument\Numeric;
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Argument handler to accept multiple file ids.
......@@ -17,19 +22,67 @@
*
* @PluginID("file_fid")
*/
class Fid extends Numeric {
class Fid extends Numeric implements ContainerFactoryPluginInterface {
/**
* The entity manager service
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* The entity query factory service.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $entityQuery;
/**
* Constructs a Drupal\file\Plugin\views\argument\Fid object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\Core\Entity\Query\QueryFactory
* The entity query factory.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManager $entity_manager, QueryFactory $entity_query) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->entityQuery = $entity_query;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.entity'),
$container->get('entity.query')
);
}
/**
* Override the behavior of titleQuery(). Get the filenames.
*/
public function titleQuery() {
$titles = db_select('file_managed', 'f')
->fields('f', array('filename'))
$fids = $this->entityQuery->get('file')
->condition('fid', $this->value)
->execute()
->fetchCol();
foreach ($titles as &$title) {
$title = check_plain($title);
->execute();
$controller = $this->entityManager->getStorageController('file');
$files = $controller->loadMultiple($fids);
$titles = array();
foreach ($files as $file) {
$titles[] = String::checkPlain($file->getFilename());
}
return $titles;
}
......
......@@ -111,5 +111,24 @@ function testFileListingPages() {
$result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", array(':value' => t('Temporary')));
$this->assertEqual(1, count($result), 'Unused file marked as temporary.');
// Test file usage page.
foreach ($nodes as $node) {
$file = entity_load('file', $node->file->target_id);
$usage = file_usage()->listUsage($file);
$this->drupalGet('admin/content/files/usage/' . $file->id());
$this->assertResponse(200);
$this->assertText($node->getTitle(), 'Node title found on usage page.');
$this->assertText('node', 'Registering entity type found on usage page.');
$this->assertText('file', 'Registering module found on usage page.');
foreach ($usage as $module) {
foreach ($module as $entity_type) {
foreach ($entity_type as $entity) {
$this->assertText($entity, 'Usage count found on usage page.');
}
}
}
$this->assertLinkByHref('node/' . $node->id(), 0, 'Link to registering entity found on usage page.');
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment