Skip to content

Commit

Permalink
Merge pull request #12 from xurizaemon/drupal_check
Browse files Browse the repository at this point in the history
Replace deprecated functions.
  • Loading branch information
leymannx authored Oct 25, 2019
2 parents d6b9d23 + 3dfbdf9 commit 19e4503
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ maybe you can start here and save a lot of time.

## See also

You should definitely check out the excellent Drupal Migrate module and the toolkit & features it offers. They may fit your needs better. If you want a lightweight start point, this module may be useful and we hope you find it so!
You should definitely check out the excellent Drupal Migrate module and the
toolkit & features it offers. They may fit your needs better. If you want a
lightweight start point, this module may be useful and we hope you find it so!

## License

Expand Down
8 changes: 6 additions & 2 deletions src/Batch/CsvImportBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Node can be used later to actually create nodes. See commented code block
// in csvimportImportLine() below. Since it's unused right now, we hide it from
// coding standards linting.
use Drupal\Core\File\FileSystemInterface;
use Drupal\node\Entity\Node;

// @codingStandardsIgnoreEnd
Expand All @@ -29,7 +30,8 @@ public static function csvimportImportFinished($success, $results, $operations)
if (!empty($results['failed_rows'])) {

$dir = 'public://csvimport';
if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
if (\Drupal::service('file_system')
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY)) {

// We validated extension on upload.
$csv_filename = 'failed_rows-' . basename($results['uploaded_filename']);
Expand Down Expand Up @@ -93,8 +95,10 @@ public static function csvimportImportLine($line, &$context) {

// In order to slow importing and debug better, we can uncomment
// this line to make each import slightly slower.
// usleep(2500);
// @codingStandardsIgnoreStart
//usleep(2500);

// @codingStandardsIgnoreEnd
// Convert the line of the CSV file into a new node.
// @codingStandardsIgnoreStart
//if ($context['results']['rows_imported'] > 1) { // Skip header line.
Expand Down
11 changes: 8 additions & 3 deletions src/Form/CSVimportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\csvimport\Form;

use Drupal\Component\Utility\Environment;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

Expand Down Expand Up @@ -32,7 +34,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['csvfile'] = [
'#title' => $this->t('CSV File'),
'#type' => 'file',
'#description' => ($max_size = file_upload_max_size()) ? $this->t('Due to server restrictions, the <strong>maximum upload file size is @max_size</strong>. Files that exceed this size will be disregarded.', ['@max_size' => format_size($max_size)]) : '',
'#description' => ($max_size = Environment::getUploadMaxSize()) ? $this->t('Due to server restrictions, the <strong>maximum upload file size is @max_size</strong>. Files that exceed this size will be disregarded.', ['@max_size' => format_size($max_size)]) : '',
'#element_validate' => ['::validateFileupload'],
];

Expand All @@ -53,17 +55,20 @@ public static function validateFileupload(&$element, FormStateInterface $form_st
'file_validate_extensions' => ['csv CSV'],
];

// @TODO: File_save_upload will probably be deprecated soon as well.
// @see https://www.drupal.org/node/2244513.
if ($file = file_save_upload('csvfile', $validators, FALSE, 0, FILE_EXISTS_REPLACE)) {

// The file was saved using file_save_upload() and was added to the
// files table as a temporary file. We'll make a copy and let the
// garbage collector delete the original upload.
$csv_dir = 'temporary://csvfile';
$directory_exists = file_prepare_directory($csv_dir, FILE_CREATE_DIRECTORY);
$directory_exists = \Drupal::service('file_system')
->prepareDirectory($csv_dir, FileSystemInterface::CREATE_DIRECTORY);

if ($directory_exists) {
$destination = $csv_dir . '/' . $file->getFilename();
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
if (file_copy($file, $destination, FileSystemInterface::EXISTS_REPLACE)) {
$form_state->setValue('csvupload', $destination);
}
else {
Expand Down

0 comments on commit 19e4503

Please sign in to comment.