drupal 8 php filter,Drupal 7 与 Drupal 8 的一些变化

1,Settings, States, ConfigEntity

Drupal 7:保存配置

// mymodule/mymodule.module

variable_set("mymodule_results", 5);

Drupal 8:保存配置

// mymodule/mymodule.module

config('mymodule.config')->set('results', 5)->save();// mymodule/config/mymodule.config.yml

myvalue: 5

Drupal 7:保存状态

// mymodule/mymodule.module

variable_set("mymodule_last_update", 12345678);

Drupal 8:保存状态

// mymodule/mymodule.module

state()->set('mymodule_last_update', 12345678);

Drupal 7:设定

Drupal 7的设定是放在数据库

Drupal 8 设定是基于ConfigEntity,举例图像样式,image style。

在sites/default/files/config/activeXXX能看到image style的配置。

c5f18bda3f6d759bf5683eeca6ed53f4.png

现在通过drupal 8后台增加一个image style,你会发现多了一个配置文件。

e1fa0d4aaf870e9335cbbe4a92486731.png

如果我们把这个配置文件拷贝到其他drupal8站点的话,同样的,在drupal8后台也能看到新加的image style

27247d9d2596febe072c28a512637584.png

2,模板引擎

Drupal 7 模板引擎:PHP Template

.tpl.php

”>

Drupal 8 模板引擎:Twig

.twig

{% if content %}

{{ contents }}

{% endif %}

3,多语言

字段多语言:

– D7

// Determine the $active_langcode somehow.

$field = field_info_field('field_foo');

$langcode = field_is_translatable($entity_type, $field) ?

$active_langcode : LANGUAGE_NONE;

$value = $entity->field_foo[$langcode][0]['value'];

– D8

// Determine the $active_langcode somehow.

$translation = $entity->getTranslation($active_langcode);

$value = $translation->field_foo->value;

实体多语言:

– D7

function entity_do_stuff($entity, $langcode = NULL) {

if (!isset($langcode)) {

$langcode = $GLOBALS['language_content']->language;

}

$field = field_info_field('field_foo');

$langcode = field_is_translatable($entity_type, $field) ?

$langcode : LANGUAGE_NONE;

if (!empty($entity->field_foo[$langcode])) {

$value = $entity->field_foo[$langcode][0]['value'];

// do stuff

}

}

– D8

$langcode = Drupal::languageManager()

->getLanguage(Language::TYPE_CONTENT);

$translation = $entity->getTranslation($langcode);

entity_do_stuff($translation);

function entity_do_stuff(EntityInterface $entity) {

$value = $entity->field_foo->value;

$langcode = $entity->language()->id;

// do stuff

}

判断当前语言:

– D7

function field_attach_view($entity_type, $entity,

$view_mode, $langcode = NULL, $options = array()) {

$display_language = field_language($entity_type, $entity,

NULL, $langcode);

$options['language'] = $display_language;

$null = NULL;

$output = _field_invoke_default('view', $entity_type,

$entity, $view_mode, $null, $options);

return $output;

}

– D8

public function viewEntity(EntityInterface $entity,

$view_mode = 'full', $langcode = NULL) {

$langcode = NULL; // Defaults to the current language

$translation = $this->entityManager

->getTranslationFromContext($entity, $langcode);

$build = entity_do_stuff($translation, 'full');

return $build;

}

4,Hook 跟 Plugin

– 7.x: hook_image_toolkits()

/**

* Implements hook_image_toolkits().

*/

function system_image_toolkits() {

include_onceDRUPAL_ROOT . '/' . drupal_get_path('module',

'system') . '/' . 'image.gd.inc';

return array(

'gd' => array(

'title' => t('GD2 image manipulation toolkit'),

'available' => function_exists('image_gd_check_settings') &&

image_gd_check_settings(),

),

);

}

– 8.x: ImageToolkitManager

classImageToolkitManager extendsDefaultPluginManager {

// ... various methods ... //

/**

* Gets a list of available toolkits.

*/

public function getAvailableToolkits() {

// Use plugin system to get list of available toolkits.

$toolkits = $this->getDefinitions();

$output = array();

foreach($toolkits as $id => $definition) {

if(call_user_func($definition['class'] . '::isAvailable')) {

$output[$id] = $definition;

}

}

return $output;

}

}

– 7.x: 降低饱和度函数

/**

* Converts an image to grayscale.

*

* @param$image

* An image object returned by image_load().

*

* @return

* TRUE on success, FALSE on failure.

*

* @seeimage_load()

* @seeimage_gd_desaturate()

*/

function image_desaturate(stdClass $image) {

return image_toolkit_invoke('desaturate', $image);

}

– 8.x: 降低饱和度方法

/**

* Defines the GD2 toolkit for image manipulation within Drupal.

*

* @ImageToolkit(

* id = "gd",

* title = @Translation("GD2 image manipulation toolkit")

* )

*/

classGDToolkit extendsPluginBase implementsImageToolkitInterface {

// ... all the toolkit methods ... //

public function desaturate(ImageInterface $image) {

// PHP using non-bundled GD does not have imagefilter.

if(!function_exists('imagefilter')) {

return FALSE;

}

return imagefilter($image->getResource(),IMG_FILTER_GRAYSCALE);

}

}

5,模块

– 8.x

modules/mymodule/mymodule.info.yml

name: 'My test module'

type: module

description: 'Drupalcon demo.'

core: 8.x

modules/mymodule/mymodule.module

/**

* @file

* Drupalcon demo module.

*/

增加两个tabs:

不需要像Drupal7一样需要改变 default local task,而是只需要增加一个yaml 文件,tabs: mymodule/mymodule.local_tasks.yml

mymodule_list_tab:

route_name: mymodule.list

title: 'List'

tab_root_id: mymodule_list_tab

mymodule_settings_tab:

route_name: mymodule.settings

title: 'Settings'

tab_root_id: mymodule_list_tab

397494d5094a5e54bfef5217bad342f2.png

Drupal 7通过hook menu 定义地址,Drupal 8改为Routes

mymodule/mymodule.routing.yml

mymodule.list:

path: '/admin/config/mymodule/list'

defaults:

_content: '\Drupal\mymodule\Controller\MyController::dolist'

_title: 'Mymodule list'

requirements:

_access: 'TRUE'

mymodule.settings:

path: '/admin/config/also-mymodule/settings'

defaults:

_content: '\Drupal\mymodule\Controller\MyController::settings'

_title: 'Mymodule settings'

requirements:

_access: 'TRUE'

6,区块 block

Drupal7通过hook_block 系列钩子新建block, drupal 8将block 作为插件形式,每个自定义的block 通过一个class定义。

/**

* Provides a block with 'Mymodule' links.

*

* @Block(

* id = "mymodule_my_block",

* admin_label = @Translation("Mymodule block")

* )

*/

classMyBlock extendsBlockBase {

public function build() {

return array(

'first_link' => array(

'#type' => 'link',

'#title' => $this->t('Mymodule List'),

'#route_name' => 'mymodule.list',

),

'second_link' => array(

'#type' => 'link',

'#title' => $this->t('Mymodule Settings'),

'#route_name' => 'mymodule.settings',

));

}

}

34e3e172d3eaceb382284cb653b8ade8.png

20130512100553.jpg

378f6b10af5ab46cc956acebd50c14db.png

Hook 跟 Plugin比较:

568910bd689dbccfe364b43d38267ffd.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值