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

 
Hướng dẫn MixItUp and Drupal 7 Views

Hướng dẫn MixItUp and Drupal 7 Views

Recently I had to create a View with exposed filters that refine the results by certain taxonomy terms. In the meantime, the design team came across the MixItUp JQuery library

Cài đặt Apache, MySQL, phpMyAdmin trên VPS CentOS

Cài đặt Apache, MySQL, phpMyAdmin trên VPS CentOS

Đăng nhập vào VPS sử dụng quyền root.

Hướng dẫn phân quyền trong Wordpress 3.4

Hướng dẫn phân quyền trong Wordpress 3.4

Wordpress thực sự mạnh mẽ cho cộng đồng bloger chuyên nghiệp, với những ưu điểm riêng biệt làm cho Wordpress ngày càng lớn mạnh và trở nên phổ biến. Bài viết này sẽ giới thiệu về các loại tài khoản dùng để phần quyền trong việc quản lý nội dung trong Wordpress, hiện tại Wordpress có 4 loại tài khoản chính là Admin, Editer, Author, Contributer, và Subsrciber

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

 

Diet con trung