0

Using View's module it is possible to create a page to display some content horizontally. I have done this using carousel feature. The content type that these contents belong to have an image_field and a body_field.

What I want from Drupal 8 is that when these images are clicked they should redirect visitors to a specific page (not to the node's / content's page).

Is it possible to do so or using html on body_field to place the images with a and img tags is the only way?

UPDATE:

At first I understood nothing from the answer below but a day later it just made sense like in the movie "Phenomenon"; I was struck by a mysterious light in the sky. Here is what I did:

  • Created a new link_field inside the content type that I am using on this View.

  • Went back to the content / node (under Content) where I wanted to add the link to the image and filled the link_field

  • Added this newly created link_field to the View and made it hidden by checking the box titled "Exclude from display".

  • Clicked on the image_field on View settings (under the FEILDS) and REWRITE RESULTS > check the box "Output this field as a custom link" (An empty area shows up to be filled here).

  • Went down and expanded the drop down list titled "REPLACEMENT PATTERNS" and found the link_field's name (The name you gave to your link field you have created it) and copied the string (something like {{ the_name_of_my_link_field }}).

  • Pasted this string into the empty area above then Saved, Saved, Saved to exit.

That's all!

1 Answer 1

2

You can do this using the Rewrite feature of Views.

These instructions assume that the view is set to show Fields (instead of Content or something else). If you are not using fields, you can switch to fields under FORMAT in the Views UI.

  1. Click the image field in your view and select the checkbox Exclude from display.
  2. Add a new field to the view of type Custom text.
  3. On the custom text configuration screen, check the Replacement patterns. This will include some twig tokens that you can use, including the image file. You can then use this token to add HTML to the Text field to get the link you want. For example, <a href="/mypage">{{ image }}</a>.

Note that when using Views twig replacement tokens in a custom text field, order matters! If your custom text field is above another field, it won't be able to get the tokens from the field(s) below. In that case, you will need to click the flippy triangle to the right of Fields in the Views UI and choose rearrange, and then move the field that uses tokens below the field(s) that supply the tokens.

Another option is to open up the settings for your image field in Views and, under Rewrite results, click Output this field as a custom link and set the link options there.

1
  • Thanks, it took me a while to understand it. Commented Oct 28, 2020 at 19:09

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.