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
online prescription for ed:…
online prescription for ed: edboldrix - cheap ed meds
best rx pharmacy online:…
best rx pharmacy online: Belvion - no prescription pharmacy paypal
https://belvionpharmacy.shop…
https://belvionpharmacy.shop/# worldwide pharmacy
online pharmacy discount…
online pharmacy discount code Belvion no prescription needed pharmacy
https://edboldrix.com/#…
https://edboldrix.com/# cheapest ed pills
http://axeryl.com/#…
http://axeryl.com/# prednisone 4mg
best ed pills online:…
best ed pills online: Boldrix - cheapest online ed treatment
online medicine delivery now…
online medicine delivery now: Gen Indus Pharma - online pharma
https://canamcrosscare.com/#…
https://canamcrosscare.com/# canadian pharmacy price checker
buy medicine online india:…
buy medicine online india: GenIndusPharma - indian chemist
mexico pharmacy list…
mexico pharmacy list SolMedsDirect mexico rx
mexican pharmacy store:…
mexican pharmacy store: SolMedsDirect - medicine from mexico
medicine from mexico:…
medicine from mexico: SolMedsDirect - pharmacy mexico online
http://geninduspharma.com/#…
http://geninduspharma.com/# indian pharmacy delivery to usa
canadian online pharmacy:…
canadian online pharmacy: СanAm Cross Care - canadian pharmacy service
indian pharmacy official…
indian pharmacy official website: GenIndusPharma - all generic medicine india
https://geninduspharma.com/#…
https://geninduspharma.com/# india pharmacy mail order
canada drug pharmacy:…
canada drug pharmacy: canadian drugstore online - canadian pharmacies
legitimate canadian pharmacy…
legitimate canadian pharmacy СanAmCrossCare legit canadian online pharmacy
https://geninduspharma.shop/…
https://geninduspharma.shop/# online pharmacy sites
certified canadian pharmacy:…
certified canadian pharmacy: reputable canadian online pharmacy - canadian pharmacy ratings
canadian pharmacy official…
canadian pharmacy official site or the canadian pharmacy the canadian pharmacy
http://goodadvices.com/?URL=canamcrosscare.com canadian drugstore online and http://www.orangepi.org/orangepibbsen/home.php?mod=space&uid=7035346 canadian pharmacy website
canadian online pharmacy best rated canadian pharmacy and canadian pharmacy service canadian pharmacy reviews
canadian pharmacy king and…
canadian pharmacy king and canadian pharmacy store canadian pharmacy official site
https://www.techjobscafe.com/goto.php?s=Top&goto=https://canamcrosscare… best rated canadian pharmacyand https://gikar.it/user/voukukflhf/ canadian pharmacies online
legitimate canadian online pharmacy reliable canadian pharmacyand canada online pharmacy legit canadian online pharmacy
https://solmedsdirect.com/#…
https://solmedsdirect.com/# purple pharmacy mexico
mexican online pharmacy…
mexican online pharmacy shipping: Sol Meds Direct - mexico drug store
legitimate canadian online…
legitimate canadian online pharmacy: СanAmCrossCare - reliable canadian pharmacy
pharmacy india: Gen Indus…
pharmacy india: Gen Indus Pharma - generic pharmacy online india
the purple pharmacy mexico…
the purple pharmacy mexico or mexican pharmacy online catalog pharmacy in mexico online
http://www.78901.net/alexa/index.asp?url=solmedsdirect.com mexico pharmacy online free shipping or https://virtualchemicalsales.ca/user/latulnuqnq/?um_action=edit online pharmacy in mexico
best mexican pharmacy online mexican pharmacy legit and mexico meds mexican pharmacy online ordering
https://solmedsdirect.com/#…
https://solmedsdirect.com/# medicine from mexico
medicine online delivery:…
medicine online delivery: buy medicine without prescription - indian pharmacy that ships to uk
canadian pharmacies online…
canadian pharmacies online or canadian drug pharmacy canadian pharmacy checker
https://www.bausch.com.sg/redirect/?url=https://canamcrosscare.com canadian pharmacy compare or http://lenhong.fr/user/esqyqmuvdc/ canadian mail order pharmacy
canadian pharmacy compare pharmacies in canada that ship to the us and reliable canadian pharmacy canadian pharmacy king
best canadian pharmacy: Сan…
best canadian pharmacy: Сan Am Cross Care - canadian pharmacy drugs online
vipps approved canadian…
vipps approved canadian online pharmacy or canada rx pharmacy world reputable canadian online pharmacy
http://ixawiki.com/link.php?url=http://canamcrosscare.shop canadian pharmaciesor https://boyerstore.com/user/mhiigjmggy/?um_action=edit best online canadian pharmacy
canadian pharmacy comparison online pharmacy canadaand canadian pharmacy rx cipa canada online pharmacy
indian medicine online:…
indian medicine online: GenIndusPharma - online chemists
vipps approved canadian…
vipps approved canadian online pharmacy: СanAmCrossCare - canadian pharmacy drugs online
https://geninduspharma.shop/…
https://geninduspharma.shop/# п»їindian pharmacy
mexico rx and legit mexican…
mexico rx and legit mexican online pharmacy mexican pharmacy online
http://maps.google.com.mm/url?q=https://solmedsdirect.com can mexican pharmacies take prescription drugs or https://gikar.it/user/vcwckazbpi/ mexico pharmacy list
mexican online pharmacy buy medicine mexico pharmacy order online delivery and does mexico sell prescription drugs mexican pharmacy that ships to usa
online indian pharmacy:…
online indian pharmacy: pharmacy site - buy medicines online
canadian pharmacy service or…
canadian pharmacy service or canada pharmacy canada pharmacy store
https://cse.google.com.ng/url?sa=t&url=https://canamcrosscare.com canadian pharmacy reviews or https://vintage-car.eu/user/rasxgphbtj/ canadian pharmacy world
canadian pharmacy world canadian pharmacy compare or canada rx pharmacy canadian drugstore online
Почитайте интересный https:/…
Почитайте интересный https://vladimir-courses.ru с полезными познавательными статьями. На сайте публикуются актуальные обзоры. Просматривайте подходящие материалы и находите нужную информацию.
Загляните на актуальный…
Загляните на актуальный https://mulino58.ru где собраны познавательную информацию. В публикациях представлены новые публикации. Выбирайте полезные сведения и находите нужную информацию.
pharmacy websites:…
pharmacy websites: GenIndusPharma - generic medicine buy online
canadian pharmacies that…
canadian pharmacies that deliver to the us and canadian pharmacy world canadian pharmacy price checker
https://www.google.dj/url?sa=t&url=https://canamcrosscare.shop canadian pharmacy comparisonand http://dnp-malinovka.ru/user/ivqohbnzbg/?um_action=edit best canadian online pharmacy
legit canadian pharmacy legit canadian pharmacyand canadian pharmacies that deliver to the us canadian pharmacies
Обратите внимание на…
Обратите внимание на актуальный https://vladimir-courses.ru с полезными полезной информацией. В отдельных разделах собраны тематические материалы. Читайте полезные обзоры и узнавайте новое.
Откройте познавательный…
Откройте познавательный https://mulino58.ru среди представленных познавательную информацию. В разделах собраны новые публикации. Выбирайте актуальные публикации и знакомьтесь с интересными темами.
legit canadian pharmacy:…
legit canadian pharmacy: canadian pharmacy official site - canadian online pharmacy
buy medicines online: best…
buy medicines online: best site for online medicine - indian online pharmacy ship to usa
http://solmedsdirect.com/#…
http://solmedsdirect.com/# mexican pharmacies that ship to us
online indian pharmacy:…
online indian pharmacy: tablets delivery - online drug store
mexican pharmacy for hair…
mexican pharmacy for hair loss pills or mexico rx mexican pharmacy for hair loss pills
http://www.78901.net/alexa/index.asp?url=solmedsdirect.com mexican pharmacy that ships to usa and http://bbs.51pinzhi.cn/home.php?mod=space&uid=8380731 pharmacy in mexico
online pharmacies in mexico farmacia pharmacy mexico and order medicine from mexico mexican medicine
Pagination
Add new comment