1

So I have the following code in my Drupal 7 module:

$uri = entity_uri('node', $node);
$breadcrumb[] = l($node->title, $uri['path'], $uri['options']);

Anyone know how to convert the above code to Drupal 8?

Since l() is depreciated, I believe the second line should be replaced as follows:

$breadcrumb = array(Link::createFromRoute($node->title, $uri['path'], $uri['options']));

I'm struggling with what to do with entity_uri() since the change records don't really indicate how that function is to be replaced.

Note: this is being used within the BreadcrumbBuilderInterface class.

1 Answer 1

2

Try with this code:

$link = $node->toLink($node->getTitle(), 'canonical', $options);

Get more information about Node object here: "Node Object"

5
  • The entity_uri() function provides the $options, but where would I get those in the above example? Also looks like I may want to use toLink() instead as Entity::link() is depreciated.
    – Webdrips
    Commented Mar 17, 2016 at 7:54
  • toLink gives you an Link object, that has the options included.
    – Berdir
    Commented Mar 17, 2016 at 8:03
  • Yes please change the answer to the following to get credit: $link = $node->toLink($node->getTitle(), 'canonical', $options);
    – Webdrips
    Commented Mar 19, 2016 at 0:37
  • Yes please change the answer to the following to get credit: $link = $node->toLink($node->getTitle(), 'canonical', $options); Part of the reason for this is the BreadcrumbBuilderInterface expects a Drupal\Core\Link object.
    – Webdrips
    Commented Mar 19, 2016 at 0:43
  • Get more information about Node object here: "Node Object" - This leads to a page with no helpful information. This should be either provided with correct link or should be removed. Commented Aug 28, 2017 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.