Here are code snippets of creating internal and external links in Drupal 8 having query, fragment identifier (named anchor) and class attribute.
Internal link:
use Drupal\Core\Link;
use Drupal\Core\Url;
$options = array(
'query' => ['type' => 'article', 'status' => 1],
'fragment' => 'article-list',
'attributes' => ['class' => ['btn', 'btn-mini']],
'absolute' => TRUE,
);
$link = Link::fromTextAndUrl(t('Drupal node articles'), Url::fromUri('internal:/node', $options))->toString();
This code will generate HTML output of:
<a href="http://www.mywebsite.com/node?type=article&status=1#article-list" class="btn btn-mini">Drupal node articles</a>
External link:
use Drupal\Core\Link;
use Drupal\Core\Url;
$options = array(
'query' => ['page' => 1],
'fragment' => 'content',
'attributes' => ['class' => ['btn', 'btn-mini']],
);
$link = Link::fromTextAndUrl(t('Nginx articles'), Url::fromUri('https://www.webfoobar.com/taxonomy/term/23', $options))->toString();
This code will generate HTML output of:
<a href="https://www.webfoobar.com/taxonomy/term/23?page=1#content" class="btn btn-mini">Nginx articles</a>
Using route name
We will use \Drupal\Core\Url::fromRoute()
static function if we are to use route name instead of URI. In this function, the first parameter is the route name, the second parameter is "query" option and the other options becomes the third parameter. Here's an example of link to modules listing page with fragment identifier (named anchor):
use Drupal\Core\Link;
use Drupal\Core\Url;
$link = Link::fromTextAndUrl(t('Drupal 8 modules list'), Url::fromRoute('system.modules_list', [], ['fragment' => 'edit-modules-field-types', 'absolute' => TRUE]))->toString();
This code will generate HTML output of:
<a href="http://www.mywebsite.com/admin/modules#edit-modules-field-types">Drupal 8 modules list</a>
And here's an example of link with query, fragment identifier (named anchor) and class attribute:
use Drupal\Core\Link;
use Drupal\Core\Url;
$query = array(
'status' => 1,
'type' => 'All',
'title' => 'article',
'langcode' => 'All',
);
$options = array(
'fragment' => 'edit-modules-field-types',
'attributes' => ['class' => ['btn', 'btn-mini']],
'absolute' => TRUE,
);
$link = Link::fromTextAndUrl(t('Published contents'), Url::fromRoute('system.admin_content', $query, $options))->toString();
This code will generate HTML output of:
<a href="http://www.mywebsite.com/admin/content?status=1&type=article&title=&langcode=All#edit-modules-field-types" class="btn btn-mini">Published contents</a>
Comments
I have already changed the…
I have already changed the output of last example. Thank you for the correction.
How to add title attribute
How to add title attribute to the anchor link. It seems title attribute is not working inside fromUri
title attribute
In your options array have you tried this:
'attributes' => ['title' => t('Drupal')],
what in case of href="#"
How can we achieve this:
<a href="#">Your Text</a>
We can't leave the "fragment…
use Drupal\Core\Link;
use Drupal\Core\Url;
$options = array(
'fragment' => '.',
);
$link = Link::fromTextAndUrl(t('Your Text'), Url::fromUri('internal:', $options))->toString();
... will generate <a href="#.">Your Text</a>
.
Last Example
If I'm not mistaken the last example would actually output:
<a href="http://www.mywebsite.com/admin/content?status=1&type=article&title=&langcode=All#edit-modules-field-types" class="btn btn-mini">Published contents</a>