Thêm classes vào links trong Drupal 8

Thêm classes vào links trong Drupal 8

As I start porting the modules I maintain(link is external) to Drupal 8, I'm hitting a few places where things haven't been intuitive to me. I'll try to work on the drupal.org documentation when I get a chance, but in the meantime I figured it would be worth writing up a few notes.

>> Giới thiệu Drupal Site là Platform khá tốt

>> Hướng dẫn Drupal 7 Migration thành Drupal 8

A common task is creating a link, and adding classes and other attributes to it. The Responsive Share Buttons(link is external) is basically just a block of links to social networks, so this was a key building block.

In Drupal 7 this was pretty simple - the link building function(link is external) took three arguments - a title, a path, and an array of options:

$link = l(t('Link Title'), 'http://drupal.org', array(
  'attributes' => array(
    'class' => array(
      'my-first-class',
      'my-second-class',
    ),
  ),
));

In Drupal 8, the l function(link is external) now takes a Url object with attributes, rather than a string, so it's a little different. Here's how to build a link to an external URL and add a class to it: First the Url class needs to be brought into scope:

use Drupal\Core\Url;

And then you can build the Url object and call setOptions on it:

$url = Url::fromUri('http://drupal.org');
$link_options = array(
  'attributes' => array(
    'class' => array(
      'my-first-class',
      'my-second-class',
    ),
  ),
);
$url->setOptions($link_options);
$link = \Drupal::l(t('Link title'), $url);

Incidentally, the other gotcha here that had me scratching my head for a while was how to get the current page title, and how to get the current URL. Drupal 7 had easily accessible functions for these tasks, but the object-oriented approach

Drupal 7

$title = drupal_get_title();
$current_url = url(current_path(), array('absolute' => TRUE));

Drupal 8

$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
$current_url = $request->getUri();

My own learning journey with Drupal 8 is very much in its early days, and a lot of the old Drupalisms are pretty familiar to me, but it does seem a little long-winded. Give it a while, and I'm sure I'll get up to speed, and start seeing the benefits of the object-oriented approach in Drupal 8.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Công cụ  để Optimize Drupal Site Performance

Công cụ để Optimize Drupal Site Performance

You don't need to rely only on performance-related modules within Drupal

Drupal Developer

Những công cụ cho Drupal Developer

Drupal là nền tảng cực kỳ tốt để bạn có thể dựa vào đó và phát triển các ứng dụng web của mình. Tuy nhiên, để trở thành một drupal developer giỏi, bạn cần nhiều thời gian học hỏi và không sử dụng thành thạo các công cụ hỗ trơ. Xin giới thiệu với bạn những công cụ mà chúng tôi biết được trong thời gian dài sử dụng Drupal.

Sự khác nhau Route, Path, URL, URI, Link, Menu item trong Drupal 8

Sự khác nhau Route, Path, URL, URI, Link, Menu item trong Drupal 8

A nice article from KatteKrab (Donna Benjamin) covering terms in the Drupal context. These kinds of post are very handy indeed.

Công ty diệt chuột T&C

 

Diet con trung