I am getting my feet wet with D7 and came up against something that I haven't been able to Google yet. I am using views to get a list of variables in a custom block. One variable is:
$fields['field_main_image_large_title']->content
If I want to print out this variable I use:
print render $fields['field_main_image_large_title']->content;
Now I like to use this field as part of a url in a link. In D6 I'd just do:
<a href='my/url/<?php print $fields['field_main_image_large_title']->content; ?>'>myLinkTitle</a>
I tried this for D7 like this:
<a href='my/url/<?php print render $fields['field_main_image_large_title']->content; ?>'>myLinkTitle</a>
and while it inserts the variable string into the url, I get several spaces before and after the string. I am dealing with this at the moment in using the trim function on $fields['field_main_image_large_title']->content;
I am doing this in a views template.
While this works it feels like a hack and I am looking for somebody to point me in the right direction on how to do this the Drupal 7 way...
Comments
First of all, in d6 or in d7
First of all, in d6 or in d7 its very rare that I would ever print <a href....> at all. I would always use the l() function. To solve your specific problem I would create a process function in my template.php file that does something like this:
<?php
// NOTE This function assumes you want your link to appear in page.tpl.php. If you want it in node.tpl.php then you should use the mytheme_node_process() function instead...
function mytheme_page_process(&$variables) {
$url_part = trim(render($fields['field_main_image_large_title']->content));
$variables['link'] = l('myLinkTitle', 'my/url/' . $url_part);
}
?>
Now in your page.tpl.php you can simply put
<?phpprint $link;
?>
EDIT: P.S. There may already be a variable called "link" so be careful
Thanks for the response
Thanks for the response bleen18 that looks like the proper way of doing it. However, My code is running in a Views Row template where I have direct access to the individual variables. I'll have to find out how I can access these variables in a preprocess function. Any thoughts?
Pre-process function for a views template
Never used a pre-process function for a views template before. You have to tell Drupal first about the tpl file you are using. Better explained right here: http://evolvingweb.ca/story/theming-views-drupal-templates-and-preproces....
Thanks again bleen18. Now I am doing this the Drupal way :-)