Skip to content

Instantly share code, notes, and snippets.

@robdecker
Created October 31, 2019 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robdecker/b75c8b019f40d3b8ebc59c18e7d8c732 to your computer and use it in GitHub Desktop.
Save robdecker/b75c8b019f40d3b8ebc59c18e7d8c732 to your computer and use it in GitHub Desktop.
[Links] #d8

From How to add classes to links in Drupal 8

$url = Url::fromUri('http://drupal.org');
$link_options = array(
  'attributes' => array(
    'class' => array(
      'my-first-class',
      'my-second-class',
    ),
  ),
);
$url->setOptions($link_options);
$link = \Drupal::l(t('Link title'), $url);

note:

l() is deprecated, so use Link::fromTextAndUrl(); instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment