Skip to content

Commit 2376caf

Browse files
Mile23paul-m
Mile23
authored andcommitted
Issue #2986438 by Mile23: @todos in FileExampleReadWriteForm
1 parent 2f0f176 commit 2376caf

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

file_example/src/Form/FileExampleReadWriteForm.php

+28-12
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Drupal\file_example\Form;
44

5-
use Drupal\Core\Database\Database;
5+
use Drupal\Core\Entity\EntityTypeManagerInterface;
66
use Drupal\Core\Extension\ModuleHandlerInterface;
77
use Drupal\Core\File\FileSystemInterface;
88
use Drupal\Core\Form\FormBase;
99
use Drupal\Core\Form\FormStateInterface;
1010
use Drupal\Core\State\StateInterface;
1111
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
1212
use Drupal\Core\Url;
13-
use Drupal\file\Entity\File;
1413
use Drupal\file\FileInterface;
1514
use Drupal\stream_wrapper_example\StreamWrapper\SessionWrapper;
1615
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -55,6 +54,10 @@ class FileExampleReadWriteForm extends FormBase {
5554
* Indicator variable for the session:// scheme being available.
5655
*
5756
* @var bool
57+
*
58+
* @todo Remove this since we have a hard dependency on
59+
* stream_wrapper_example.
60+
* https://www.drupal.org/project/examples/issues/2990336
5861
*/
5962
protected $sessionSchemeEnabled;
6063

@@ -65,6 +68,13 @@ class FileExampleReadWriteForm extends FormBase {
6568
*/
6669
protected $moduleHandler;
6770

71+
/**
72+
* Entity type manager service.
73+
*
74+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
75+
*/
76+
protected $entityTypeManager;
77+
6878
/**
6979
* Constructs a new FileExampleReadWriteForm page.
7080
*
@@ -86,13 +96,15 @@ public function __construct(
8696
FileSystemInterface $file_system,
8797
StreamWrapperManagerInterface $stream_wrapper_manager,
8898
ModuleHandlerInterface $module_handler,
89-
RequestStack $request_stack
99+
RequestStack $request_stack,
100+
EntityTypeManagerInterface $entity_type_manager
90101
) {
91102
$this->state = $state;
92103
$this->fileSystem = $file_system;
104+
$this->streamWrapperManager = $stream_wrapper_manager;
93105
$this->moduleHandler = $module_handler;
94106
$this->requestStack = $request_stack;
95-
$this->streamWrapperManager = $stream_wrapper_manager;
107+
$this->entityTypeManager = $entity_type_manager;
96108
$this->sessionSchemeEnabled = $this->moduleHandler->moduleExists('stream_wrapper_example');
97109
}
98110

@@ -105,7 +117,8 @@ public static function create(ContainerInterface $container) {
105117
$container->get('file_system'),
106118
$container->get('stream_wrapper_manager'),
107119
$container->get('module_handler'),
108-
$container->get('request_stack')
120+
$container->get('request_stack'),
121+
$container->get('entity_type.manager')
109122
);
110123
$form->setMessenger($container->get('messenger'));
111124
return $form;
@@ -204,15 +217,18 @@ protected function setDefaultDirectory($uri) {
204217
* May be other alternatives.
205218
* https://www.drupal.org/project/examples/issues/2986438
206219
*/
207-
private static function getManagedFile($uri) {
208-
$fid = Database::getConnection('default')->query(
209-
'SELECT fid FROM {file_managed} WHERE uri = :uri',
210-
[':uri' => $uri]
211-
)->fetchField();
220+
protected function getManagedFile($uri) {
221+
// We'll use an entity query to get the managed part of the file.
222+
$storage = $this->entityTypeManager->getStorage('file');
223+
$query = $storage->getQuery()
224+
->condition('uri', $uri);
225+
$fid = $query->execute();
212226
if (!empty($fid)) {
213-
$file_object = File::load($fid);
227+
// Now that we have a fid, we can load it.
228+
$file_object = $storage->load(reset($fid));
214229
return $file_object;
215230
}
231+
// Return FALSE because there's no managed file for that URI.
216232
return FALSE;
217233
}
218234

@@ -674,7 +690,7 @@ public function handleFileDelete(array &$form, FormStateInterface $form_state) {
674690
// Since we don't know if the file is managed or not, look in the database
675691
// to see. Normally, code would be working with either managed or unmanaged
676692
// files, so this is not a typical situation.
677-
$file_object = self::getManagedFile($uri);
693+
$file_object = $this->getManagedFile($uri);
678694

679695
// If a managed file, use file_delete().
680696
if (!empty($file_object)) {

0 commit comments

Comments
 (0)