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

 
4 cách tránh mỏi mắt khi làm việc với màn hình máy vi tính

4 cách tránh mỏi mắt khi làm việc với màn hình máy vi tính

Ngồi lâu trước màn hình máy vi tính dễ khiến mắt bạn mỏi nhừ, dẫn đến nguy cơ bị tật về mắt. Dưới đây là 5 công cụ giúp bạn giảm thiểu nguy cơ đó.

Google Panda - Thuật toán cho thứ hạng thật?

Google Panda - Thuật toán cho thứ hạng thật?

Mới đây, Google đã bản cập nhật một thành phần quan trọng về cách xếp thứ hạng các trang web đã ảnh hưởng tới 12% kết quả tìm kiếm và làm giảm số lượng truy cập của rất nhiều trang.

Tim Cook mới chỉ làm được một vài thay đổi rất nhỏ.

Apple đã thay đổi gì dưới thời Tim Cook?

Theo tờ WSJ, kể từ khi lên làm Giám đốc điều hành Apple vào tháng 8 thay cho “cố nhân” Steve Jobs tới giờ, Tim Cook mới chỉ làm được một vài thay đổi rất nhỏ.

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

 

Diet con trung