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
indian chemist: PharmaStash …
indian chemist: PharmaStash - indian pharmacies in usa
http://pharmastash.com/#…
http://pharmastash.com/# medications from india
https://botica24.com/#…
https://botica24.com/# mexican pharmacy usa delivery
indian pharmacy online…
indian pharmacy online lowest price and indian pharmacy official website online shopping medicine
http://burgman-club.ru/forum/away.php?s=https://pharmastash.com best indian pharmacy website or http://www.orangepi.org/orangepibbsen/home.php?mod=space&uid=7199164 online medical store india
indian pharmacy that ships to uk online medicine shopping india or buy medicines online in india pharmacy site
pharmacy online shopping:…
pharmacy online shopping: PharmaStash - buy medicines online in india
https://botica24.com/#…
https://botica24.com/# mexican rx pharm
real canadian pharmacy:…
real canadian pharmacy: canadian pharmacies that deliver to the us - legitimate canadian pharmacy
http://vesperarx.com/#…
http://vesperarx.com/# canadian pharmacy meds
п»їmexican pharmacy or…
п»їmexican pharmacy or farmacia pharmacy mexico top 10 online pharmacies in mexico
http://whois.desta.biz/whois/botica24.com mexican pharmacies that ship or http://www.xunlong.tv/en/orangepibbsen/home.php?mod=space&uid=7197377 mail order pharmacy mexico
mexican pharmacies that ship mexican pharma or mexico pharmacy online free shipping mexico pharmacy
certified canadian…
certified canadian international pharmacy: Vespera - trusted canadian pharmacy
https://vesperarx.com/# п…
https://vesperarx.com/# п»їcanadian pharmacy
rx india: meds from india -…
rx india: meds from india - online medicine shopping
canadian pharmacies or…
canadian pharmacies or canadian pharmacy rx canada rx pharmacy
https://maps.google.li/url?q=https://vesperarx.shop canada pharmacy storeand https://www.0468wudi.com/home.php?mod=space&uid=341383 canadian pharmacies
safe canadian pharmacy reliable canadian pharmacyor best canadian pharmacy safe canadian pharmacy
http://pharmastash.com/#…
http://pharmastash.com/# online indian pharmacy
https://botica24.shop/#…
https://botica24.shop/# mexico pharmacy order online prescription
farmacias mexicanas and…
farmacias mexicanas and mexican pharmacy online ordering pharmacies in mexico
https://american-europe.us/?data=http://botica24.com/ pharmacy in mexico online or https://exhibitioncourthotel4.co.uk/user-2/tfxvdeygxk/?um_action=edit legit mexican pharmacy
mexico pharmacy order online delivery mexican pharmacies that ship to the united states or mexican prescription drug imports reliable mexican pharmacies
mexican pharmacy for…
mexican pharmacy for prescription drugs: Botica 24 - mexican pharmacy usa delivery
https://vesperarx.shop/#…
https://vesperarx.shop/# reputable canadian pharmacy
mexico pharmacy order online…
mexico pharmacy order online free shipping: mexican pharmacy online ordering - mexican pharmacy prescription savings
online medicine home…
online medicine home delivery: order medicine without prescription - online medical store
https://botica24.shop/#…
https://botica24.shop/# mexican pharmacy for prescription drugs
purple pharmacy online…
purple pharmacy online ordering: Botica 24 - mexican online pharmacy wegovy
pharmacies in canada that…
pharmacies in canada that ship to the us: Vespera - reputable canadian online pharmacy
http://vesperarx.com/# safe…
http://vesperarx.com/# safe online pharmacies in canada
http://vesperarx.com/#…
http://vesperarx.com/# trusted canadian pharmacy
legit mexican online…
legit mexican online pharmacy Botica 24 mexico pharmacy order online usa
mexico pharmacy mail order…
mexico pharmacy mail order online: best mexican online pharmacies - mexican pharmacy online medications
https://botica24.com/# is…
https://botica24.com/# is mexican pharmacy store legit
pharmacies in canada that…
pharmacies in canada that ship to the us and canadian pharmacy legit canada pharmacy online
https://www.crb600h.com/mobile/api/device.php?uri=https://vesperarx.com canadian pharmacy checker or https://gikar.it/user/iddwjdqinf/ canadian pharmacy ratings
reliable canadian pharmacy safe online pharmacies in canada and canadian pharmacy service canada pharmacy world
best canadian online…
best canadian online pharmacy: Vespera - best canadian online pharmacy
https://vesperarx.com/# the…
https://vesperarx.com/# the canadian pharmacy
reliable mexican pharmacies:…
reliable mexican pharmacies: Botica 24 - best pharmacy in mexico
canadian pharmacy official…
canadian pharmacy official site Vespera real canadian pharmacy
https://botica24.com/#…
https://botica24.com/# pharmacies in mexico
http://botica24.com/# does…
http://botica24.com/# does mexican pharmacy sell prescriptions
canadian pharmacy website:…
canadian pharmacy website: legitimate canadian online pharmacy - trusted canadian pharmacy
https://botica24.shop/# mexi…
https://botica24.shop/# mexi pharmacy
canadian pharmacy price…
canadian pharmacy price checker and canadian pharmacy world cipa canada online pharmacy
http://images.google.dk/url?q=http://vesperarx.shop п»їcanadian pharmacyand http://lenhong.fr/user/thqnmnkvfb/ trusted canadian pharmacy
real canadian pharmacy certified canadian pharmacyor the canadian pharmacy canadian pharmacy meds
best online pharmacies in…
best online pharmacies in india: PharmaStash - india prescriptions
https://pharmastash.shop/#…
https://pharmastash.shop/# best online medicine store
canadian pharmacy meds:…
canadian pharmacy meds: canadian pharmacy comparison - canadian online pharmacy
canadian pharmacy rx and…
canadian pharmacy rx and canadian pharmacies that ship to usa canada pharmacy online
https://cse.google.dm/url?q=https://vesperarx.com canada pharmacy online or https://mantiseye.com/community/cqphvobsce online canadian pharmacy
canadian pharmacy reviews canadian mail order pharmacy or canada pharmacy online legitimate canadian pharmacy
https://vesperarx.shop/#…
https://vesperarx.shop/# canadian pharmacies online
canada pharmacy: Vespera -…
canada pharmacy: Vespera - trusted canadian pharmacy
http://vesperarx.com/# best…
http://vesperarx.com/# best canadian pharmacy
http://botica24.com/# purple…
http://botica24.com/# purple pharmacy online ordering
buy medicines from india…
buy medicines from india online: PharmaStash - buy generic medicines online india
https://vesperarx.com/#…
https://vesperarx.com/# canadian pharmacy comparison
all generic medicine india…
all generic medicine india and pharmacy in india buy medication online
https://maps.google.com.tr/url?sa=t&url=https://pharmastash.com buy medicines online in india or http://bbs.51pinzhi.cn/home.php?mod=space&uid=8531602 india pharmacy
online pharmacy in india online medicine websites or overseas pharmacies shipping to usa pharmacy order online
online mexico pharmacy usa:…
online mexico pharmacy usa: mexican pharmacy online usa - mexican pharmacy online ordering
Pagination
Add new comment