0

I am trying to rewrite the output of node.tpl.php.

I am using field link. I cannot figure out how to get the field to render as a button. I did find that the field is output as an array

I tried this but it just makes the link just a link to the page I am on:

<a class="btn btn-danger btn-lg" href="<?php print render ($content[field_amazon_link][LANGUAGE_NONE][0]['display_url']); ?>">Buy Now</a>

The devel module gives me this info. Keep in mind php is my week area. This is my first time rewriting a node file as I used to have a helper and he has moved on.

field_amazon_link (Array, 1 element)

0 (Array, 5 elements)
    url (String, 142 characters ) https://www.amazon.com/gp/product/B00ZGUBN3C/re...
    title (String, 83 characters ) https://www.amazon.com/gp/product/B00ZGUBN3C/re...
    attributes (Array, 1 element)
    html (Boolean) FALSE
    display_url (String, 83 characters ) https://www.amazon.com/gp/product/B00ZGUBN3C/re... 

And this is from the object devel tool field_amazon_link (Array, 1 element)

und (Array, 1 element)
    0 (Array, 5 elements)
        url (String, 142 characters ) https://www.amazon.com/gp/product/B00ZGUBN3C/re...
        title (String, 83 characters ) https://www.amazon.com/gp/product/B00ZGUBN3C/re...
        attributes (Array, 1 element)
        html (Boolean) FALSE
        display_url (String, 83 characters ) https://www.amazon.com/gp/product/B00ZGUBN3C/re..

2 Answers 2

0

This is what ultimately worked for me. I don't really understand it completely, but if anyone can shed some light on why it worked, I would really like to know.

<a class="btn btn-danger btn-lg" target="_blank" href="<?php print $content['field_amazon_link']['#items']['0']['url']; ?>">Buy Now</a>
0

The gist of it is to add the correct classes btn btn-danger btn-lg to the array before rendering it. You could accomplish it like this, with in a '.tpl.php' file:

<?php
  $content['field_amazon_link']['#items']['0']['attributes']['class'][] = 'btn';
  $content['field_amazon_link']['#items']['0']['attributes']['class'][] = 'btn-danger';
  $content['field_amazon_link']['#items']['0']['attributes']['class'][] = 'btn-lg';
?>

Then you can use the Drupal API to render it to HTML using the render function the whole, and print it, like so:

<?php print render($content['field_amazon_link']); ?>
2
  • @tHaKaREe, I didn't get to test this code, i will do when i have a chance. Also, I would like to provide some alternative implementations. Sep 3, 2018 at 21:37
  • 1
    I appreciate the help either way. Thanks. Got it working with my above solution I found
    – tHaKaREe
    Sep 7, 2018 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.