Content types are the backbone of content management in Drupal. In Drupal 10, you can create custom content types to structure and organize your content. This blog post will guide you through the process of creating content types programmatically in Drupal 10, allowing you to tailor your website's content structure to your specific needs.
Prerequisites
Before creating custom content types programmatically in Drupal 10, you should have the following:
- A working Drupal 10 or 9 website.
- Basic knowledge of Drupal site building and module development.
- Familiarity with PHP and Drupal's module structure.
Step 1: Define the Custom Module
To create a custom content type programmatically, you need to place the code in a custom module. If you don't already have a custom module, you can create one by following these steps:
- In your Drupal installation, navigate to the sites/all/modules directory (or your preferred module location).
- Create a new directory for your custom module. Give it a unique and descriptive name, such as my_custom_content_type.
- Inside your module directory, create a my_custom_content_type.info.yml file with the following content:
name: 'My Custom Content Type Module'
type: module
description: 'Defines custom content types programmatically.'
core_version_requirement: ^9 || ^10
package: Custom
dependencies:
- nodeThis YAML file defines the basic information about your custom module.
Step 2: Create a Custom Content Type
To create a custom content type, you'll need to implement the hook_entity_type_build and hook_node_type_insert hooks within your custom module.
- Create a file named my_custom_content_type.module in your module directory.
- In this file, implement the hook_entity_type_build hook to define your custom content type. Here's an example:
/**
* Implements hook_entity_type_build().
*/
function my_custom_content_type_entity_type_build(array &$entity_types) {
$entity_types['node']->setClass('Drupal\my_custom_content_type\NodeTypeCustom');
}This code defines a custom class, NodeTypeCustom, to handle the configuration of your custom content type.
- Create the NodeTypeCustom class in a file named NodeTypeCustom.php and place it in your module's directory:
namespace Drupal\my_custom_content_type;
use Drupal\node\NodeTypeBase;
/**
* Provides a custom content type.
*/
class NodeTypeCustom extends NodeTypeBase {
/**
* {@inheritdoc}
*/
public function postSave(NodeTypeInterface $node_type) {
parent::postSave($node_type);
// Define the custom content type settings.
$config = \Drupal::configFactory()->getEditable('node.type.' . $node_type->id());
$config->set('name', 'Custom Content Type');
$config->set('description', 'A custom content type created programmatically.');
$config->set('new_revision', TRUE);
$config->save();
}
}n the NodeTypeCustom class, we extend the NodeTypeBase class to define the custom content type settings, such as the name and description.
Step 3: Enable and Verify Your Custom Content Type
- In your Drupal admin interface, navigate to Extend (admin/modules) and enable your custom module, "My Custom Content Type Module."
- After enabling the module, go to Structure > Content types (admin/structure/types) to see your custom content type listed as "Custom Content Type."
Conclusion
Creating custom content types programmatically in Drupal 10 allows you to define content structures that fit your specific project requirements. By following the steps outlined in this blog post, you can easily create and configure custom content types within your Drupal website, providing a flexible and tailored content management experience.
In Drupal 9 and 10, custom modules should be stored in the "modules/custom" directory rather than "sites/all/modules." Drupal's module directory structure has evolved, and the recommended practice is to place custom modules in the "modules/custom" directory to follow the modern Drupal standards.
So, if you're creating a custom module in Drupal 9 or 10, you should place it in the following directory:
/sites/all/modules/custom/By following this structure, your custom modules will be more organized and adhere to Drupal's best practices, making it easier to manage and maintain your site's codebase.
Comment
isotretinoin 30mg price:…
isotretinoin 30mg price: DermaTret Direct - accutane price per pill
People also search for Close…
People also search for Close lopressor generic names lopressor generic brand names
https://dermatretdirect.shop…
https://dermatretdirect.shop/# cheap isotretinoin 10mg
can you take sudafed with…
can you take sudafed with gabapentin or xylitol in gabapentin can gabapentin cause speech problems
http://vmus.adu.org.za/vm_search.php?database=vimma&prj_acronym=MammalM… gabapentin used with hydrocodone or https://brueckrachdorf.de/user/boybuuznyo/ gabapentin generic names
gabapentin 100 mg capsule gabapentin and menopause or gabapentin european pharmacopoeia can you take gabapentin and norco
http://zpacksupply.com/# buy…
http://zpacksupply.com/# buy zithromax online australia
accutane generic brands:…
accutane generic brands: cheap accutane online pharmacy - accutane price per pill
zithromax online usa and…
zithromax online usa and zithromax cost canada zithromax antibiotic
https://2ip.ru/lookup/?ip=zpacksupply.com zithromax 500 priceand http://ussher.org.uk/user/pnayoqsdun/?um_action=edit buy azithromycin zithromax
generic zithromax india can you buy zithromax over the counterand generic zithromax india zithromax purchase online
generic synthroid for sale…
generic synthroid for sale or synthroid 200 mcg price per pill synthroid 25mcg price
http://www.drachenzaehmenleichtgemacht.at/notice.php?url=https://levoca… order generic synthroid 75mcg and http://bbs.51pinzhi.cn/home.php?mod=space&uid=8476382 order levothyroxine without prescription
cheap synthroid 50 mcg buy thyroid hormone pills online or levothyroxine sodium 50 mcg buy buy generic levothyroxine 100mcg
order roaccutane 20mg or…
order roaccutane 20mg or purchase isotretinoin pills order isotretinoin without prescription
https://www.google.se/url?q=https://dermatretdirect.com buy generic isotretinoin no rx and http://lostfilmhd.com/user/bqavfxfavh/ cheap accutane online pharmacy
buy accutane from canada buy legit accutane online and accutane generic brands generic accutane 20mg buy
http://dermatretdirect.com/#…
http://dermatretdirect.com/# buy accutane discreet shipping
synthroid 88 mcg best price…
synthroid 88 mcg best price levothyroxine mail order pharmacy buy levothyroxine online pharmacy
https://levocarepharma.shop/…
https://levocarepharma.shop/# buy thyroid hormone pills online
Нужен психолог? информация…
Нужен психолог? информация на сайте запишитесь на консультацию для работы с тревогой, страхами, стрессом и сложностями в отношениях. Профессиональная психологическая помощь при эмоциональных трудностях, жизненных кризисах и проблемах в отношениях.
Best selection of the day…
Best selection of the day https://alefaneqvetclinic.com/regulations-surrounding-online-casinos-in…
Нужен психолог? психотерапия…
Нужен психолог? психотерапия запишитесь на консультацию для работы с тревогой, страхами, стрессом и сложностями в отношениях. Профессиональная психологическая помощь при эмоциональных трудностях, жизненных кризисах и проблемах в отношениях.
Read more on the website…
Read more on the website https://getmanni.de/wordpress/2026/10/13/australian-online-gambling-reg…
The most interesting click…
The most interesting click https://gifted-side.flywheelstaging.com/ethio-lottery-betting-open-now-…
Today's summary https:/…
Today's summary https://treasury.jp/zplay-bet-ethiopia-betting-open-now-ethiopia-slot-m…
http://zpacksupply.com/# buy…
http://zpacksupply.com/# buy zithromax
Нужен психолог? страхи…
Нужен психолог? страхи запишитесь на консультацию для работы с тревогой, страхами, стрессом и сложностями в отношениях. Профессиональная психологическая помощь при эмоциональных трудностях, жизненных кризисах и проблемах в отношениях.
Best selection of the day…
Best selection of the day https://aquamarine-whale-964714.hostingersite.com/promotions-and-offers…
Нужен психолог? фобии…
Нужен психолог? фобии запишитесь на консультацию для работы с тревогой, страхами, стрессом и сложностями в отношениях. Профессиональная психологическая помощь при эмоциональных трудностях, жизненных кризисах и проблемах в отношениях.
Expanded article here https:…
Expanded article here https://annuaire.tunisie.co/archives/485655
synthroid cost without…
synthroid cost without insurance LevoCare Pharma generic synthroid for sale
order isotretinoin without…
order isotretinoin without prescription or buy accutane from canada buy legit accutane online
https://maps.google.si/url?sa=t&url=https://dermatretdirect.com cheap accutane online pharmacy and http://lostfilmhd.com/user/bakajsvcvk/ buy accutane from canada
purchase isotretinoin pills buy generic isotretinoin no rx or generic accutane 20mg buy generic isotretinoin tablets
http://gabarxdirect.com/#…
http://gabarxdirect.com/# can you gain weight on gabapentin
п»їbuy synthroid online Levo…
п»їbuy synthroid online Levo Care Pharma order levothyroxine 175 mcg
Pagination
Add new comment