1

I use the following code to modify the image in the user_picture field of user with uid 42:

// Get user.
$user = \Drupal\user\Entity\User::load(42);

// Create file.
$file = file_save_data(
  file_get_contents('/tmp/image.jpg'),
  'public://pictures/image.jpg'
);

// Set file.
$user->set('user_picture', $file->id());

// Save user.
$user->save();

It works great, but it's very simplistic and doesn't do any of what has been configured in /admin/config/people/accounts/fields/user.user.user_picture, e.g:

  • Validate "Allowed file extensions".
  • Possibly downscale based on "Maximum image resolution".
  • Save image in the "File directory" location.
  • Validate "Maximum upload size".

Is there a way to somehow upload and set the file through what has been configured in the field?

2 Answers 2

1

You can access the values of the image field settings with:

$user->getFieldDefinition('user_picture')->getSettings();

Which will return an array, example:

enter image description here


You then can use file_save_upload

and you can pass to it for second param, an array containing all the validation functions

array $validators: (optional) An associative array of callback functions used to validate the file. The keys are function names and the values arrays of callback parameters which will be passed in after the file entity. The functions should return an array of error messages; an empty array indicates that the file passed validation. The callback functions will be called in the order specified in the array, then the hook hook_file_validate() will be invoked so other modules can validate the new file.

source: https://api.drupal.org/api/drupal/core%21modules%21file%21file.module/function/file_validate/8.9.x

Here are the validation functions you can use:

enter image description here

Source: https://api.drupal.org/api/drupal/core%21modules%21file%21file.module/8.9.x

2
  • Thanks. My file exists on disk but the first arg in file_save_upload regards an "upload form element" which I do not have.
    – cherouvim
    Commented Nov 23, 2020 at 5:56
  • @cherouvim usually you would create a form. But if you're just doing this programmatically without any UI for some reason, then keep doing it the way you're doing it and just pass the $file through all the validation functions and add a conditional to your save, so it only saves if it passes through all of them.
    – No Sssweat
    Commented Nov 23, 2020 at 6:04
1

Follow what No Sssweat said and as the documentation explains that "The files will be added to the file_managed table as temporary files. Temporary files are periodically cleaned" you'll need to set your file as permanent before to save it, else it will be erased during a cron task.

if ($file = file_save_upload($form_field_name, $validators, $destination, $delta, $replace)) {
  $file->setPermanent();
  $file->save();
}

Your Answer

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

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