Creating a link field in a custom content type

6.4k views Asked by At

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.

1

There are 1 answers

4
Clive On BEST ANSWER

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.