drupal模块
Please be aware that due to the development process Drupal 8 has been undergoing at the time of writing, some parts of the code might be outdated. Take a look at this repository in which I try to update the example code and make it work with the latest Drupal 8 release.
请注意,由于在编写本文时Drupal 8正在进行开发过程,因此某些代码部分可能已过时。 看看这个存储库 ,我尝试在其中更新示例代码并使之与最新的Drupal 8版本一起使用。
Drupal 8 brings about a lot of changes that seek to enroll it in the same club other modern PHP frameworks belong to. This means the old PHP 4 style procedural programming is heavily replaced with an object oriented architecture. To achieve this, under the initiative of Proudly Found Elsewhere, Drupal 8 includes code not developed specifically for Drupal.
Drupal 8带来了许多变化,试图将其注册到其他现代PHP框架所属的俱乐部。 这意味着旧PHP 4风格的过程编程已被面向对象的体系结构大量替代。 为此,在Proudly Found Elsewhere的倡议下,Drupal 8包含了不是专门为Drupal开发的代码。
One of the most important additions to Drupal are Symfony components, with 2 major implications for Drupal developers. First, it has the potential to greatly increase the number of devs that will now want to develop for Drupal. And second, it gives quite a scare to some of the current Drupal 7 developers who do not have much experience with modern PHP practices. But that’s ok, we all learn, and lessons taken from frameworks like Symfony (and hopefully Drupal 8), will be easily extensible and applicable to other PHP frameworks out there.
Drupal最重要的补充之一是Symfony组件 ,对Drupal开发人员有2个主要意义。 首先,它有潜力极大地增加现在想要为Drupal开发的开发人员的数量。 其次,它给目前没有太多现代PHP实践经验的Drupal 7开发人员带来了很大的恐惧。 但这没关系,我们都可以学习,并且从Symfony(以及希望的Drupal 8)等框架中吸取的教训将很容易扩展,并适用于其他PHP框架。
In the meantime, Drupal 8 is in a late stage of its release cycle, the current version at the time of writing being alpha11
. We will use this version to show some of the basic changes to module development Drupal 7 devs will first encounter and should get familiar with. I set up a Git repo where you can find the code I write in this series so you can follow along like that if you want.
同时,Drupal 8处于其发布周期的后期,撰写本文时的当前版本为alpha11
。 我们将使用该版本来展示Drupal 7开发人员将首先遇到并应该熟悉的模块开发的一些基本更改。 我设置了一个Git存储库 ,您可以在其中找到我在本系列中编写的代码,以便您可以按照自己的意愿进行操作。
The first thing we are going to look at is defining the necessary files and folder structure to tell Drupal 8 about our new module. In Drupal 7 we had to create at least 2 files (.info
and .module
), but in Drupal 8, the YAML version of the former is enough. And yes, .info
files are now replaced with .info.yml
files and contain similar data but structured differently.
我们要看的第一件事是定义必要的文件和文件夹结构,以向Drupal 8介绍我们的新模块。 在Drupal 7中,我们必须至少创建2个文件( .info
和.module
),但是在Drupal 8中,前者的YAML版本就足够了。 是的, .info
文件现在已替换为.info.yml
文件,并且包含相似的数据,但结构有所不同。
Another major change is that custom and contrib module folders now go straight into the root modules/
folder. This is because all of the core code has been moved into a separate core/
folder of its own. Of course, within the modules/
directory, you are encouraged to separate modules between custom and contrib like in Drupal 7.
另一个主要变化是,定制和贡献模块文件夹现在直接进入根modules/
文件夹。 这是因为所有核心代码均已移至其自己的单独core/
文件夹中。 当然,在modules/
目录中,鼓励您像在Drupal 7中一样在custom和contrib之间分隔模块。
Let’s go ahead and create a module called demo
(very original) and place it in the modules/custom/
directory. And as I mentioned, inside of this newly created demo/
folder, all we need to begin with is a demo.info.yml
file with the following required content:
让我们继续创建一个名为demo
(非常原始)的modules/custom/
,并将其放置在modules/custom/
目录中。 正如我提到的,在这个新创建的demo/
文件夹中,我们首先需要一个demo.info.yml
文件,其中包含以下必需内容:
name: Drupal 8 Demo module
description: 'Demo module for Drupal 8 alpha11'
type: module
core: 8.x
Three out of four you should be familiar with (name, description and core). The type
is now also a requirement as you can have yml files for themes as well. Another important thing to keep in mind is that white spaces in yml files mean something and proper indentation is used to organize data in array-like structures.
您应该熟悉的四分之三(名称,描述和核心)。 现在也需要该type
因为您也可以具有主题的yml文件。 要记住的另一件重要事情是,yml文件中的空格表示某些含义,并且适当的缩进用于将数据整理成数组状结构。
You can check out this documentation page for other key|value pairs that can go into a module .info.yml
file and the change notice that announced the switch to this format.
您可以在此文档页面中查找可放入模块.info.yml
文件中的其他键对和声明已切换为该格式的更改通知 。
And that’s it, one file. You can now navigate to the Extend page, find the Demo module and enable it.
就是一个文件。 现在,您可以导航到“ 扩展”页面,找到“演示”模块并启用它。
As I mentioned, we are no longer required to create a .module
file before we can enable the module. And architecturally speaking, the .module
files will be significantly reduced in size due to most of the business logic moving to service classes, controllers and plugins, but we’ll see some of that later.
如前所述,在启用模块之前,不再需要创建.module
文件。 从体系结构上讲,由于大多数业务逻辑都转移到了服务类,控制器和插件, .module
文件的大小将大大减少,但是稍后我们将介绍其中的一些内容。
In Drupal 7, hook_menu()
was probably the most implemented hook because it was used to define paths to Drupal and connect these paths with callback functions. It was also responsible for creating menu links and a bunch of other stuff.
在Drupal 7中, hook_menu()
可能是最实现的钩子,因为它用于定义Drupal的路径并将这些路径与回调函数连接。 它还负责创建菜单链接和许多其他内容。
In Drupal 8 we won’t need hook_menu()
anymore as we make heavy use of the Symfony2 components to handle the routing. This involves defining the routes as configuration and handling the callback in a controller (the method of a Controller
class). Let’s see how that works by creating a simple page that outputs the classic Hello world!
.
在Drupal 8中,由于我们大量使用Symfony2组件来处理路由,因此不再需要hook_menu()
。 这涉及将路由定义为配置,并在控制器中处理回调( Controller
类的方法)。 让我们通过创建一个输出经典的Hello world!
的简单页面来了解其工作原理Hello world!
。
First, we need to create a routing file for our module called demo.routing.yml
. This file goes in the module root folder (next to demo.info.yml
). Inside this file, we can have the following (simple) route definition:
首先,我们需要为我们的模块创建一个名为demo.routing.yml
的路由文件。 该文件位于模块根文件夹( demo.info.yml
旁边)中。 在此文件中,我们可以具有以下(简单)路由定义:
demo.demo:
path: '/demo'
defaults:
_content: '\Drupal\demo\Controller\DemoController::demo'
_title: 'Demo'
requirements:
_permission: 'access content'
The first line marks the beginning of a new route called demo
for the module demo
(the first is the module name and the second the route name). Under path
, we specify the path we want this route to register. Under defaults
, we have two things: the default page title (_title
) and the _content
which references a method on the DemoController
class. Under requirements
, we specify the permission the accessing user needs to have to be able to view the page. You should consult this documentation page for more options you can have for this routing file.
第一行标记为模块demo
称为demo
的新路由的开始(第一行是模块名称,第二行是路由名称)。 在path
,我们指定要此路由注册的路径。 在defaults
,我们有两件事:默认页面标题( _title
)和_content
,它们引用DemoController
类上的方法。 根据requirements
,我们指定访问用户必须具备的权限才能查看页面。 您应该查阅此文档页面,以获取有关此路由文件的更多选项。
Now, let’s create our first controller called DemoController
that will have a method named demo()
getting called when a user requests this page.
现在,让我们创建第一个名为DemoController
控制器,该控制器将具有一个名为demo()
的方法,该方法在用户请求此页面时被调用。
Inside the module directory, create a folder called src/
and one called Controller/
inside of it. This will be the place to store the controller classes. Go ahead and create the first one: DemoController.php
.
在模块目录中,创建一个名为src/
的文件夹,并在其中创建一个名为Controller/
的文件夹。 这将是存储控制器类的地方。 继续并创建第一个: DemoController.php
。
The placement of the Controllers and, as we will see, other classes, into the src/
folder is part of the adoption of the PSR-4 standard. Initially, there was a bigger folder structure we had to create (PSR-0 standard) but now there is a transition phase in which both will work. So if you still see code placed in a folder called lib/
, that’s PSR-0.
将控制器以及其他类(如我们将看到的)放置到src/
文件夹中是采用PSR-4标准的一部分。 最初 ,我们必须创建一个更大的文件夹结构(PSR-0标准),但是现在有一个过渡阶段,在这两个阶段都可以工作。 因此,如果您仍然看到代码放在名为lib/
的文件夹中,那就是PSR-0。
Inside of our DemoController.php
file, we can now declare our class:
在我们的DemoController.php
文件中,我们现在可以声明我们的类:
<?php
/**
* @file
* Contains \Drupal\demo\Controller\DemoController.
*/
namespace Drupal\demo\Controller;
/**
* DemoController.
*/
class DemoController {
/**
* Generates an example page.
*/
public function demo() {
return array(
'#markup' => t('Hello World!'),
);
}
}
This is the simplest and minimum we need to do in order to get something to display on the page. At the top, we specify the class namespace and below we declare the class.
这是我们要做的最简单和最少的操作,以便使某些内容显示在页面上。 在顶部,我们指定类名称空间,在下面,我们声明该类。
Inside the DemoController
class, we only have the demo()
method that returns a Drupal 7-like renderable array. Nothing big. All we have to do now is clear the caches and navigate to http://example.com/demo
and we should see a Drupal page with Hello World printed on it.
在DemoController
类内部,我们只有demo()
方法,该方法返回类似Drupal 7的可渲染数组。 没什么大不了的。 现在我们要做的就是清除缓存并导航到http://example.com/demo
,我们应该看到一个Drupal页面,上面印有Hello World 。
In Drupal 7, when we implement hook_menu()
, we can also add the registered paths to menus in order to have menu links showing up on the site. This is again no longer handled with this hook but we use a yml file to declare the menu links as configuration.
在Drupal 7中,当我们实现hook_menu()
,我们还可以将注册的路径添加到菜单中,以使菜单链接显示在站点上。 这个钩子也不再处理此问题,但是我们使用yml文件将菜单链接声明为配置。
Let’s see how we can create a menu link that shows up under the Structure
menu of the administration. First, we need to create a file called demo.menu_links.yml
in the root of our module. Inside this yml file we will define menu links and their position in existing menus on the site. To achieve what we set out to do, we need the following:
让我们看看如何创建一个菜单链接,该菜单链接显示在管理的“ Structure
菜单下。 首先,我们需要在模块的根目录中创建一个名为demo.menu_links.yml
的文件。 在此yml文件中,我们将定义菜单链接及其在站点上现有菜单中的位置。 为了实现我们的目标,我们需要以下内容:
demo.demo:
title: Demo Link
description: 'This is a demo link'
parent: system.admin_structure
route_name: demo.demo
Again we have a yml structure based on indentation in which we first define the machine name of the menu link (demo
) for the module demo
(like we did with the routing). Next, we have the link title and description followed by the parent of this link (where it should be placed) and what route it should use.
同样,我们有一个基于缩进的yml结构,在该结构中,我们首先为模块demo
定义菜单链接( demo
)的机器名称(就像我们对路由所做的那样)。 接下来,我们具有链接标题和描述,后跟该链接的父级(应放置在何处)以及应使用的路由。
The value of parent
is the parent menu link (appended by its module) and to find it you need to do a bit of digging in *.menu_links.yml
files. I know that the Structure
link is defined in the core System module so by looking into the system.menu_links.yml
file I could determine the name of this link.
parent
的值是父菜单链接(由其模块附加),要找到它,您需要对*.menu_links.yml
文件进行一些挖掘。 我知道Structure
链接是在核心系统模块中定义的,因此通过查看system.menu_links.yml
文件,我可以确定该链接的名称。
The route_name
is the machine name of the route we want to use for this link. We defined ours earlier. And with this in place, you can clear the cache and navigate to http://example.com/admin/structure
where you should now see a brand new menu link with the right title and description and that links to the demo/
path. Not bad.
route_name
是我们要用于此链接的路由的机器名称。 我们较早地定义了我们的。 有了这个,您可以清除缓存并导航到http://example.com/admin/structure
,现在您应该会看到带有正确标题和描述的全新菜单链接,该链接也指向demo/
路径。 不错。
In this article we began exploring module development in Drupal 8. At this stage (alpha11 release), it is time to start learning how to work with the new APIs and port contrib modules. To this end, I am putting in writing my exploration of this new and exiting framework that will be Drupal 8 so that we can all learn the changes and hit the ground running when release day comes.
在本文中,我们开始探索Drupal 8中的模块开发。在此阶段(alpha11版本),是时候开始学习如何使用新的API和端口贡献模块了。 为此,我正在撰写对这个新的和即将存在的框架( Drupal 8)的探索,以便我们所有人都可以学习到这些变化,并在发布日到来时立即投入工作。
For starters, we looked at some basics: how you start a Drupal 8 module (files, folder structure etc), all compared with Drupal 7. We’ve also seen how to define routes and a Controller class with a method to be called by this route. And finally, we’ve seen how to create a menu link that uses the route we defined.
首先,我们研究了一些基础知识:与Drupal 7相比,如何启动Drupal 8模块(文件,文件夹结构等)。我们还看到了如何定义路由和Controller类,该类由调用这条路线。 最后,我们已经了解了如何创建使用我们定义的路线的菜单链接。
In the next tutorial, we will continue building this module and look at some other cool new things Drupal 8 works with. We will see how we can create blocks and how to work with forms and the configuration system. See you then.
在下一个教程中,我们将继续构建该模块,并研究Drupal 8可以使用的其他一些很酷的新功能。 我们将看到如何创建块以及如何使用表单和配置系统。 回头见。
翻译自: https://www.sitepoint.com/build-drupal-8-module-routing-controllers-menu-links/
drupal模块