In Drupal 7 this was possible.

$link = array(
  '#type'  => 'link',
  '#title' => t('My Title'),
  '#href'  => 'node/1',

But I've tried this with Drupal 8, and it does not output anything.

$link = array(
  '#type' => 'link',
  '#title' => t('test'),
  '#url' => 'node/1',

Is there a way I can render a link without having to call the Link plugin directly, or create my own theme function?

I'm theming a field group formatter, which must output a render array.
The use of drupal_render() is deprecated and should not be called directly; I'm using it here to make my example clearer.

  • Actually this should work. What means does not output anything ? Where do you print this rendered HTML ?? drupal_render() is deprecated. Call the RenderService directly: \Drupal::service('renderer')->render($elements, $is_recursive_call) Just to generate the link you could also use: Link::fromTextAndUrl($text, Url $url) See the link class. – rémy May 23 '16 at 7:53
  • What do you want to theme ?? – rémy May 23 '16 at 7:55
  • Thanks for the answer, but as stated I do not want to call the Link plugin directly. I've updated my question with a bit more background info to understand my scenario. – leon.nk May 23 '16 at 8:25
  • Have added info about what I'm theming to the question. – leon.nk May 23 '16 at 8:25

I think the problem with your Drupal 8 code is that '#url' needs a URL object.

External URL

'#url' => Url::fromUri('https://www.drupal.org'),

Internal URL from a route

'#url' => Url::fromRoute('entity.node.canonical', ['node' => 1]),

In Drupal 8 you are not supposed to render yourself. When porting code from Drupal 7, remove all rendering and return only render arrays.

  • Great thanks, this works! Yes I'm not drupal_render() directly, it was just for the example. – leon.nk May 23 '16 at 8:08

As an alternative, you can also use \Drupal\Core\Link::fromTextAndUrl() or \Drupal\Core\Link::createFromRoute().

Twig can print that as-is, you can also get a render array by calling \Drupal\Core\Link::toRenderable(). The resulting render array is the same then as building it from hand.


Here's an example of how to render a link in Drupal 8 with a custom class. It's a little odd, but the options are passed into the URL, not the link function.

use Drupal\Core\Url;

$options = array('attributes' => array('class' => 'my-custom-class'));
$url = Url::fromUri('http://www.example.com', $options);
$markup = \Drupal::l(t('Link Text Goes Here!'), $url);

This is useful in a field formatter, that expects markup returned. If you are doing this elsewhere, it's best to convert it to a link render array item.

use Drupal\Core\Url;

$options = array('attributes' => array('class' => 'my-custom-class'));
$build['examples_link'] = [
  '#title' => t('Link Text Goes Here!'),
  '#type' => 'link',
  '#url' => Url::fromUri('http://www.example.com', $options)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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