0

I want to display a button on the homepage. The best solution i've thought about was to render it in a block. The code is the following :

  $button['my_tickets'] =array(
    '#type'       => 'link',
    '#title'      => t('My tickets'),
    '#href' => '',
    '#class' => 'btn btn-default',
    '#id' => '',
    '#options' => array(
      'html' => true,
    )
  );

And here's the theme :

  function My_module_theme() {
    return array(
      'my_module_my_button' => array(
        'variables' => array('vars' => NULL),
        'template' => PATH_TO_TEMPLATE,
      ),
    );
  }

Problem : The link isn't rendered with the classes inside the "a" tag. How can I do that ?

5
  • 1
    The best way would depend on how your homepage is set up currently. Do you use panels? Just blocks? Is there any WYSIWYG content on the homepage already? Chances are you should add it using a WYSIWYG or possible use l()
    – sareed
    Dec 22, 2015 at 15:51
  • I don't use panels, just block. No wysiwyg content in the homepage for now.
    – Ismail H
    Dec 22, 2015 at 15:57
  • 1
    Have you tried just adding the block through the UI admin/structure/block/add and using the WYSIWYG editor to make the link and then style it as a button?
    – sareed
    Dec 22, 2015 at 16:09
  • I have to make it translatable afterwards. Having it constructed trought the UI would prevent me from it.
    – Ismail H
    Dec 22, 2015 at 16:17
  • 1
    Blocks can be translated as well via Block languages module (i18n_block) - part of Internationalization module (drupal.org/project/i18n) Dec 22, 2015 at 16:25

3 Answers 3

2

I think the classes should be added in the attributes array:

 $button['my_tickets'] =array(
    '#type'       => 'link',
    '#title'      => t('My tickets'),
    '#href' => '',
    '#attributes' => array('class' => array('btn', 'btn-default')),
    '#id' => '',
    '#options' => array(
      'html' => true,
    )
  );
1

You should use the following code instead

$link = array(
  '#theme' => 'link',
  '#text' => 'Click Here',
  '#path' => $path,
  '#options' => array(
    'attributes' => array('class' => array('cool-class'), 'id' => 'cool-id'),
      //REQUIRED:
      'html' => true,
   ),
);
0

That seems like a slightly complex way of making a button, Why not just add to your page or page-front template: <a href="your-link-destination" class="btn btn-default">My tickets</a>

3
  • 2
    Because hard-coding links in to a tpl is bad practice.
    – sareed
    Dec 22, 2015 at 15:47
  • No hard-coding allowed in the project ^^
    – Ismail H
    Dec 22, 2015 at 15:58
  • aha! I see you updated your question with more specifics!
    – olisb
    Dec 22, 2015 at 16:38

Your Answer

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

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