Drupal - How do I create a link?
\Drupal::l is deprecated. Maybe this case will be useful for somebody
use Drupal\Core\Url;
use Drupal\Core\Link;
$url = Url::fromRoute('entity.node.edit_form', array('node' => NID));
$project_link = Link::fromTextAndUrl(t('Open Project'), $url);
$project_link = $project_link->toRenderable();
// If you need some attributes.
$project_link['#attributes'] = array('class' => array('button', 'button-action', 'button--primary', 'button--small'));
print render($project_link);
One, this is not 100% complete, see this issue . With that said, let me quote some code from the change notice:
Drupal 7:
// Internal path.
$internal_link = l(t('Book admin'), 'admin/structure/book');
// External Uri.
$external_link = l(t('External link'), 'http://www.example.com/', array('external' => TRUE));
Drupal 8:
// Internal path (defined by a route in Drupal 8).
use Drupal\Core\Url;
$url = Url::fromRoute('book.admin');
$internal_link = \Drupal::l(t('Book admin'), $url);
// External Uri.
use Drupal\Core\Url;
$url = Url::fromUri('http://www.example.com/');
$external_link = \Drupal::l(t('External link'), $url);
Edit: route names are in the moduledirectory/modulename.routing.yml
files and (by default) in the {router}
table.
Here are some examples of creating links in Drupal 8. Note that $this->t('some text') is available within blocks that extend BlockBase. If you copy these to another class that doesn't have it or use these in a .module file, you may need to change this to t() 1.
Basic Link to a node:
$node = Node::load($nid);
$build['node_link'] = $node->toLink()->toRenderable();
This creates a render array like this:
$link = [
'#type' => 'link',
'#url' => $url_object,
'#title' => 'Title of Node',
];
You can create the render array without loading the node this way:
$url_object = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
$link = [
'#type' => 'link',
'#url' => $url_object,
'#title' => $this->t('Read More'),
];
Or using the core Link class:
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
If you want to use markup in the text of your link, you can't just put a string in. You need to use a render array element:
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
$link_text = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => $this->t('Load More'),
];
$link = Link::fromTextAndUrl($link_text, $url);
To create an absolute link, you add this option to the URL, not the link:
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid], ['absolute' => TRUE]);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
To add a class to your link, you also need to add this to the URL, not the link:
$options = [
'attributes' => [
'class' => [
'read-more-link',
],
],
];
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
To add a query string to your link, you also need to this to the URL, not the link:
$options = [
'query' => [
'car' => 'BMW',
'model' => 'mini-cooper',
],
'attributes' => [
'class' => [
'read-more-link',
],
],
];
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
To set the link to open in a new window with target = _blank:
$options = [
'attributes' => [
'target' => '_blank'
],
];
$url = Url::fromRoute('entity.media.edit_form', ['media' => $entity->id()], $options);
$link = Link::fromTextAndUrl(t('Edit'), $url);
$form['entity']['edit_link'] = $link->toRenderable();
Here's a link to a taxonomy term page:
$url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
Here's a link to node edit page:
$url = Url::fromRoute('entity.node.edit_form', ['node' => $nid]);
$link = Link::fromTextAndUrl($this->t('Edit'), $url);
$build['read_more'] = $link->toRenderable();
To create an external link:
$url = Url::fromUri('http://www.example.com/');
$link = Link::fromTextAndUrl($this->t('Vist this example site'), $url);
$build['external_link'] = $link->toRenderable();
Link to homepage:
$url = Url::fromRoute('<front>');
$link = Link::fromTextAndUrl($this->t('Home'), $url);
$build['homepage_link'] = $link->toRenderable();
Note that on any of these url objects, you can get the url as a string by calling:
$url->toString();
For example:
$url_string = Url::fromRoute('<front>')->toString();