0

I have several custom content types and in one specific one I need to offer two fields, one for the href for a link, and one for the text of a link this way I can build and style it with minimal user involvement of the HTML/CSS. I also have a custom node.tpl for this content type. My problem is that drupal throws divs around each field I create that aren't in my template file for this content type (node-custom.tpl) and I can't put the href for a link with divs around it inside <a href="<div id="something">google.co.uk</div>"></a> See my problem. Maybe I'm doing this all wrong so any other ideas are welcome.

Please note I'm trying to create this site with minimum involvement of HTML/CSS access for the user. I'm aware I could hand code the link in the field.

apaderno
  • 28,547
  • 16
  • 75
  • 90
HomeBrew
  • 849
  • 2
  • 12
  • 25

1 Answers1

1

The easiest way to do this would be to use a preprocess function in your template.php file and build the link up manually:

function mytheme_preprocess_node(&$vars) {
  $node = $vars['node'];
  if ($node->type = 'my_type') {
    $uri = $node->field_name_of_link_field[LANGUAGE_NONE][0]['value'];
    $text = $node->field_name_of_display_text_field[LANGUAGE_NONE][0]['value']; 
    $vars['my_link'] = l($text, $uri); // Using Drupal's l() function to render a link
  }   
}

Then in your node template file you'll have access to the variable $my_link which you can output anywhere, and will contain the correct HTML for the link. Finally, go to the "Manage Display" page for your content type and set the display of the two fields you no longer need to output to 'Hidden'.

There are other ways so if that's no good let me know

EDIT

Just to add, I think the easiest way to do this would actually be to install the Link module and use the provided field type instead of the two other fields you're currently using.

Clive
  • 36,918
  • 8
  • 87
  • 113
  • Great, thanks for the response. That all seems pretty straight forward so ill give it ago tomorrow. Just to clarify, this is for a graphical button that appears in the bottom right of several web pages in the site. the content within the link also changes. I thought doing it this way would make it easy for the 'content editor' to maintain, is this a good idea or would you advise on going a different route? Cheers much appreciated. – HomeBrew Nov 09 '11 at 22:47
  • Actually on second thought you could just install the [Link module](http://drupal.org/project/link) which provides a field type you can attach to your node that will only accept links. That will sort out the issue you're having and give the added benefit of validating the link for you :) – Clive Nov 09 '11 at 22:49
  • Awesome, cheers. I may try your first suggestion just to learn as I've not played around to much with any drupal functions. Most likely ill end up using the link module though. Muchos Gracias! – HomeBrew Nov 09 '11 at 23:04
  • @user823903: No problem! Don't forget to accept the answer if this helped, it'll let others with the same problem know they've found the right answer :) – Clive Nov 10 '11 at 08:30