I'm developing module that creates a content type. I saw the same question and there was a solution. I had tried it but it didn't work.


name: Job Post
description: A job posting content type
type: module
package: Pro Drupal Development
core: 8.x


use Drupal\node\Entity\Node;

$content_type = \Drupal\node\Entity\NodeType::create([
    'type' => 'new_content_type',
    'label' => 'new content type',

closed as unclear what you're asking by leymannx, kiamlaluno Jul 16 at 13:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Don't reinvent the wheel. There is a "Node" module and a "Field UI" module doing exactly this, and both are part of Drupal core. – Hudri Jul 16 at 8:28
  • 1
    You do not need a custom module for this, these are modules part of Drupal core. You can do this using a point & click interface: Go to Manage > Structure > Content types (in the Admin Toolbar) or visit the url /admin/structure/types and click the button "Add content type" – Hudri Jul 16 at 8:43
  • 1
    @Hudri – OP maybe wants to create a shippable module to provide some features and a new content type upon installation. Although it would be nice if they'd clarify about this in their question, OP? – leymannx Jul 16 at 9:00
  • 1
    @ectuser if you export the configuration of created content type to module/config/install directory the content type will be created automatically when the module is installed. That's how Drupal core creates default content types (Article and Basic page). Check out core/profiles/standard/config for examples. – ya.teck Jul 16 at 9:18
  • 1
    The module code above has zero context in what function it is in, it’s likely it was never called to begin with. – Kevin Jul 16 at 11:52

The code from your question should work in a hook_install(). But you need a lot more configuration for a content type, which you can create the easiest way in UI. See the comment of @Hudri.

Then export the configuration to the config folder of your module, see Organizing config for use with a module

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