Posted by akanik on June 27, 2011 at 6:14pm
So this may be a silly, simple question... but it has me. I am trying to modify a field to be output as a link. Scenario: the user adds his/her personal website address (akanik.com) in to the "Website" field. The template takes that field, enters the value of it into the a href and therefore makes the original field data (akanik.com) clickable.
<div class="each left">
<?php print render($content['field_email']); ?>
<a href="http://<?php print ($content['field_website']); ?>"><?php print render($content['field_website']); ?></a>
<?php print render($content['body']); ?>
</div>With the above structure, what is returned is an array, not the value of field_website. What syntax would I need to accomplish linking the field to what it describes?
Thanks,
Allie

Comments
Theme your field
Try creating field--your-field.tpl.php (in your case field--field-website.tpl.php), and editing it so that it produces the code you want. You can use field.tpl.php in the modules/field folder as a starting point.
To do this, you want to install Devel module, which among other things lets you use the function
dsm('foo');, where foo is an object or array, and prints a neat little expandable breakdown of what's inside it. So in your field--your-field.tpl.php you can dsm() the arrays $element and $items (and if you keep the default foreach statement, $item too) to find the values you want and print them out.Eventually you're going to come up with something like:
<?php foreach ($items as $delta => $item) : ?>
//you might need to drill into $item to get the unprocessed value for the $item in the href, can't remember.
<a href="<?php print render($item); ?>"><?php print render($item); ?></a>
<?php endforeach; ?>
There are a couple other options, like drilling into either the $node object or $content array until you get to the value you want, or if you can get intense with an entity wrapper. But theming your field is probably the easiest and most Drupal way to do it.
Ok, sounds great. Where do I
Ok, sounds great. Where do I put the
dsm('foo');code? Just in a php tag inside of field--field_website.tpl.php?*Sorry that was totally in your earlier posting. I tried to use that code and I got nothing in the message area... I'll have to do some research on the devel module. I probably don't have something configured correctly. Thanks!
You got it. You'll be able to
You got it. You'll be able to see the result on the Devel tab of your node.
[edited the code example above] Where I said "unprocessed safe value", I meant "unprocessed value". The safe value is the processed/sanitized value, which might cause some problems if '&' or other special characters are in the url. Also, where I said the tpl would be named field--your_field.tpl.php, I should have said field--your-field.tpl.php: all field name underscores should be turned into hyphens in the theming files. The perils of writing support posts after work and before food.
Got it!
I thought that that was going to be much more painful than it was. Thanks so much for your help with that and so many other issues that I've posted. You make my brain hurt a lot less!
allie