Forked from davidjguru/drupal_8_working_with_links_in_different_ways.txt
Created
January 25, 2021 19:21
-
-
Save gobinathm/151774f00d429182c9463b71684c96b4 to your computer and use it in GitHub Desktop.
Drupal 8: Working with links in different ways.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1- Basic. | |
$url1 = Url::fromUri('https:www.google.com'); | |
$link1 = Link::fromTextAndUrl('This is my link to Google', $url1); | |
// 2- Using classes URL and Link. | |
use Drupal\Core\Url; | |
use Drupal\Core\Link; | |
// 2.1- Link to your Drupal Site Home page (declared in the file 'system.routing.yml'). | |
$url1 = Url::fromRoute('<front>'); | |
$link1 = Link::fromTextAndUrl('This is my First link to Home Page', $url1); | |
//2.2- Link to /admin/people (declared in the file 'user.routing.yml'). | |
$url2 = Url::fromRoute('entity.user.collection'); | |
$link2 = Link::fromTextAndUrl('Link to the user admin page', $url2); | |
//2.3- External Link to Drupal.org. | |
$url3 = Url::fromUri('https://www.drupal.org'); | |
$link3 = Link::fromTextAndUrl('External link to Drupal.org', $url3); | |
//2.4- Link to /admin/structure/blocks created with text and Url. | |
$url4 = Url::fromRoute('block.admin_display'); | |
$link4 = Link::fromTextAndUrl(t('Block administration page'), $url4); | |
//2.5- Link to the node with id = 1. | |
$url5 = Url::fromRoute('entity.node.canonical', ['node' => 1]); | |
$link5 = Link::fromTextAndUrl(t('Go to node with id = 1'), $url5); | |
//2.6- Link to the edit mode of the node with id = 1. | |
$url6 = Url::fromRoute('entity.node.edit_form', ['node' => 1]); | |
$link6 = Link::fromTextAndUrl(t('Go to the edit mode'), $url6); | |
//2.7- External Link to Gitlab with attributes. | |
$url7 = Url::fromUri('https://gitlab.com'); | |
// We'll add some HTML attributes to the link. | |
$link_options = [ | |
'attributes' => [ | |
'target' => '_blank', | |
'title' => 'Link to Gitlab web site', | |
], | |
]; | |
$url7->setOptions($link_options); | |
$link7 = Link::fromTextAndUrl(t('Go to Gitlab web site'), $url7); | |
// 3- Using services with static methods from the \Drupal global class. | |
$url8 = Url::fromRoute('your_custom_module.what_a_great_route'); | |
$link8 = \Drupal::service('link_generator')->generate('My link created by static calling to service', $url8); | |
// 4- Using services in a dynamic way, passed as arguments to a constructor. | |
<?php | |
/** | |
* @file | |
* Contains \Drupal\your_custom_module\Controller\YourCustomController. | |
*/ | |
namespace Drupal\your_custom_module\Controller; | |
use Drupal\Core\Utility\LinkGeneratorInterface; | |
class YourCustomController extends ControllerBase { | |
protected $link_generator; | |
public function __construct(LinkGeneratorInterface $linkGenerator) { | |
$this->link_generator = $linkGenerator; | |
} | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('link_generator') | |
); | |
} | |
public function link() { | |
// Add a link created dynamically with the 'link_generator' service. | |
$url9 = Url::fromUri('https://twitter.com'); | |
$link9 = $this->link_generator->generate('Link to twitter', $url9); | |
// Build the render output. | |
$output['your_custom_module_links'] = [ | |
'#theme' => 'item_list', | |
'#items' => $link9, | |
'#title' => $this->t('Just a link generated with injected service: '), | |
]; | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment