13

In Drupal 7 we used to use hook_schema() in the .install file to create a table. I believe that, in Drupal 8, you can do that as well, but I was under the impression that you should create .yml files with your schema.

What's the method for creating tables in Drupal 8?

1
  • 3
    No, you're correct. hook_schema is still the way to create custom tables in Drupal 8 that are not described by content entities or fields. If your custom table is something you would consider "content", then I would use entities, but if it needs to be faster and will have limited amount of rows and you're not doing any caching on the front end, then a custom table is fine. You should also consider whether this table is "configuration", in which case a configuration entity or object is the way to go.
    – mradcliffe
    Nov 3 '16 at 22:05
11

hook_schema() is still used from Drupal 8 modules to create custom database tables used from the module. Even the User and the Node modules implement it, although user_schema() and node_schema() don't define the schema for the respective entities, which are created in a different way.

The configuration schema is not used to create custom database tables a module uses, but describes the structure of configuration files. The PDF file found in the documentation I linked makes clear the relation between a configuration file and its schema.

screenshot

Content entities don't use hook_schema(); Drupal creates the database table for them basing on the base fields the content entity class defines.

Which method to use depends on what the module defines. It's up to the module define a content entity, a configuration entity, or just use a custom table. A module could even use all of them.

11

You can use hook install to create your own tables, if you really want to, although Drupal has many other more out-of-the-box solutions. But if you do want to roll your own, there's an example module, called dbtng_example, in the examples module: https://www.drupal.org/project/examples, complete with routing for adding and removing your custom database entries.

I'd recommend installing it and trying it out, then you can use that as a basis for creating your own database table in Drupal 8.

Another approach to consider is to create a config entity or a content entity.

If you use the drupal command line tool there's even a command for generating these "drupal generate:entity:config", and "drupal generate:entity:content".

Also in the examples module, there is config_entity_example and content_entity_example.

From https://www.previousnext.com.au/blog/understanding-drupal-8s-config-entities: "The main difference between content and config entities is how they are stored and (at the moment) config entities are not fieldable".

There are some other more out of the box solutions, such as the config_pages module that allow you to easily spin up places to store data on your site: https://www.drupal.org/project/config_pages

We recently used these for one off landing pages, by creating a controller and route. This has a huge advantage over a custom table as you can easily add new fields and you can use data types such as images and entity references easily, you can also set field formatters, and load the data formatted using the config_pages entity view builder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.