Hi there,
I am trying to do something that's seemingly simple, but I just can't seem to figure out the "missing link" between success and failure.
The situation:
I have created a node type "Home Boxes" that has two CCK fields: One is an image, and the other is a text field for a link. I have created a block in Views that will put the 2 Home Boxes on the home page. The trouble is that I would like to input a specific link in the CCK text field and have it be the image's link to another page (specified in the CCK text field).
I've tried creating a file (views-view-field--Home-Boxes--block-1.tpl.php) using the theme wizard, but I'm not having any luck getting it to actually put the link on the image. I'm quite new to all of this, so my them file may be completely wrong:
<a href="<?php print $field_home_box_link ?>"><?php print $field_home_box?></a>
<?php print $output; ?>Any assistance or tutorials you can point me to would be a great help.
Thank you!!
Comments
Bump
Sorry ... I know this may seem like a relatively simple question to most, but I've spent a lot of time trying to figure this out. Any assistance would be most appreciated! If you need additional information, I'd be happy to provide it.
A little further?
OK, now I have changed the code to:
<a href="<?php print $field->field_home_box_link; ?>"><?php print $output; ?></a>this is on the views-view-field--Home-Boxes--block-1--field-home-box-fid.tpl.php custom theme.
I know it's pulling from the theme because I've tried putting a static link in place of the $field->field_home_box_link; portion of the code string and the static link works; however, when I use $field->field_home_box_link;, it's producing a blank url. Therefore, I think I need something other than $field->field_home_box_link; for that spot. Any ideas?
Help!
Try this
<a href="<?php print $node->field_home_box_link[0]['value']; ?>"><?php print $output; ?></a>This should take the first of those elements (I assumed you only had one per page) and prints it as the href value in the link.
Still no luck ...
Thank you for your help, but it's still coming up with a blank link. I do have two elements on the page, each with a unique link, if that helps. Any other ideas?
Many thanks!
Machine readable name
Is "home_box_link" the machine-readable name of the field? I've used this with other CCK fields I've set up with no problems.
Yes
Yes, "field_home_box_link" is the machine-readable name of the field. I must be missing something else somewhere else. Ugh. I really appreciate your help.
I'm a little slow. This is
I'm a little slow. This is apparently something I've misread. You have a block that's trying to load the node's CCK fields into the block, so you'll have to do some tricky PHP as well.
Try adding this before the last line I gave you (which is right):
$node = node_load(arg(1));This will grab the node ID of the page you are on (the home page node) and from there it will assign the home page's information (including the CCK fields and values) into the block content so you can then print it out with the previous statement I gave you.
Still no luck ... I've probably done a bad job of explaining ...
I'm sorry ... I really do appreciate your help; I hope one day I can return the favor to someone else.
Maybe I can explain the situation a little more specifically. The home page of the site I'm working on, http://dev.hstt.org, has two small boxes on the lower right-hand side (currently bordered in red, I'm working on some css issues with them, as well).
I would like the Humane Society staff to have a simple way to be able to swap out those boxes and link them to wherever they please. In order to accomplish this, I have created a Content Type "Home Boxes," with two CCK fields: one CCK field is the image itself, the second CCK field is for them to input the link. I have created two nodes to get them started (they will only have permission to update these nodes, and not add more), which you will see are the third and fourth boxes on the page.
I created a block view with these Home Box content type nodes. Through this view, I am trying to get the link to wrap around the image within the HTML by manipulating the "views-view-field--Home-Boxes--block-1--field-home-box-fid.tpl.php" file. As you will see, it's putting a blank href tag in there.
To be sure the Theme file is loading at all, I just tried replacing the href print string with 'http://cnn.com', rescanning the files in the views block, and it changed the link to cnn.com (I just changed it back to your recommendation after testing that).
I'm I going about all this bass ackwards? Again, many, many thanks.
One last try ...
bump
did u ever fiind an easy way to do this
got the same issue
Try the following
<a href="<?php $node = node_load(arg(1)); print $node->field_home_box_link[0]['value']; ?>"><?php print $output; ?></a>A block that displays an image, or an image link...
I think, I had a similar problem, which I finally solved with a block and CCK (D6). I hope, this helps!
Requirements:
Solution:
This is my very first custom PHP/Drupal code, so it probably isn't elegant, and could be quite dangerous --but it works as expected...
function cover_block($op='list', $delta=0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Cover Image');
return $blocks;
case 'view':
// Find out current node
$actual_node = node_load(arg(1));
// Find out referred cover image node
$nid = $actual_node->field_cover_image_ref[0]['nid'];
$node = node_load($nid);
$image = $node->field_cover_image[0]['filepath'];
$alt = ' alt="' .$node->field_cover_image[0]['data']['alt'] .'"';
$link = $node->field_cover_image_link[0]['value'];
$blocks['subject'] = t('Cover Image');
if ($link) {
$blocks['content'] = '<a href="' .$link
.'"><img src="/' .$image .'"' .$alt .' /></a>';
} else if ($image) {
$blocks['content'] = '<img src="/' .$image
.'"' .$alt .' />';
} else {
$blocks['content'] = '<img src="some_default_here.jpg" alt="" />';
}
return $blocks;
}
}
image links with Views, Link, and ImageField
I'm not sure if this is exactly what you're looking for, but here's how I did something similar.
Background: I wanted to display a list of "sponsors" on a nonprofit org's website. The website would have a page /sponsors which has a list showing information (including a logo, company name, and description) for each sponsor. The sponsor logo would be an image link to the sponsor website.
Basic idea: Create a Sponsor content type with CCK and display it with Views. Also uses ImageField and Link CCK types.
In addition to the normal title and body fields, the Sponsor type will have the following fields:
Label | field name | Type-------------------------------------------------
Logo | field_logo | File (widget is Image)
Website | field_website | Link
With the content type setup, we create a new view (/admin/build/views/add) and call it 'sponsors'. Then we edit the view (admin/build/views/edit/sponsors).
In the Filters box, we add a filter "Node: Type = Sponsor". Now our list will just show sponsors.
In the Fields box, "Node: Title" and "Node: Body" are already there. We'll need to add our website URL and sponsor logo. Click "+" and add "Content: Website" and "Content: Logo". I then rearranged the fields so those two content fields came before Node: Title. NOTE: Since we'll be referencing the website URL from the logo, "Content:Website" must come before "Content: Logo".
Setting up the image link:
In the Fields box, click on "Content: Logo" and scroll down to configure how the image is displayed. If "Link this field to its node" is checked, disable it (we'll want to link to the website instead). Select the "Output this field as a link" checkbox and some more fields appear. We'll set "Link Path:" to the website url. Find the appropriate field in the "Replacement patterns" box below. Since the name of our website Link is field_website, the replacement pattern we'll put in the Link Path is "[field_website_url]". We also don't want any text label on the photo, so we'll change Label from "Widget Label" to "None".
At this point, the logo will be a link, but it will link to the title of our link, not the address. To fix that, go back up to the Fields box, edit "Content: Website", and change the Link display format from "Title, as link" to "URL, as link". Now [field_website_url] is a reference to the URL instead of title and our image link will work. Finally (while still editing Content:Website), since we don't want to show the URL in the view, we'll disable it by selecting "Exclude from display".
I also wanted to have the node title (Sponsor name, in my case) link to the website. This can be done as well, following essentially the same procedure.
Since I wanted this view to be a page (/sponsors), I selected "Page" and clicked "Add Display", then set the path under Page Settings. A block can be created just as easily by selecting Block and clicking 'Add Display'.
hope that helps!
can't link an image to a node reference within a view
I'm trying to accomplish the above scenario but linking an image to a Node Reference within a View. As has been stated, you don't get a "URL, as link" option only a "Title, as link" option. Is there any way to accomplish this within Views (without editing or extending Views).
Benjamin Lotter
"Delight thyself also in the LORD and He shall give thee the desires of thine heart." ~Psalm 37:4
Close, but no cigar
The above advice almost works for me; the problem is that my "link" is actually a node reference, and you don't get the URL with a node reference! Arrrggghhhh!
A workaround
The workaround I've found to this is to create a custom views template. Here's one that transfers the URL from a node field to an image field; the actual view just outputs the link to the node, and then an image that's linked to something, and this template mungs the urls (and suppresses output of the first field). Should be pretty easy to follow; apologies in advance for any stylistic faux-pas, I'm a Drupal/PHP newbie
<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
* @file views-view-fields--recent-products--default.tpl.php, hacked from
* views-view-fields.tpl.php, which you can get to by using the views theme:information
* link in a view, then click on row style output link to get the currently active code;
* You also get told the filename(s) you can use to put your new code in; you start by
* just copying the default code (if you're smart, anyway...)
*
* Name of file depends on what view you are messing with
*
* Default simple view template to all the fields as a row.
*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
* - $field->content: The output of the field.
* - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
* - $field->class: The safe class id to use.
* - $field->handler: The Views field handler object controlling this field. Do not use
* var_export to dump this object, as it can't handle the recursion.
* - $field->inline: Whether or not the field should be inline.
* - $field->inline_html: either div or span based on the above flag.
* - $field->separator: an optional separator that may appear before a field.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
*
* 09/10/21 - RJW - Horribly hacked to transfer a URL from one field to another
*
*/
?>
<?php foreach ($fields as $id => $field): ?>
<?php
// depending on the class of the field being emitted, do various things.
switch ($field->class) {
case 'field-page-nid' :
// in this instance, we extract the url from $field->content and save it, but do not display
// the field. Note that if field-page-nid is not output before field-image-cache-fid, this
// code will break horribly, cats and dogs will start living together, etc.
$field_page_nid_url = preg_replace('/([^"]+)"([^"]+)".*/',"$2",$field->content);
break;
case 'field-image-cache-fid' :
// hack in the url
$field->content = preg_replace('/([^"]+")([^"]+)(".*)/',"$1$field_page_nid_url$3",$field->content);
// fall through to regular processing
default :
?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
<?php if ($field->label): ?>
<label class="views-label-<?php print $field->class; ?>">
<?php print $field->label; ?>:
</label>
<?php endif; ?>
<?php
// $field->element_type is either SPAN or DIV depending upon whether or not
// the field is a 'block' element type or 'inline' element type.
?>
<<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
</<?php print $field->inline_html;?>>
<?php
}
?>
<?php endforeach; ?>
Link Image Field sorted it for me
http://drupal.org/project/linkimagefield