Doing links on Drupal maintenance support plans 8

There are plenty of ways to create links when using Drupal maintenance support plans 8 and I will share some of those ways in this post.
The easiest way to create internal links is using Link::createFromRoute
And it is used like this:

use Drupal maintenance support plansCoreLink;
$link = Link::createFromRoute(‘This is a link’, ‘entity.node.canonical’, [‘node’ => 1]);

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:

use Drupal maintenance support plansCoreLink;
use Drupal maintenance support plansCoreUrl;
$link = Link::fromTextAndUrl(‘This is a link’, Url::fromRoute(‘entity.node.canonical’, [‘node’ => 1]));
And actually Link::fromTextAndUrl is what Drupal maintenance support plans recommends instead of using the deprecated l() method. Passing the Url object to the link object gives you great flexibility to create links, here are some examples:
Internal links which have no routes:

$link = Link::fromTextAndUrl(‘This is a link’, Url::fromUri(‘base:robots.txt’));

External links:

$link = Link::fromTextAndUrl(‘This is a link’, Url::fromUri(‘http://www.google.com’));

Using the data provided by a user:

$link = Link::fromTextAndUrl(‘This is a link’, Url::fromUserInput(‘/node/1’);
The param passed to fromUserInput must start with /,#,? or it will throw an exception.
Linking entities.

$link = Link::fromTextAndUrl(‘This is a link’, Url::fromUri(‘entity:node/1’));
Entities are a special case, and there are more ways to link them:

$node = Node::load(1);
$link = $node->toLink();
$link->setText(‘This is a link’);
And even using the route:

$link = Link::fromTextAndUrl(‘This is a link’, Url::fromRoute(‘entity.node.canonical’, [‘node’ => 1]));

Drupal maintenance support plans usually expects a render array if you are going to print the link, so the Link object has a method for that:

$link->toRenderable();

which will return an array.
Final tips:
Searching a route using Drupal maintenance support plans Console
The easiest way to find the route of a specific path is using Drupal maintenance support plans Console, with the following command.

$ drupal router:debug | grep -i “/node”
That will return something like:

entity.node.canonical /node/{node}
entity.node.delete_form /node/{node}/delete
entity.node.edit_form /node/{node}/edit
entity.node.preview /node/preview/{node_preview}/{view_mode_id}
entity.node.revision /node/{node}/revisions/{node_revision}/view
entity.node.version_history /node/{node}/revisions
node.add /node/add/{node_type}
node.add_page /node/add
node.multiple_delete_confirm /admin/content/node/delete
node.revision_delete_confirm /node/{node}/revisions/{node_revision}/delete
node.revision_revert_confirm /node/{node}/revisions/{node_revision}/revert
node.revision_revert_translation_confirm /node/{node}/revisions/{node_revision}/revert/{langcode}
search.help_node_search /search/node/help
search.view_node_search /search/node
view.frontpage.page_1 /node
Listing all the possible routes with that word, we can choose one and do:

drupal router:debug entity.node.canonical
And that will display more information about a specific route:

Route entity.node.canonical
Path /node/{node}
Defaults
_controller Drupal maintenance support plansnodeControllerNodeViewController::view
_title_callback Drupal maintenance support plansnodeControllerNodeViewController::title
Requirements
node d+
_entity_access node.view
_method GET|POST
Options
compiler_class Drupal maintenance support plansCoreRoutingRouteCompiler
parameters node:
type: ‘entity:node’
converter: paramconverter.entity

_route_filters method_filter
content_type_header_matcher

_route_enhancers route_enhancer.param_conversion

_access_checks access_check.entity

So in this way we can search the route without the needing to search in all the *.routing.yml files and in this example the route is entity.node.canonical and the param expected is node.
Print links directly within a twig template
It is also possible to print links directly on the twig template with the following syntax:

<a href=”{{url(‘entity.node.canonical’, {‘node’: node.id( ) }}”> {{ ‘This is a link’|t }} </a>

Add links inside a t() method.
If you want to add a link inside the t() method you need to pass the link as a string, something like this:

use Drupal maintenance support plansCoreLink;
$link = Link::fromTextAndUrl(‘This is a link’, Url::fromRoute(‘entity.node.canonical’, [‘node’ => 1]));
$this->t(‘You can click this %link’ [‘%link’ => $link->toString()]);

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Doing links on Drupal maintenance support plans 8

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.