0

Consider this pattern :

[content type]-[entity ID].[file:ffp-extension-original]

is there any hook so I can change the uploaded file name based on content type and node ID in drupal 8?

PS:

I used File(field) paths module but I couldn't find proper tokens to get it work right

2 Answers 2

1

You might be able to do this with hook_ENTITY_TYPE_update(). Take a look at the D8 version of the upload_replace module code for an example of changing filenames.

1
  • Thanks @CG Monroe , but as I needed info about the node containing the file to get the final path, this solution didn't provides all my needs, hence it helped to get an overall view, thanks, voted up Mar 2, 2018 at 22:58
1

As I needed to know some data about the node which the file is added to, none of hook_file_update, hook_file_presave or hooks like that was proper for my needs, while I was searching I found a drupal 7 solution, so I changed the code to get it to work.

Here is the module I wrote, this module works on any file and image field :

<?php
/**
 *@file
 * this module alters node forms and adds a submit handler which inside that it renames the uploaded files
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity;
use Drupal\Core\Entity\ContentEntityForm;

function file_name_converter_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $bundle = FALSE;
  $form_object = $form_state->getFormObject();
  if ($form_object instanceof ContentEntityForm) {
    $bundle = $form_object->getEntity()->bundle();
    $form_state->set('#bundle_value', $bundle);
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'file_name_converter_node_form_submit';
      }
    }
  }

}

function file_name_converter_node_form_submit(&$form, FormStateInterface $form_state) {

  $values = $form_state->getValues();
  $bundle = $form_state->get('#bundle_value');

  $bundle_fields = \Drupal::entityManager()->getFieldDefinitions('node', $bundle);

  foreach ($bundle_fields as $field_name => $field_definition) {
    if (!empty($field_definition->getTargetBundle())) {
      $field_type = $field_definition->getType();
      if($field_type == 'image' || $field_type == 'file') {
        if(!empty($values[$field_name])) {
          foreach ($values[$field_name][0]['fids'] as $delta => $fid) {
            $file = file_load($fid);

            $uri = $file->getFileUri();
            $fname = $file->getFilename();
            $exp = explode('.', $fname);
            $extnsn = $exp[count($exp)-1];

            $new_name = 'MySite-' . $field_type . '-' . $bundle . '-' . $values['nid'] . '.' . $extnsn;

            $new_uri = str_replace($fname, $new_name, $uri);

            // And finally rename the file.
            file_move($file, $new_uri);
          }
        }
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.