Search code examples
phpdrupaldrupal-7drupal-themingdrupal-render

Drupal: How to render a span inside a link?


I want this result:

<a href="path/to/something">This is a link! <span>with a span inside!</span></a>


This renders a link:

$render = array(
  'link' => array(
    '#type' => 'link',
    '#title' => t("This is a link!"),
    '#href' => "path/to/something",
  ),
);


Why does this not render a span inside the link?

$render = array(
  'link' => array(
    '#type' => 'link',
    '#title' => t("This is a link!"),
    '#href' => "path/to/something",
    'span' => array(
      '#type' => 'markup',
      '#markup' => ' <span>with a span inside!</span>',
    ),
  ),
);


Thanks in advance!


Solution

  • Just adjust your code to be:

    $render = array(
      'link' => array(
        '#type' => 'link',
        '#title' => "<span>" . t("This is a link!") . "</span>",
        '#href' => "path/to/something",
         '#options' => array(
            'html' => TRUE,
        )
      ),
    );
    

    Hope this works... Muhammad.