Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wri 347 filename update #359

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/wri_admin/config/install/file.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file exists already once this comes in as a module. Can you try moving it to the config/install folder directly in the root? I think that will fix the error circle is throwing.

If it works, you can update the path in the update hook too (but you can leave it in the wri_admin module)

type: textfield
length: 128
icon:
directory: core/modules/file/icons
make_unused_managed_files_temporary: false
filename_sanitization:
transliterate: true
replace_whitespace: true
replace_non_alphanumeric: true
deduplicate_separators: true
lowercase: true
replacement_character: '-'
7 changes: 7 additions & 0 deletions modules/wri_admin/wri_admin.install
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ function wri_admin_update_10302() {
}
\Drupal::service('distro_helper.updates')->installConfig('dblog_persistent.channel.solr_schema_problems', 'wri_admin', 'install');
}

/**
* Update the file sytem settings.
*/
function wri_admin_update_10400() {
\Drupal::service('distro_helper.updates')->updateConfig('file.settings', ['filename_sanitization'], 'wri_admin');
}
119 changes: 118 additions & 1 deletion modules/wri_admin/wri_admin.module
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
* Implements hook_page_attachments().
*/

use Drupal\media\Entity\Media;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;

/**
Expand Down Expand Up @@ -75,7 +79,9 @@ function wri_admin_form_alter(&$form, FormStateInterface $form_state, $form_id)
];
}
}

if (strpos($form_id, 'media_') === 0) {
$form['#validate'][] = 'wri_admin_validate_media_name';
}
}

/**
Expand All @@ -99,3 +105,114 @@ function wri_admin_theme_suggestions_entity_moderation_form(array $variables) {

return $suggestions;
}

/**
* Validation function to ensure the media name is not just numbers.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double comment here!

*/

/**
* Validation function to ensure the media name is not just numbers.
*/
function wri_admin_validate_media_name(array &$form, FormStateInterface $form_state) {
\Drupal::logger('wri_admin')->notice('Validation triggered for media name.');

// Log available form state keys (safe debugging).
$form_state_keys = array_keys($form_state->getValues());
\Drupal::logger('wri_admin')->notice('Form state keys: @keys', ['@keys' => implode(', ', $form_state_keys)]);

// Check if the 'name' field exists in the form state.
$name_values = $form_state->getValue('name');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like if you upload the file from a node page, the value is located within "Media" -- Try uploading a file named '1.jpg' as the hero image on an article.

if (!isset($name_values[0]['value']) || empty($name_values[0]['value'])) {
\Drupal::logger('wri_admin')->warning('Media name field is missing or empty.');
return;
}

// Extract and clean up the media name.
$name = trim($name_values[0]['value']);
$name_without_extension = pathinfo($name, PATHINFO_FILENAME);
\Drupal::logger('wri_admin')->notice('Extracted name: @name', ['@name' => $name_without_extension]);

// Ensure the name contains at least one letter or dash.
if (!preg_match('/[a-zA-Z-]/', $name_without_extension)) {
\Drupal::logger('wri_admin')->warning('Invalid media name detected: @name', ['@name' => $name_without_extension]);

// Ensure the form contains the 'name' field before setting an error.
if (isset($form['name'])) {
$form_state->setErrorByName('name', t('The media name must contain at least one letter or dash (-). Only numbers and underscores (_) are not allowed.'));
}
else {
\Drupal::logger('wri_admin')->error('Failed to set error: "name" field missing from form structure.');
}
}
}

/**
* Implements hook_entity_presave().
*/
function wri_admin_entity_presave(EntityInterface $entity) {
if ($entity instanceof Media && $entity->hasField('field_media_image')) {
$file_id = $entity->get('field_media_image')->target_id;
$file = File::load($file_id);

if ($file) {
$original_uri = $file->getFileUri();
$pathinfo = pathinfo($original_uri);

// Transliterate the new filename based on the media Name field.
$transliteration = \Drupal::service('transliteration');
$media_name = $entity->getName();

// Transliterate and replace special characters.
$transliterated_name = $transliteration->transliterate($media_name, 'en');

// Convert to lowercase and replace spaces/special characters with dashes.
$sanitized_name = strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', trim($transliterated_name)));

// Ensure the filename doesn't end with a dash.
$sanitized_name = rtrim($sanitized_name, '-');

$new_filename = $sanitized_name . '.' . $pathinfo['extension'];

// Ensure the new filename does not exist yet.
$file_system = \Drupal::service('file_system');
$new_uri = $pathinfo['dirname'] . '/' . $new_filename;
$new_uri = $file_system->getDestinationFilename($new_uri, FileSystemInterface::EXISTS_RENAME);

\Drupal::logger('wri_admin')->notice('Presave Hook Triggered:
Media ID: @id,
Original URI: @orig_uri,
Proposed New Filename: @new_filename',
[
'@id' => $entity->id(),
'@orig_uri' => $original_uri,
'@new_filename' => $new_filename,
]
);

try {
// Copy the file to the new location.
$copy_result = $file_system->copy($original_uri, $new_uri, FileSystemInterface::EXISTS_RENAME);
if (!$copy_result) {
throw new \Exception("File copy failed for: $original_uri to $new_uri");
}

// Update the file entity with the new URI.
$file->setFileUri($new_uri);
$file->setFilename($new_filename);
$file->save();

// Delete the old file from S3.
$file_system->delete($original_uri);

\Drupal::logger('wri_admin')->notice('File successfully renamed to: @new_filename', ['@new_filename' => $new_filename]);

}
catch (\Exception $e) {
\Drupal::logger('wri_admin')->error('File rename error: @error', ['@error' => $e->getMessage()]);
}
}
else {
\Drupal::logger('wri_admin')->warning('Presave Hook: File not found for Media ID: @id', ['@id' => $entity->id()]);
}
}
}