0

We have explored it and found - how to create a content type by adding the YML files in the config/install directory of a custom module. The new content type will be created when we enable the module.

But we have a use case where we need to generate content types on saving a form. For eg: We have a custom form with a field called "Content-Type Name". On submitting the form, we need to create a new content type with that name.

We are not able to find any proper documentation for this in Drupal 8.

  • 1
    A content type is a config entity, you can create one like any other config entity – Clive Oct 18 '18 at 7:17
  • 3
    Also there is already an existing form for this: the original create content type form. The code itself should be a sufficient documentation for this matter. – ssibal Oct 18 '18 at 7:21
  • "Also there is already an existing form for this: the original create content type form." -- pretty much this. It already exists. I can't think of a use case to reinvent the wheel here. But by all means, look at the provided answer. – Kevin Oct 18 '18 at 14:02
3

A content type is a config entity, you can create one like any other config entity (*):

$content_type = \Drupal\node\Entity\NodeType::create([
  'type' => $machine_name,
  'label' => $label,
]);
$content_type->save();

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.