Skip to content
Snippets Groups Projects
Commit 6de39d2c authored by Yas Naoi's avatar Yas Naoi
Browse files

Issue #3245859 by yas, baldwinlouie: Change deprecated function file_save_data...

Issue #3245859 by yas, baldwinlouie: Change deprecated function file_save_data to \Drupal::service('file.repository')->writeData()
parent fc65627d
No related branches found
No related tags found
No related merge requests found
services:
cloud:
class: Drupal\cloud\Service\CloudService
arguments: ['@entity_type.manager', '@entity.definition_update_manager', '@config.manager', '@config.factory', '@config.typed', '@file_system', '@extension.path.resolver', '@module_handler', '@request_stack', '@http_client', '@plugin.manager.cloud_config_plugin', '@twig']
arguments: ['@entity_type.manager', '@entity.definition_update_manager', '@config.manager', '@config.factory', '@config.typed', '@file_system', '@extension.path.resolver', '@module_handler', '@request_stack', '@http_client', '@plugin.manager.cloud_config_plugin', '@twig', '@file.repository']
cloud.subscriber:
class: Drupal\cloud\EventSubscriber\CloudSubscriber
......
......@@ -15,6 +15,7 @@ use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\FileRepositoryInterface;
use Drupal\geocoder\Entity\GeocoderProvider;
use Drupal\geocoder\ProviderPluginManager;
use GuzzleHttp\ClientInterface;
......@@ -120,6 +121,13 @@ class CloudService extends CloudServiceBase implements CloudServiceInterface {
*/
protected $twig;
/**
* The file repository service.
*
* @var \Drupal\file\FileRepositoryInterface
*/
protected $fileRepository;
/**
* The geocoder provider plugin manager.
*
......@@ -154,7 +162,9 @@ class CloudService extends CloudServiceBase implements CloudServiceInterface {
* The cloud service provider plugin manager (CloudConfigPluginManager).
* @param \Drupal\Core\Template\TwigEnvironment $twig
* The Twig handler.
* @param \Drupal\geocoder\ProviderPluginManager $geocoder_provider_plugin_manager
* @param \Drupal\file\FileRepositoryInterface|null $file_repository
* The file repository service.
* @param \Drupal\geocoder\ProviderPluginManager|null $geocoder_provider_plugin_manager
* The geocoder provider plugin manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager,
......@@ -169,6 +179,7 @@ class CloudService extends CloudServiceBase implements CloudServiceInterface {
ClientInterface $http_client,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
TwigEnvironment $twig,
FileRepositoryInterface $file_repository = NULL,
ProviderPluginManager $geocoder_provider_plugin_manager = NULL) {
// The parent constructor takes care of $this->messenger object.
......@@ -186,6 +197,7 @@ class CloudService extends CloudServiceBase implements CloudServiceInterface {
// Setup the configuration factory.
$this->configFactory = $config_factory;
// Setup the typed config manager.
$this->typedConfigManager = $typed_config;
// Setup the file system.
......@@ -209,6 +221,11 @@ class CloudService extends CloudServiceBase implements CloudServiceInterface {
// Setup the twig handler.
$this->twig = $twig;
// Setup the file repository service.
$this->fileRepository = !empty($file_repository)
? $file_repository
: \Drupal::service('file.repository');
// Setup the geocoder provider plugin manager.
$this->geocoderProviderPluginManager = $geocoder_provider_plugin_manager;
}
......@@ -230,6 +247,7 @@ class CloudService extends CloudServiceBase implements CloudServiceInterface {
$container->get('http_client'),
$container->get('plugin.manager.cloud_config_plugin'),
$container->get('twig'),
$container->has('file.repository') ? $container->get('file.repository') : NULL,
$container->has('plugin.manager.geocoder.provider') ? $container->get('plugin.manager.geocoder.provider') : NULL
);
}
......@@ -496,10 +514,15 @@ class CloudService extends CloudServiceBase implements CloudServiceInterface {
$icon_content = fread($handle, filesize($icon));
fclose($handle);
$destination = 'public://images/cloud/icons';
$destination = $this->configFactory->get('system.file')->get('default_scheme') . '://images/cloud/icons';
$this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY);
$file = file_save_data($icon_content, "public://images/cloud/icons/${module}.png");
// @todo The optional parameter was FileSystemInterface::EXISTS_RENAME.
// It is default option, which appends _{incrementing number} until the
// filename is unique. Now we use FileSystemInterface::EXISTS_REPLACE
// however consider to revert to FileSystemInterface::EXISTS_RENAME if
// it doesn't work.
$file = $this->fileRepository->writeData($icon_content, "${destination}/${module}.png", FileSystemInterface::EXISTS_REPLACE);
if (!empty($file)) {
$file->setPermanent();
$file->save();
......
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