From 1a893da0fcafd8fd8aae7529e5d7e1f6a4a5b872 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Mon, 14 May 2018 13:09:46 +0200 Subject: [PATCH] Use core's filename transliteration --- composer.json | 3 + drupal-org-core.make | 1 + .../config/install/thunder_media.settings.yml | 1 - .../config/schema/thunder_media.schema.yml | 3 - .../src/Form/ConfigurationForm.php | 8 --- .../FilenameTransliterationTest.php | 67 ------------------- modules/thunder_media/thunder_media.module | 23 ------- .../thunder_media.post_update.php | 21 ++++++ 8 files changed, 25 insertions(+), 102 deletions(-) delete mode 100644 modules/thunder_media/tests/src/Functional/FilenameTransliterationTest.php create mode 100644 modules/thunder_media/thunder_media.post_update.php diff --git a/composer.json b/composer.json index bcf9046ce..a214e23d6 100644 --- a/composer.json +++ b/composer.json @@ -71,6 +71,9 @@ }, "drupal/redirect": { "Provided config does not match the config after an export": "https://www.drupal.org/files/issues/2932248-3.patch" + }, + "drupal/core": { + "Use new Transliteration functionality in core for file names": "https://www.drupal.org/files/issues/2018-05-14/2492171-93.patch" } } }, diff --git a/drupal-org-core.make b/drupal-org-core.make index 37968339b..0112324a6 100644 --- a/drupal-org-core.make +++ b/drupal-org-core.make @@ -3,3 +3,4 @@ core = 8.x projects[drupal][type] = core projects[drupal][download][url] = https://git.drupal.org/project/drupal.git projects[drupal][download][branch] = 8.6.x +projects[drupal][patch][] = https://www.drupal.org/files/issues/2018-05-14/2492171-93.patch diff --git a/modules/thunder_media/config/install/thunder_media.settings.yml b/modules/thunder_media/config/install/thunder_media.settings.yml index 0979f7064..7208be34e 100644 --- a/modules/thunder_media/config/install/thunder_media.settings.yml +++ b/modules/thunder_media/config/install/thunder_media.settings.yml @@ -1,2 +1 @@ -enable_filename_transliteration: false enable_filefield_remove_button: false diff --git a/modules/thunder_media/config/schema/thunder_media.schema.yml b/modules/thunder_media/config/schema/thunder_media.schema.yml index 69fa4874b..7bcfa4ffb 100644 --- a/modules/thunder_media/config/schema/thunder_media.schema.yml +++ b/modules/thunder_media/config/schema/thunder_media.schema.yml @@ -1,9 +1,6 @@ thunder_media.settings: type: config_object mapping: - enable_filename_transliteration: - type: boolean - label: 'Default transliteration' enable_filefield_remove_button: type: boolean label: 'Enable remove button' diff --git a/modules/thunder_media/src/Form/ConfigurationForm.php b/modules/thunder_media/src/Form/ConfigurationForm.php index 17602712d..5d7495bf5 100644 --- a/modules/thunder_media/src/Form/ConfigurationForm.php +++ b/modules/thunder_media/src/Form/ConfigurationForm.php @@ -34,13 +34,6 @@ public function getFormId() { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('thunder_media.settings'); - $form['enable_filename_transliteration'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Enable filename transliteration'), - '#description' => $this->t('Enable this checkbox to clean filenames before saving the files.'), - '#default_value' => $config->get('enable_filename_transliteration'), - ]; - $form['enable_filefield_remove_button'] = [ '#type' => 'checkbox', '#title' => $this->t('Enable file field remove button'), @@ -58,7 +51,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $this->config('thunder_media.settings') - ->set('enable_filename_transliteration', $form_state->getValue('enable_filename_transliteration')) ->set('enable_filefield_remove_button', $form_state->getValue('enable_filefield_remove_button')) ->save(); } diff --git a/modules/thunder_media/tests/src/Functional/FilenameTransliterationTest.php b/modules/thunder_media/tests/src/Functional/FilenameTransliterationTest.php deleted file mode 100644 index 1a9ab03ba..000000000 --- a/modules/thunder_media/tests/src/Functional/FilenameTransliterationTest.php +++ /dev/null @@ -1,67 +0,0 @@ -config('thunder_media.settings') - ->set('enable_filename_transliteration', TRUE) - ->save(); - } - - /** - * Test for transliteration of file name. - */ - public function testFileTransliteration() { - - $account = $this->drupalCreateUser(['access site reports']); - $this->drupalLogin($account); - - $original = drupal_get_path('module', 'simpletest') . '/files'; - - file_unmanaged_copy($original . '/image-1.png', PublicStream::basePath() . '/foo°.png'); - - // Upload with replace to guarantee there's something there. - $edit = [ - 'file_test_replace' => FILE_EXISTS_RENAME, - 'files[file_test_upload]' => drupal_realpath('public://foo°.png'), - ]; - $this->drupalPostForm('file-test/upload', $edit, t('Submit')); - $this->assertSession()->statusCodeEquals(200); - $this->assertSession()->responseContains('You WIN!'); - - $this->assertTrue(file_exists('temporary://foodeg.png')); - - $max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField(); - - $file = File::load($max_fid_after); - - $this->assertSame('foodeg.png', $file->getFilename()); - $this->assertSame('temporary://foodeg.png', $file->getFileUri()); - - } - -} diff --git a/modules/thunder_media/thunder_media.module b/modules/thunder_media/thunder_media.module index 2d6cec607..fb5436338 100644 --- a/modules/thunder_media/thunder_media.module +++ b/modules/thunder_media/thunder_media.module @@ -5,31 +5,8 @@ * Contains media related functions. */ -use Drupal\file\Entity\File; use Drupal\Core\Form\FormStateInterface; -/** - * Implements hook_ENTITY_TYPE_create(). - */ -function thunder_media_file_create(File $entity) { - - $config = \Drupal::config('thunder_media.settings'); - - if ($config->get('enable_filename_transliteration')) { - - $pathinfo = pathinfo($entity->getFilename()); - - /** @var \Drupal\pathauto\AliasCleaner $aliasCleaner */ - $aliasCleaner = \Drupal::service('pathauto.alias_cleaner'); - - // Check for needed pathinfo array keys. - if (!empty($pathinfo['filename']) && !empty($pathinfo['extension'])) { - $cleanFilename = $aliasCleaner->cleanString($pathinfo['filename']) . '.' . $pathinfo['extension']; - $entity->setFilename($cleanFilename); - } - } -} - /** * Implements hook_preprocess_views_view(). */ diff --git a/modules/thunder_media/thunder_media.post_update.php b/modules/thunder_media/thunder_media.post_update.php new file mode 100644 index 000000000..5eba30518 --- /dev/null +++ b/modules/thunder_media/thunder_media.post_update.php @@ -0,0 +1,21 @@ +set('filename_transliteration', \Drupal::config('thunder_media.settings')->get('enable_filename_transliteration')); + \Drupal::config('thunder_media.settings')->clear('enable_filename_transliteration'); + + // Output logged messages to related channel of update execution. + return $thunderUpdater->logger()->output(); +}