5

In Drupal 7 with this query:

SELECT * FROM node_type;

I can obtain the list of all my content types, now in Drupal 8 I can't find the node_type table.

I know that with this code:

$content_types = \Drupal::service('entity.manager')->getStorage('node_type')->loadMultiple();

$content_types_list = [];
foreach ($content_types as $content_type) {
  $content_types_list[$content_type->id()] = $content_type->label();
}

I can obtain the info, but where is this information stored now in the database?

2 Answers 2

6

It's configuration. Like any other config entity and simple configuration, it is stored in the {config} table.

But using the entity API is the only supported way to load that information, do not query that table directly.

5
  • thanks, and for the moment there is no way to involve a LEFT JOIN with the content types machine names and other table? Commented Apr 4, 2017 at 12:53
  • machine names are the ids, what exactly do you need to join/query?
    – Berdir
    Commented Apr 4, 2017 at 14:11
  • Yes, I just don't explain my point, In Drupal 7 with this query SELECT type, name, COUNT(nid) as total FROM node_type LEFT JOIN node USING(type) GROUP BY type HAVING total < 1 ORDER BY name I can get all the content types that don't have nodes, but now this is not posible because I can use the LEFT JOIN with the config table. This is my point. Commented Apr 4, 2017 at 15:06
  • Yes, such a join isn't really possible anymore but there are some alternatives. you could do an entity aggregated query and then compare that against a list of node type ids. Or if you don't have dozens of types, it might actually not be that much slower to just loop over the types and select one record, because a count(*) on a large data sate can be quite slow if it's not well optimized.
    – Berdir
    Commented Apr 4, 2017 at 17:36
  • Thanks, I'll made some test and I'll let you know the result, for the moment I use the code in the question and this query to know the content types that don't have nodes SELECT type FROM {node} GROUP BY type The difference between all the content types defined and the content types that have nodes are the content types that don't have nodes. Commented Apr 4, 2017 at 17:42
1

As answered earlier, it is stored in the {config} table. To be more specific:

  • If the machine name for a content type is "blogpost", there will be a record in the {config} table with name="node.type.blogpost".
  • The data field will contain a PHP serialized array with the configuration details of the content type. This must be reasonably valid in order for the content type to be listed on the /admin/structure/types page.
  • If you create a content type via the UI, it will also create the following {config} records (though these are not necessary to be listed on the /admin/structure/types page):
    • core.entity_form_display.node.{type}.default
    • core.entity_view_display.node.{type}.default
    • core.entity_view_display.node.{type}.teaser
    • field.field.node.{type}.body

For writing production code, I would echo the earlier recommendation not to directly query the {config} table. However, using these details to query the table could be helpful for things like debugging a failed migration.

1
  • Then how should one "query the config table" ?
    – Raf A.
    Commented May 29, 2018 at 4:50

Your Answer

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

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