Drupal 7's l() and url() equivalent in Drupal 8 with query, fragment and attributes examples

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

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>

We can't leave the "fragment" empty in generating links in Drupal 8. Instead of empty, use period ".". The following codes:
  
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>.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.