6

Is there any way to reuse the same file or image while uploading I just worry about having multiple/duplicate image files on the server - in time it could get full and slow down the server.

I have used FileField Sources module but I think its not compatible with drupal 8 its not showing configuration setup, any other alternative?

3

The short answer is: Yes!

And you also don't have to wait till 8.4 comes out (however that will make things easier).

I suggest you the following modules:

How is it working? (in nutshell):

Instead of using "image fields" you will have reference fields to media entities (Media Entity), which provides you the opportunity to use such an entity multiple times, also by other contents. Media Entity Image gives you just a predefined bundle to media entity, which is a nice to have. Media entity browser gives you a widget, which opens a modal, where you can select from existing media images, or upload a new one!


Drupal 8.4 will provide this as a core feature, which will make it more stable and easier to use. But until then: these three modules also do the job.

| improve this answer | |
  • I hate that this always gives a lot of errors on installation. Not a fan. – Stef Van Looveren May 28 '19 at 15:04
1

I got into this question while searching for a way to reuse the same image during programmatic creation of multiple nodes. It is not completely clear to me if the question is about creating nodes programmatically or from the Drupal backend, but I think it could be useful for someone if I post the solution I found to the programmatic use case too (you can skip this part and go to the manual solution if you are not interested to coding).

Programmatic Solution

  1. assuming that you already have the image file on your local file system, you have to create an image object first:

    $imgUri = file_build_uri("import/default_image.jpg"); $imgFile = File::Create(['uri' => $imgUri]); $imgFile->save();

In the above code I assume that you have uploaded (e.g. via FTP) a file named "default_image.jpg" to the subdirectory "import" in your public file system, but of course you can place it wherever you want and use the name you prefer, simply change the argument to "file_build_uri" accordingly.

  1. for each node you want to create, if you now set the $imgFile object as the value of an image field the system will reuse the same image file. Find below a sample code for creating one node with a field image:

    use Drupal\node\Entity\Node; ... $node = Node::create([ 'type' => 'article', 'title' => 'Test article', 'field_image' => $imgFile, ... ]); $node->save();

Manual Solution

Finally I would like to give a suggestion for the manual use case, that is if we need to upload the same file multiple times (e.g. in a node creation form) and do not want the system to create multiple copies of the same file on disk. In this case the solution that works for me is to:

  1. store the image as a separate node, e.g. by creating a content type or taxonomy just for storing "default images"
  2. instead of uploading the image to the target content type, we refer to the image stored at point 1 using an entity reference field. So basically we wouldn't have an image field at all in our target content type, and would replace it with an entity reference field which references one of the default images.

Needless to say the solution above is suitable for when you really have a ton of files to upload and want to avoid the disk being filled-in with duplicates of the same image. If you only have a bunch of images I think there is no better solution than the easiest one: accepting the duplications and living with them :)

Note that the latest modules for Drupal 8 which "automagically" resolve this problem are based exactly on this technique. So you can also download Media Entity for example and rely on it (along with the supporting Media Entity Browser and other useful sub-modules) to do the job for you.

| improve this answer | |

Your Answer

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