2

I'm quite new to Drupal but I've been a developer for a few years. I'm struggling with what should be a very simple task. I have created a field 'field_client_link' that authors can use to enter a url to a portfolio content type about a client. If an author has entered a link I want to wrap a client logo in it, otherwise just display the image. I'm sure this is not as hard as I'm making it out to be. In WordPress I would just write something like this and be done with it:

<?php 

    if (get_field('link')) {
        echo '<a href="' . get_field('link') . '">' . get_field('image') . '</a>';
    } else {
        echo get_field('image');
    }

?>

Here are my settings. I've set the link to be hidden because I don't want it outputting until I call it later. Hoping you can help me:

Views settings

Configure field client link

Configure field client logo

4 Answers 4

0

Unfortunately, I know of no easy way to do this.

Let me be more specific -- you've provided a Views example where you're showing you can Rewrite the output, there are some tokens you can replace values with. You also show some custom php you'd write in wordpress.

Well, in order to do this in Drupal; so far as I know you have to use come custom php as well. Views is designed to make simple lists (read SQL queries) as output. Conditional values such as this are essentially out of the basic Views UI scope.

So you either use the Views PHP module, create a views template file and preprocessor function or use the module mentioned by @PatrickRyan.

1
  • Thanks. It seems wrong to use a module for something so simple so I will try and preprocess it. Thanks for the explanation.
    – chap
    Commented May 4, 2015 at 0:32
1

There's a lightweight module that lets you attach a custom link to any image using the image formatter setting: Image Link Formatter

It will automatically detect any link field and make it an option to set that link for the image.

I usually just go this route instead of dealing with tokens or template modifications every time I want to link an image.

1
  • take all my upvotes :D
    – tenken
    Commented Apr 28, 2015 at 1:12
0

I hope you have a separate field for the logo (eg: client_logo) and a link field (Eg: client_link) in your content type. Now install and enable Views Conditional module.

Go to your view add whatever fields you want including the client_logo and client_link under FIELDS section. Exclude both of these fields from display (edit your field and tick Exclude from display). Set "Formatter" to "URL, as plain text" in link field (field_client_link)

According to above module guidelines do following steps 1.Click on "ADD" next to fields. Scroll down to "Views: Views Conditional", probably found near the bottom of the list.

  1. Set "If this field..." to your link field (in this example field_client_link)
  2. Set "Is..." to "NOT EMPTY"
  3. Then add the out put using html tags and tokens (You can find tokens under "REPLACEMENT VARIABLES")

for this example under "Then output this..."

<a href='[field_client_link]'>[field_client_logo]</a>

and under "Otherwise, output this..."

[field_client_logo]

Note: You don't need to include tag to print the logo because the [field_client_logo] is already enabled with tag fron Drupal view.

0

So I've been playing around with this for a while and finally figured out a solution that is possible with views only (although in my example I use the Entity reference module). I thought I'd post it here since I took me ages to figure out and I hope I can save someone the hassle. It's actually not that hard in the end.

The aim here is to have a view that lists all client logos and if that client has a related project, link to that, otherwise don't.

Step 1 is to get the entity reference module.

Step 2 is to add an entity reference field to your client content type with an autocomplete widget. You'll want to target a specific content type. In my case I targeted a project. Make sure you set a maximum of 1 entry for this field/

Step 3 is to edit your client content and add references to projects in your newly created field.

Step 4 set up your view. You'll want to get your entity reference field that you just created and set it up. You need to exclude it from display and you select entity id from the field formatter option.

Settings for entity reference field

Step 5. Next you need to rewrite the output of your entity reference field. You'll want to use the format node/ + [replacement_token] so you get a valid url. Importantly, you need to go into the No results behaviour tab and check hide rewriting if empty. Because we hid our entity field from display, it will now only be rewritten if someone has added a reference to a project in the client content type.

Rewrite results settings for entity reference field

Step 6. Now we need to edit the settings for the logo field. Importantly, the field order must be entity reference field followed by logo field.

Views base settings

Step 7. In the logo field settings go to rewrite results and check output this field as a link. In the link path we can use replacement tokens and we want to use the field that we have just rewritten in step 5. In my case I rewrote [field_related_client_projects]. Before it was just an entity id. e.g. 768 or whatever. Now however, this replacement token is node/nodeid so it can be used as a link

Rewrite results for logo

Step 8. Uncheck hide rewriting if empty.

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.