1

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 ^^).

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I'll take a look to it and will come back to let you know how it works (well I have a lot to do at work so it will take some time before I'll work on it but thank you ^^.
0

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.

1 Comment

For now we are still using D7 but there is a strong discussion about migrating to D8 so it could be a good id to look a it. I'll look into that when I'll have some time to do it (deadlines and everything.....). Thank you ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.