Thiết kế web
Tin tức

Giới thiệu tổng quan Drupal 8 services năm 2015

Từ tháng 7 năm 2014, phiên bản Drupal 8 đã xuất hiện với bộ Core Drupal bổ trợ trên 25 Services.
Một Service đơn giản như User.data. Nó cho phép tài khoản người dùng có thể lưu trữ, sửa, xóa dữ liệu trên một giao diện được gọi là UserDataInterface.
Để thực hiện điều đó chúng ta sẽ add thêm các dịch vụ mới thuộc các thành phần của UserDataInterface.
Trước tiên ta tạo một Thư mục cho module của mình như: alternative_userdata và đặt tên alternative_userdata.info.yml sau đó chúng ta có thể định nghĩa thông tin của module này như sau:

name: Alternative UserData
type: module
description: Adds alternative storage for user data.
core: 8.x
dependencies:
- user

Vậy là chúng ta đã thiết lập tên cho module, loại module, mô tả cho module người dùng.
Sau đó chúng ta cần định nghĩa dịch vụ mà chúng ta sẽ sử dụng, để làm điều này chúng ta tạo 1 file có tên là alternative_userdata.services.yml. và trong file này chúng ta cần add service để gọi class trong alternative_userdata.user.data

services:
alternative_userdata.user.data:
class: Drupal\alternative_userdata\AlternativeUserData

Tiếp theo là ta tạo các class trong AlternativeUserData.php và thêm thư mục được đặt tên là "src"

<?php
/**
* @file
 * Contains \Drupal\alternative_userdata\AlternativeUserData.
 */
namespace Drupal\alternative_userdata;
// Here you may need to add the use operator to pull in any backend client.
 /**
* Defines the alternative user data service.
*/
class AlternativeUserData implements UserDataInterface {
 // You may need to add a protected variable here for your backend client.
 /**
* Constructs a new user data service.
 */ public function __construct() {
// Here you can create a new instance of your backend client.
 }
/**
* Implements \Drupal\user\UserDataInterface::get().
*/
public function get($module, $uid = NULL, $name = NULL) {
 // This needs to return the user data from your backend. }
 /**
* Implements \Drupal\user\UserDataInterface::set().
 */ public function set($module, $uid, $name, $value) {
// This needs to save the user data to your backend.
}
 /**
* Implements \Drupal\user\UserDataInterface::delete().
*/
 public function delete($module = NULL, $uid = NULL, $name = NULL) {
 // This needs to save your user data to your backend.
 }
 }

Sau khi ta có một service được định nghĩa tron module, và chúng ta hoàn toàn có thể kích hoạt nó, nhưng khi bạn kích hoạt rồi nó cũng sẽ chưa thực hiện bất cứ lệnh nào cho đến khi ta ánh xạ nó tới file services.yml của site.
Chỉnh sửa file services.yml thường nó nằm ở thư mục sites/default/services.yml và add thêm:

services:
user.data:
alias: alternative_userdata.user.data

Khi Drupal sử dụng user.data nó sẽ sử dụng service alternative_userdata.user.data từ module bạn tạo
Chẳng đơn giản chút nào phải không các bạn?

Đánh giá của khách hàng
  • Sản phẩm Online Marketing của 3S Group mang lại hiệu quả lớn cho kế hoạch kinh doanh của GELEXIMCO .. 

    Mr. Lê Quốc Tuấn Tập đoàn GELEXIMCO

Follow me

Thiết kế website Facebook

Thiết kế website You Tube

Thiết kế website Google+

 

 

3S TECHNOLOGY., JSC | Tầng 6, tòa nhà 88 Tô Vĩnh Diện, Thanh Xuân, Hà Nội | Điện thoại: 024 625 91 969 *  Hotline: 0985 348 635  | Email: info@3sgroup.vn