欢迎各位兄弟 发布技术文章
这里的技术是共享的
浏览 5655 次
猪跑啦独家原创专稿,欢迎您转载本文,转载请注明来源。标签: drupal 8 模块开发
今天花了一点时间,眼睛了一下drupal 8的模块开发。并且分享一下作为例子。
在开发前,我们首先看看在drupal7 下的基本模块是怎么样的。
test.info
; $Id:$name = Testdescription = "Test Module"package = "Test"core = 7.xdependencies[] = nodedependencies[] = some_other_custom_modulefiles[] = test.module; |
test.module
<?php /** Implementation of hook_menu*/function test_menu() { $items = array(); $items['test/%'] = array( 'title' => t('Test'), 'description' => t('Test'), 'page callback' => '_test', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK ); return $items;}function _test($variable) { $output = $variable; return $output;}function test_block_info() { $block = array(); $block['test']['info'] = t('Test Block'); return $block;}function test_block_view($delta = '') { $block = array(); switch ($delta) { case 'test': $block['subject'] = t('Test block'); $block['content'] = t('Here is the content'); } return $block;}?> |
在hook_menu 中有看到'test/%',他是一个生成的地址。每个item下都有一个page callback,通过page callback返回数据到地址'test/%'里面显示。
如果有模块依赖,就在.info 里面增加 dependencies[] 就可以。
现在我们来看看drupal8的,首先drupal8没有 modulename.info 文件,而是改用了 modulename.info.yml 文件。
test.info.yml
name: Testdescription: Test modulecore: 8.xpackage: Testtype: moduledependencies - node - some_other_custom_module模块依赖的写法有所改变,只需要在前面打一个dependencies,紧接着每行代表一个依赖的模块,每行以 “-” 开头。
test.module
<?php/*** Implementation of hook_menu()*/function test_menu() { $items = array(); $items['test/{variable}'] = array( 'title' => 'Test', 'route_name' => 'test_page', 'type' => MENU_DEFAULT_LOCAL_TASK, ); return $items;}?>在drupal7中的百分号代表匹配符,而drupal8中取而代之的是大括号,
Drupal7 ---- “test/%”
Drupal8 ---- "test/{variable}"
还有是,不再需要用page callback返回,并且多了route_name,test/{variable} 会通过route_name返回。
所以我们要写一个route,文件名是modulename.routing.yml。并且需要定义一个Controller,Controller 是放在lib/Drupal/modulename/Controller/ 下, 如lib/Drupal/modulename/Controller/ControllerName.php
test.routing.yml
test_page: path: '/test/{variable}' defaults: _content: 'Drupal\test\Controller\TestController::testPage' requirements: _permission: 'access content' |
记得 routing_name => 'test_page' 是要跟 test.module 中hook_menu 里面 的route_name的值。
path: '/test/{variable}' 是访问的地址。
'_content' 是返回什么内容。
‘_permission’ 是设置访问权限。
最后我们还得创建一个 controller,创建文件夹test/lib/Drupal/test/Controller,并且在test/lib/Drupal/test/Controller里面增加我们的controller文件。我们这里命名为TestController.php
TestController.php
<?phpnamespace Drupal\test\Controller;class TestController { public function testPage($variable) { $build = array( '#type' => 'markup', '#markup' => t($variable), ); return $build; }}namespace 是命名空间引用。
class 是一个类,类名是TestController,它必须跟test.routing.yml 文件里面的_content: 'Drupal\test\Controller\TestController::testPage' 对应。
好了,后台开启模块,模块开启后,输入地址 http://yoursite/test/it-works, 系统会自动匹配$variable = (“it-works”),并且在页面打印出 “it-works”,
这就是一个简单的drupal8模块开发。
接下来我们来看看如何建立一个区块,在drupal 8里面,区块的定义略有变化,drupal 8把区块当成是一种Plugin方式引入,
方法如下:
先在test模块里面建立文件夹结构:test/lib/Drupal/test/Plugin/Block,
然后在test/lib/Drupal/test/Plugin/Block里面建立第一个block文件,
TestBlock.php
<?php/*** @file* Contains \Drupal\test\Plugin\Block\TestBlock*/namespace Drupal\test\Plugin\Block;use Drupal\block\BlockBase;use Drupal\block\Annotation\Block;use Drupal\Core\Annotation\Translation;/*** Provides a test block.** @Block(* id = "test_block",* admin_label = @Translation("Test Block")* )*/class TestBlock extends BlockBase { /** * {@inheritdoc} */ public function build() { $this->configuration['label'] = t('Test block'); return array( '#children' => t('Here is the content'), ); }}这也是一个类的方式,TestBlock继承BlockBase类,保存并清空缓存,在structure里面找到block 页,会看到我们新建的block,可以把它拉到某个区域看看效果。
整过模块开发过程与drupal7改变蛮大的,drupal8是基于symfony2的,主要是使用symfony2的方式,所以如果你对symfony2熟悉的话,这些很容易就明白了。