简体   繁体   English

drupal以编程方式创建实体

[英]drupal create entities programmatically

I use a headless drupal to expose an api for my front app and I have a question regarding the creation and update process of entities in Drupal. 我使用无头Drupal公开了我的前端应用程序的api,并且对Drupal中实体的创建和更新过程有疑问。 It could be usefull if I could create my entities and my bundles programmatically like writing a php script and run it. 如果我可以像编写php脚本那样运行并以编程方式创建我的实体和捆绑包,这可能会很有用。 And maybe other to alter my sql schemes and entites fields. 也许还有其他改变我的SQL方案和实体领域。

I am actually quite noob with drupal (I am used to django and migrations files that works exactly like that). 我实际上对drupal相当陌生(我习惯于django和迁移文件,其工作原理完全一样)。 And I want to know of there is an equivalent for it with drupal (7 or 8 ^^). 我想知道与drupal(7或8 ^^)等效。

You can definitely do this in Drupal using the Entity API. 您绝对可以使用实体API在Drupal中执行此操作。 I've only dabbled with this in D7, but it does exactly what you want. 我只是在D7中涉猎了这一点,但它确实可以满足您的需求。 Of course, it is all done using a Drupal module in hooks, not just a standalone PHP script. 当然,所有这些都是在钩子中使用Drupal模块完成的,而不仅仅是一个独立的PHP脚本。

You create your table using hook_schema, and then handle schema migrations using hook_update_N (where N is any number you choose). 您可以使用hook_schema创建表,然后使用hook_update_N(其中N是您选择的任何数字)来处理模式迁移。 The entities are defined using hook_entity_info Then you need controllers to manage your entities, fields and field instances that tell Drupal how your entities relate and some information about display. 使用hook_entity_info定义实体。然后,您需要控制器来管理您的实体,字段和字段实例,这些实例告诉Drupal实体之间的关系以及有关显示的一些信息。

It is a fairly involved process the first time you run through it. 第一次运行时,这是一个相当复杂的过程。 I know it is bad form to link to external resources here, but this subject is fairly dense. 我知道在这里链接到外部资源是一种不好的形式,但是这个主题相当密集。 I found http://www.zyxware.com/articles/4779/drupal-how-to-create-custom-entity-programatically-in-drupal-7 to be a very valuable resource when I was creating a proof of concept for a project here at work. 当我为以下方面创建概念证明时,我发现http://www.zyxware.com/articles/4779/drupal-how-to-create-custom-entity-programatically-in-drupal-7是非常有价值的资源一个在这里工作的项目。

Good luck, and let me know how it goes. 祝你好运,让我知道如何进行。

If you are using Drupal 8, Drupal Console will help you a great deal. 如果您使用的是Drupal 8, Drupal控制台将为您提供很大的帮助。 There is a command for generating entities , controllers, forms and many other things. 有一个用于生成实体 ,控制器,表单和许多其他东西的命令。

For Drupal 7 the Entity Construction Kit is the way to go 对于Drupal 7, 实体构建套件是必经之路

I have used both these tools sucessfully in production. 我在生产中成功使用了这两个工具。

There is a lot of boilerplate code to write as you might have noticed from Quint's suggestion. 您可能已经从Quint的建议中注意到了很多要编写的样板代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM