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的配置。
现在通过drupal 8后台增加一个image style,你会发现多了一个配置文件。
如果我们把这个配置文件拷贝到其他drupal8站点的话,同样的,在drupal8后台也能看到新加的image style
2,模板引擎
Drupal 7 模板引擎:PHP Template
.tpl.php
<?php if ($content): ?> <div class=”<?php print $classes; ?>”> <?php print $content; ?> </div> <?php endif; ?>
Drupal 8 模板引擎:Twig
.twig
{% if content %} <div {{ attributes }}> {{ contents }} </div> {% 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
<?php /** * @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
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', )); } }
Hook 跟 Plugin比较: