I am checking out Drupal 8 and try to generate a link based on the node ID of an article. In Drupal 7 it is something like:
$options = array('absolute' => TRUE);
$nid = 1; // Node ID
$url = url('node/' . $nid, $options);
This results into an absolute link with the correct url-alias.
So the url()-function seems to be deprecated; what is the Drupal-8 way? Looks like a really basic function for me but I could not find any useful references.
Add links inside a t() method. If you want to add a link inside the t() you can do what Drupal suggests on Dynamic or static links and HTML in translatable strings: use Drupal\Core\Url; $url = Url::fromRoute('entity.
In Drupal 8 and onwards things are done differently. Consider the following: $node = \Drupal::routeMatch()->getParameter('node'); if ($node instanceof \Drupal\node\NodeInterface) { $nid = $node->id(); // Do whatever you need to do with the node ID here... }
Drupal 8 uses routes which have name different from their actual URL path. In your case, the route to use is the canonical route for a node entity: entity.node.canonical. \Drupal\Core\Url::fromRoute () will not return a string, but an object. To get the URL as a string, you need to call its toString () method.
By CodimTh There are plenty of ways to create links when using Drupal 8 and I will share some of those ways in this post. Using the Url object gives you more flexibility to create links, for instance, we can do the same as Link::createFromRoute method using the Url object like this:
Linking entities. And even using the route: Drupal usually expects a render array if you are going to print the link, so the Link object has a method for that: which will return an array. The easiest way to find the route of a specific path is using Drupal Console, with the following command.
And even using the route: Drupal usually expects a render array if you are going to print the link, so the Link object has a method for that: which will return an array. The easiest way to find the route of a specific path is using Drupal Console, with the following command.
You need to use the \Drupal\Core\Url
class, specifically its fromRoute
static method. Drupal 8 uses routes which have name different from their actual URL path. In your case, the route to use is the canonical route for a node entity: entity.node.canonical
. \Drupal\Core\Url::fromRoute()
will not return a string, but an object. To get the URL as a string, you need to call its toString()
method.
$options = ['absolute' => TRUE];
$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => 1], $options);
$url = $url->toString();
The static method is not easily testable, $url->toString()
require an initialized container. Your can replace the static method with a call to UrlGeneratorInterface::generateFromRoute()
on the url_generator
service.
$options = ['absolute' => TRUE];
$url = $this->url_generator->generateFromRoute('entity.node.canonical', ['node' => 1], $options);
$url = $url->toString();
Unfortunately, this method is marked as @internal
so you are not supposed to use it in user code (ie. outside Drupal core).
Create absolute URL:
$options = ['absolute' => TRUE];
$url_object = Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
// will output http://example.com/path-to-my-node
Create absolute link object:
$options = ['absolute' => TRUE, 'attributes' => ['class' => 'this-class']];
$node_title = Drupal\Core\Render\Markup::create('<span>' . $node_title . '</span>');
$link_object = Drupal\Core\Link::createFromRoute($node_title, 'entity.node.canonical', ['node' => $nid], $options);
// will output <a href="http://example.com/path-to-my-node" class="this-class"><span>My Node's Title</span></a>
In case you have fully loaded node object you can simply call $node->toUrl()
to get required URL. By default it returns canonical URL for a node (node/NID) but it is possible to build other URLs listed in Node entity definition (see Drupal\node\Entity\Node.php).
Same is true for other entity types.
$options = ['absolute' => TRUE];
$view_link = Link::fromTextAndUrl(t('View'), $node->toUrl('canonical', $options));
$edit_link = Link::fromTextAndUrl(t('Edit'), $node->toUrl('edit-form', $options));
$delete_link = Link::fromTextAndUrl(t('Delete'), $node->toUrl('delete-form', $options));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With