简体   繁体   中英

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. It could be usefull if I could create my entities and my bundles programmatically like writing a php script and run it. And maybe other to alter my sql schemes and entites fields.

I am actually quite noob with drupal (I am used to django and migrations files that works exactly like that). And I want to know of there is an equivalent for it with drupal (7 or 8 ^^).

You can definitely do this in Drupal using the Entity API. I've only dabbled with this in D7, but it does exactly what you want. Of course, it is all done using a Drupal module in hooks, not just a standalone PHP script.

You create your table using hook_schema, and then handle schema migrations using hook_update_N (where N is any number you choose). 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.

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.

Good luck, and let me know how it goes.

If you are using Drupal 8, Drupal Console will help you a great deal. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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