Pass link to a field (e.g. image or text field) in display suite

criscom's picture

Can this be done with Display Suite?

I have a file field (field_download) with which I load a PDF document into the node. This PDF field has a label "DOWNLOAD". Clicking on DOWNLOAD opens the PDF. Question 1: I'd like the PDF to open in a new window/tab.

Then there is an image field (field_image) with which I load an image into the node. Question 2: Now I want the PDF link to be passed to the image so that a click on the image opens the PDF in a new window/tab.

I am sure this might be a frequent feature request. Still, I haven't figured it out how to do this with Display Suite. Do I need a preprocessed field with PHP Code? Any advice, hint or PHP suggestion would be very welcome. Thanks in advance.



a response using PHP

paulhhowells's picture

I hope there is a nicer answer which allows you to do all these things through configuration rather than php.

Having said that, if you make yourself a custom Display Suite layout then you will have at least two files in your layout, one ending in .inc, and another ending in .tpl.php.

Within the .tpl template file you normally print out the custom DS layout region variables. For your purposes note that you can also access the $variables variable, and can print out parts of it. For example you might find $variables['field_download']) useful.

Try using kpr(), dsm() or dpm() to take a look at $variables. (You probably want to have the Devel module installed and turned on if you do this). e.g. Within the .tpl add a line like:

<?php kpr($variables); ?>

Sorry this isn't a fuller answer, but I hope it has some useful pointers in the right direction for you. (I haven't tested your scenario, but I did something similar printing out $variables['field_link'][0]['display_url'] within my own custom DS Layout about a month ago).

building a custom DS layout

paulhhowells's picture

Within the DS module is the file ds.api.php. At line 471 are some notes on adding your own custom DS layout. I found taking a look in the 'layouts' folder (also within the DS module) and examining the predefined layouts to be very instructive.

Thanks for the quick reply. I

criscom's picture

Thanks for the quick reply. I will try your approach though I am not that proficient in PHP. I'll let you know when I come up with a solution, though I would prefer to do this through configuration. Many thanks!


paulhhowells's picture

I hope you can find a solution using configuration too! If you find one please do record it here as I would certainly use it myself in future.


Lars Bo Jensen's picture

A configuration-based solution would involve Views. Create dynamic fields in DS, context-sensitive views (context: node ID) and set the dynamic fields to load your view(s). Works with master views and/or view panes, I think.

context-sensitive views

criscom's picture

Hi Lars,

this seems like a very interesting approach, which could also be useful for a real estate project: display all appartments of a builder.

I found more info here:

I will follow up on both: passing on a link to a field and the context-sensitive views of above link.



custom formatters

criscom's picture

I tried the custom formatters but I somehow hit a wall with my PHP knowledge.

What I tried to achieve:

Have a link field [field_promo_link_extern] and I want the title field to receive the field from the link field. ==> the html should look like this: <a href="" target="_blank">Node Title</a>

I created a custom formatter with the custom formatter module, set the format to PHP and declared link_field in the Field type(s)

In the formatter I put the following code:

$node_title_custom = $variables['#object']->title;
$url = $variables['#items'][0]['field_promo_link_extern'];
$options = array('attributes' => array('target'=>'_blank'));
Return l($node_title_custom, $url, $options);

But somehow the URL doesn't work. As a result I get:
<div class="field-promo-link-extern"> <a href="/drupal-7.17/" target="_blank">Lorem ipsum dolor</a> </div>
The node title is set correctly (Lorem ipsum dolor) but the link is wrong. If I go to the content type to apply the new formatter to the external link field the title of my link field is correctly set to the node title but the URL is wrong: http://localhost:8888/drupal-7.17(which is the base URL of my install, leading to the frontpage. It should be something like:

The error messages points out that I am using the wrong variable for my external link field.

 Notice: Undefined index: field_promo_link_extern in eval() (Zeile 2 von /Applications/MAMP/htdocs/drupal-7.17/sites/all/modules/custom_formatters/engines/ : eval()'d code).

Custom formatter screenshot:
Screenshot from node with custom formatter field (Externer Link):

Any help to get this to work would be very much appreciated. I think I am just a short step away from the solution.

new approach

criscom's picture

I tried this:

$node_title_custom = $variables['#object']->title;
$path = $element['item']['field_promo_link_extern'];
$url = file_create_url($path);
$options = array('attributes' => array('target'=>'_blank'));
Return l($node_title_custom, $url, $options);

Still get the Undefined index: field_promo_link_extern notice.

How can I get the URL from the field_promo_link_extern field?