Create a link in Drupal 8

Public

</> CopyGet raw version
php
  1. // Internal path (defined by a route in Drupal 8).
  2. use Drupal\Core\Url;
  3. $url = Url::fromRoute('book.admin');
  4. $internal_link = \Drupal::l(t('Book admin'), $url);
  5.  
  6. // External Uri.
  7. use Drupal\Core\Url;
  8. $url = Url::fromUri('http://www.example.com/');
  9. $external_link = \Drupal::l(t('External link'), $url);
  10.  
  11. ==================================or==================================================
  12. use Drupal\Core\Url;
  13. use Drupal\Core\Link;
  14. $url = Url::fromRoute('entity.node.edit_form', array('node' => NID));
  15. $project_link = Link::fromTextAndUrl(t('Open Project'), $url);
  16. $project_link = $project_link->toRenderable();
  17. // If you need some attributes.
  18. $project_link['#attributes'] = array('class' => array('button', 'button-action', 'button--primary', 'button--small'));
  19. print render($project_link);