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

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

Since July 2014 there’s been a feature in Drupal 8 to override backend specific services. There are over 25 services in Drupal core that are backend overridable.

In this post we’ll look at a simple way to override services using aliases, which comes downstream fro symfony.

Drupal 8

A simple service is the “user.data” service. It allows data to be stored, fetched and deleted, relating to a user account. For this there is an interface, UserDataInterface and this is implemented by UserData. We need to create a module that adds a new service that implements UserDataInterface.

Firstly create a folder for your module, something like alternative_userdata, then the file alternative_userdata.info.yml, in there we can define the module’s info:

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

dependencies:
  - user

So we are setting the name of the module, the type, a description of it the core version and that it depends on the user module.

Next we need to define our service, for this you wil need to create the file alternative_userdata.services.yml. In here we can add the service called alternative_userdata.user.dataand set the class.

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

Next is to create the class, add a folder named “src” (following PSR-4 autoloading standards) and within this a file called AlternativeUserData.php.

<?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.
  }

}

Now that we have a service defined in our module, the module can be enabled, however it won’t do anything until we set an alias in your site’s services.yml file.

Edit services.yml (normally at sites/default/services.yml in your Drupal 8 codebase) and add the following:

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

Now when Drupal looks to use the user.data service it will actually use the alternative_userdata.user.data service from your module.

Simple, right?

Bạn thấy bài viết này như thế nào?: 
Average: 5 (2 votes)
Ả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 seo hình ảnh cho website của bạn

Hướng dẫn seo hình ảnh cho website của bạn

Nhiều vẫn còn thắc mắc là làm sao để có hình ảnh xuất hiện trên google khi search một  từ khóa nào đó. SEO123 sẽ hướng dẫn các bạn cách SEO hình ảnh cho website để xuất hiện hình ảnh lên trang đầu trong kết quả tìm kiếm.

Cách khắc phục nếu Chrome hoặc Firefox bị treo liên tục

Cách khắc phục nếu Chrome hoặc Firefox bị treo liên tục

Chắc hẳn trong quá trình sử dụng Firefox/Chrome, bạn từng gặp phải hiện tượng trình duyệt bị đứng và ngừng hoạt động. Những thủ thuật nhỏ sau đây sẽ giúp bạn khắc phục điều đó.

Hướng dẫn tính toán exposing REST API in Drupal 8 mà không code

Hướng dẫn tính toán exposing REST API in Drupal 8 mà không code

The "processed" property mentioned above is what is known as a computed property on the textarea field.

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

 

Diet con trung