4

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?

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.

  • thanks, and for the moment there is no way to involve a LEFT JOIN with the content types machine names and other table? – Adrian Cid Almaguer Apr 4 '17 at 12:53
  • machine names are the ids, what exactly do you need to join/query? – Berdir Apr 4 '17 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. – Adrian Cid Almaguer Apr 4 '17 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 Apr 4 '17 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. – Adrian Cid Almaguer Apr 4 '17 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.

  • Then how should one "query the config table" ? – Raf A. May 29 '18 at 4:50

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.