1

I have a field that will accept an image link coming from the external source. But of course, to set that image I have to save it in Drupal first. I'm wondering how do Drupal save the image from an external link. I use the following code.

$imageUri = 'https://api.statvoo.com/favicon/?url=google.com'; // this is a favicon

$file = File::create([
          'uri' => $imageUri,
          'status' => 1,
        ]);
$file->save();

$user_profile->field_profile_image[] = [
        'target_id' => $file->id(),
        'alt' => 'Alt text',
        'title' => 'Title',
        'width' => 200,
        'height' => 200,
      ];

$user_profile->save();

I notice that the following values and the external image URL were added in the image field, but the image is not displayed on the page. Thanks in advance!

2
  • I don't think this is supported. If you want to display external links I would instead suggest to use a link field and a custom formatter. It does get more complicated if you want to mix local and remote images in the same field. Then I would suggest you use media and a custom media source plugin
    – Berdir
    Commented Aug 27, 2018 at 7:29
  • This code in php does uploads image in the the desired directory. $file_content = file_get_contents($imageUri); $directory = 'public://2018-08/'; file_prepare_directory($directory, FILE_CREATE_DIRECTORY); $imageUri = file_save_data($file_content, $directory . basename($imageUri), FILE_EXISTS_REPLACE); but unfortunately, its still not able to display the image, though I can access the photo in the same directory where it upload/save the image by default(on manual upload of image in editting node page). This is what I want to achieve similarly drupal.stackexchange.com/a/227182/70281. Commented Aug 27, 2018 at 7:37

1 Answer 1

0

Well, I think it is supported. I found an exact solution I need to be able to save and set an external image source inside Drupal according to Programmatically update node image field (using URI of the image) and get URL in twig.

I use this method system_retrieve_file.

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.