Skip to content
Snippets Groups Projects

Issue #3458479: Require at least Drupal 10.3 and remove any usage of deprecated classes, methods, constants, or functions

Merged Issue #3458479: Require at least Drupal 10.3 and remove any usage of deprecated classes, methods, constants, or functions
1 file
+ 16
17
Compare changes
  • Side-by-side
  • Inline
@@ -2,12 +2,13 @@
namespace Drupal\file_example;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\Event\FileUploadSanitizeNameEvent;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\devel\DevelDumperInterface;
use Drupal\file\FileRepositoryInterface;
use Drupal\file_example\Traits\DumperTrait;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -36,6 +37,8 @@ class FileExampleSubmitHandlerHelper {
* The file system.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The event dispatcher.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*
* @see https://php.watch/versions/8.0/constructor-property-promotion
*/
@@ -46,7 +49,8 @@ class FileExampleSubmitHandlerHelper {
protected MessengerInterface $messenger,
protected FileRepositoryInterface $fileRepository,
protected FileSystemInterface $fileSystem,
protected EventDispatcherInterface $eventDispatcher
protected EventDispatcherInterface $eventDispatcher,
protected ModuleHandlerInterface $moduleHandler,
) {
}
@@ -59,9 +63,9 @@ class FileExampleSubmitHandlerHelper {
* The key functions used here are:
* - file_save_data(), which takes a buffer and saves it to a named file and
* also creates a tracking record in the database and returns a file object.
* In this function we use FileSystemInterface::EXISTS_RENAME (the default)
* as the argument, which means that if there's an existing file, create a
* new non-colliding filename and use it.
* In this function we use FileExists::Rename (the default) as the argument,
* which means that if there's an existing file, create a new non-colliding
* filename and use it.
* - file_create_url(), which converts a URI in the form public://junk.txt or
* private://something/test.txt into a URL like
* http://example.com/sites/default/files/junk.txt.
@@ -79,7 +83,7 @@ class FileExampleSubmitHandlerHelper {
$uri = !empty($form_values['destination']) ? $form_values['destination'] : NULL;
// Managed operations work with a file object.
$file_object = $this->fileRepository->writeData($data, $uri, FileSystemInterface::EXISTS_RENAME);
$file_object = $this->fileRepository->writeData($data, $uri, FileExists::Rename);
if (!empty($file_object)) {
$url = $this->fileHelper->getExternalUrl($file_object);
$this->stateHelper->setDefaultFile($file_object->getFileUri());
@@ -119,9 +123,9 @@ class FileExampleSubmitHandlerHelper {
* The key functions used here are:
* - FileSystemInterface::saveData(), which takes a buffer and saves it to a
* named file, but does not create any kind of tracking record in the
* database. This example uses FileSystemInterface::EXISTS_REPLACE for the
* third argument, meaning that if there's an existing file at this
* location, it should be replaced.
* database. This example uses FileExists::Replace for the third argument,
* meaning that, if there's an existing file at this location, it should be
* replaced.
* - file_create_url(), which converts a URI in the form public://junk.txt or
* private://something/test.txt into a URL like
* http://example.com/sites/default/files/junk.txt.
@@ -139,7 +143,7 @@ class FileExampleSubmitHandlerHelper {
$destination = !empty($form_values['destination']) ? $form_values['destination'] : NULL;
// With the unmanaged file we just get a filename back.
$filename = $this->fileSystem->saveData($data, $destination, FileSystemInterface::EXISTS_REPLACE);
$filename = $this->fileSystem->saveData($data, $destination, FileExists::Replace);
if ($filename) {
$url = $this->fileHelper->getExternalUrl($filename);
$this->stateHelper->setDefaultFile($filename);
@@ -456,7 +460,7 @@ class FileExampleSubmitHandlerHelper {
*/
public function handleShowSession(array &$form, FormStateInterface $form_state) {
$dumper = $this->dumper();
if ($dumper instanceof DevelDumperInterface) {
if ($this->isDevelDumper($dumper)) {
// If the devel module is installed, use its nicer message format.
$dumper->dump($this->sessionHelperWrapper->getStoredData(), $this->t('Entire $_SESSION["file_example"]'));
}
@@ -483,4 +487,22 @@ class FileExampleSubmitHandlerHelper {
$this->messenger->addMessage('Session reset.');
}
/**
* Checks if the given object is an instance of DevelDumperInterface.
*
* @param object $object
* The object to check.
*
* @return bool
* Returns TRUE if the object is an instance of DevelDumperInterface,
* FALSE otherwise.
*/
protected function isDevelDumper(object $object): bool {
if ($this->moduleHandler->moduleExists('devel')) {
return in_array('DevelDumperInterface', class_implements($object));
}
return FALSE;
}
}
Loading