0

I have a custom Content Entity created with

public static function baseFieldDefinitions(EntityTypeInterface $entity_type)

In the definition, there is a 'file' field:

$fields['file1'] = BaseFieldDefinition::create('file')
->setLabel(t('Fichier source'))
->setDescription(t('Source document (can be edited)'))
->setSettings([
'target_type' => 'file',
'file_extensions' => 'doc docx odt pages',
'file_directory' => 'contrats',
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => 7,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 7,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setReadOnly(TRUE);

And it works fine!
My question is: how can I change the name of an uploaded file?
I guess I should use hook_entity_presave.
But how to retrieve file information in hook_entity_presave?
Using the comments already given (thanks), I added this:

function tempo_entity_presave(EntityInterface $entity)
{
if ($entity->getEntityType()->id() == "amap_contrats") {
$sName  = 'Contrat-'.$entity->typecontrat->target_id.'-'.str_replace("-", "", $entity->datedebutcontrat->value);
$fid1[0]['target_id'];
$fid1 = $entity->get('file1')->target_id;
$file = file_load($fid1);
$sName1 = $sName.'.'.pathinfo($file->filename->value, PATHINFO_EXTENSION);
$result = file_move($file1, $sName1, FILE_EXISTS_REPLACE);
$sName2 = $sName . '.pdf';
$result =file_move($file2, $sName2, FILE_EXISTS_REPLACE);
}
}

And now, I get this error:

Argument 1 passed to file_move() must implement interface Drupal\file\FileInterface, null given, called in /Applications/MAMP/htdocs/JdP8M3/modules/custom/tempo/tempo.module on line 48 in /Applications/MAMP/htdocs/JdP8M3/core/modules/file/file.module on line 216 #0 /Applications/MAMP/htdocs/JdP8M3/modules/custom/tempo/tempo.module(48): file_move(NULL, 'Contrat-5-20170...', 1)

Any idea?

OK, I got it! My mistake: I load '$file' and I try to file_move '$file1' :-(

  • As you said, maybe you can try to use hook_entity_presave, get back file or files uploaded to your content entity, and then process them to rename the file using the file_move() function – guilhom Jul 6 '17 at 9:23
  • Agreed (I did that in the Drupal 6 version of my site). My question is more about how to "get back file or files uploaded to the content entity"... – gbmapo Jul 6 '17 at 12:03
  • Can you try on your hook_entity_presave : $files = $entity->get('file_field_name'); I'm using this on my hook_node_presave, I guess it's the same behaviour. – guilhom Jul 6 '17 at 12:23
0

Please refer the below articles it will be helpful for you..

How do I change the uploaded file name after the node has been saved?

Changing file name on upload

0

Being newbie in drupal and spending my three hours to alter the form from function MY_THEME_form_user_form_alter(&$form, &$form_state, $form_id), I was able to achieve what I wanted. I found setFileUri and file_move helpful altogether. I am just curious about if there is any loophole if I just use this much code. Do I need to do anything else apart form this?

  $file_name = "";
  $username = "";
  $fid_0 = 'photo';
  $uid = $form_state->getValue('uid');

  if ($form_state->hasValue('user_picture')) {
    $username = $form_state->getValue('field_username')[0]['value'];

    $fid_0 = $form_state->getValue('user_picture')[0]['fids'][0];
    $file_name = $uid . '-' . strtolower($username) .'-'. $fid_0;
    $file_name = trim(preg_replace("/[^0-9A-Za-z]/", '-', $file_name));
    $file = \Drupal\file\Entity\File::load($fid_0);

    $old_file_name = $file->getFilename();
    $old_file_name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file->getFilename());

    $new_filename_uri = str_replace($old_file_name, $file_name, $file->getFileUri());

    file_move($file, $new_filename_uri);
    $file->setFilename($file_name);
    $file->setFileUri($new_filename_uri);
    $file->save();
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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