Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a link from node ID in Drupal 8

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.

like image 263
Nicensin Avatar asked Feb 14 '16 19:02

Nicensin


People also ask

How do I create a link in Drupal 8?

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.

How do I find my node ID in Drupal 8?

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... }

How to get the URL of a node entity in Drupal 8?

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.

How to create links when using Drupal 8?

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:

How do I link an entity to a route in Drupal?

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.

How to get the route of a specific path in Drupal?

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.


Video Answer


3 Answers

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).

like image 121
Pierre Buyle Avatar answered Oct 30 '22 15:10

Pierre Buyle


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>
like image 31
leymannx Avatar answered Oct 30 '22 14:10

leymannx


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));
like image 41
ya.teck Avatar answered Oct 30 '22 15:10

ya.teck