0

I created new image database on localhost (other than Drupal original database) that has images in one column. Then I installed the Views Database Connector module; I could integrate it with Drupal to display the content of the new image database. I can easily see columns like name, email, phone number stored in this new image database as string of characters, which are all text columns. When it comes to display an image from the external database, I am stuck.

I tried saving images in two formats.

  • As URL linked to the path of the image file
  • As a BLOB

In the first case, I see the URL; in the second case, I see a string of characters.

How do I make the Views module understand that this field is an image it should display as image and not as text?

  • You'd have to format it as an image tag in the first example, and in the second (I assume it's base 64 string?) try base 64 decode and/or also image tag (dont know a ton about encodings) – Kevin Jun 27 at 10:28
  • Does this work for you? Rewriting image output in views – sonfd Jun 27 at 11:18
  • Hi Kevin thanks for your reply. but where do i tell Drupal Views while adding image field from external database that it is an Image (url or BLOB) and not a sting of characters. – Nilesh Kulkarni Jun 27 at 14:14
  • Hi sonfd. i am not from programing background so not sure where to add the rewriting images output in views. could you just explain ? Thank you. – Nilesh Kulkarni Jun 27 at 14:28
1

Views fields have an option to overwrite the output of the field. You can use this to add any html you want, using twig. You'll have all fields rendered before your field available as twig variables.

In your case, since you know that you're getting an image url from your external database, you can rewrite the output of your field using twig. For example:

<img src='{{ my_image_url }}' alt='{{ "Alt text for my image"|t }}'>

To access the Rewrite Results option, click Edit on your field in the Views UI. Near the bottom of the dialog, you'll see several collapsed fieldsets. Open the Rewrite Results fieldset and check the box for Override the output of this field with custom text. You'll get a text area and another fieldset, Replacement Patterns, below it that lists all variables available for your rewrite. Find your field name for your image url and write your output similar to the snippet above.

Image of Rewrite Results area in Views UI demonstrating behavior described in the previous paragraph.

| improve this answer | |
  • Hi Sonfd i really thank you for detailed explanation. I will follow it step by step and update. Thanking you once again. – Nilesh Kulkarni Jun 27 at 16:37
  • I added images using media content type. generated their url aliases stored these aliases as a path in external database and followed ur last comment. Now i can see small image icons in the output of VIEWS which we generally see when image is not available / not found. I do not know what is still missing. – Nilesh Kulkarni Jun 27 at 17:56
  • Oh, the media entity url alias is not the same as the image url. You need the image url. – sonfd Jun 27 at 18:15
  • how can i share image ? – Nilesh Kulkarni Jun 27 at 18:18
  • You'll have to add a new question with the details of that environment. – sonfd Jun 27 at 18:22

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.