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
mexican pharmacy usa…
mexican pharmacy usa delivery: mexipharmacy website - mexican pharmacy for hair loss pills
https://pharmastash.com/#…
https://pharmastash.com/# online pharmacy website
canadian online pharmacy and…
canadian online pharmacy and canadian pharmacy 365 legit canadian online pharmacy
https://promotionalrange.com.au/?URL=https://vesperarx.shop cipa canada online pharmacyand http://la-maison-des-amis.com/user/cyykysjgee/ п»їcanadian pharmacy
canadian drug pharmacy canadian pharmacy medsand canada drug pharmacy canada pharmacy world
canada rx pharmacy: Vespera …
canada rx pharmacy: Vespera - reliable canadian pharmacy
https://vesperarx.shop/#…
https://vesperarx.shop/# canadian pharmacy world
buy medicine online in india…
buy medicine online in india PharmaStash order medicine online india
mexico pharmacy mail order…
mexico pharmacy mail order online: mexican pharmacy that ships to the us - mexico pharmacy order online free shipping
http://pharmastash.com/# buy…
http://pharmastash.com/# buy medicine from india to usa
real canadian pharmacy or…
real canadian pharmacy or canadian pharmacy compare canadian pharmacy ltd
http://pin.anime.com/source/vesperarx.shop/ best online canadian pharmacyor http://www.1gmoli.com/home.php?mod=space&uid=1269974 canadian pharmacy compare
canadian pharmacy price checker my canadian pharmacyor canadian pharmacy rx canadian pharmacy reviews
http://vesperarx.com/#…
http://vesperarx.com/# canadian mail order pharmacy
демонтаж зданий строений…
демонтаж зданий строений демонтаж модульных зданий
legit canadian online…
legit canadian online pharmacy and canadian pharmacy store canadian pharmacy drugs online
https://clients1.google.fi/url?q=https://vesperarx.com legit canadian pharmacy or https://brueckrachdorf.de/user/whcjndkqqe/ safe online pharmacies in canada
canada rx pharmacy world my canadian pharmacy or reliable canadian pharmacy reputable canadian pharmacy
https://pharmastash.shop/#…
https://pharmastash.shop/# medicines from india to usa online
canada pharmacy world or…
canada pharmacy world or canadian drugstore online п»їcanadian pharmacy
http://www.hamajim.com.shard.jp/mt/mt4i.cgi?id=4&mode=redirect&no=195&u… canadian pharmacy official websiteor http://www.garmoniya.uglich.ru/user/pshxbwjrya/ canadian pharmacies
reputable canadian pharmacy canadian pharmacies that deliver to the usand canadian pharmacy reviews online canadian pharmacy
https://botica24.com/# legit…
https://botica24.com/# legit mexican online pharmacy
ordering medicines online:…
ordering medicines online: PharmaStash - prednisone cost in india
http://botica24.com/#…
http://botica24.com/# mexipharmacy website
https://botica24.shop/#…
https://botica24.shop/# online mexico pharmacy usa
снос зданий и сооружений…
снос зданий и сооружений цена снос зданий и сооружений
http://botica24.com/#…
http://botica24.com/# reliable mexican pharmacies
mexican pharmacies that ship…
mexican pharmacies that ship to us: good online mexican pharmacy - mexican pharmacy store
https://botica24.shop/#…
https://botica24.shop/# order medication from mexico
canadian pharmacy drugs…
canadian pharmacy drugs online Vespera online canadian drugstore
buy meds online: PharmaStash…
buy meds online: PharmaStash - drugstore online
https://vesperarx.shop/#…
https://vesperarx.shop/# canada pharmacy online
http://pharmastash.com/#…
http://pharmastash.com/# best online pharmacy in india
indian pharmacy prednisolone…
indian pharmacy prednisolone: PharmaStash - online drugstore
http://botica24.com/#…
http://botica24.com/# mexican pharmacy prescription cost
canadian pharmacy ratings:…
canadian pharmacy ratings: Vespera - canadian pharmacy ltd
http://botica24.com/# mexico…
http://botica24.com/# mexico pharmacy order online prescription
https://pharmastash.com/#…
https://pharmastash.com/# online medicine shopping
mexican online pharmacy buy…
mexican online pharmacy buy medicine: Botica 24 - farmacia mexicana en linea
https://botica24.com/# buy…
https://botica24.com/# buy from mexico pharmacy
canada pharmacy world or…
canada pharmacy world or canadian pharmacy comparison canadian pharmacy reviews
https://maps.google.com.ai/url?sa=t&url=https://vesperarx.com online canadian pharmacy and https://cannapedia.site/profile/ckvkthxdwr/ canadian pharmacy reviews
online canadian pharmacy canada rx pharmacy world and canadian pharmacy service online canadian drugstore
https://vesperarx.shop/#…
https://vesperarx.shop/# canadian pharmacy checker
buying medicine online in…
buying medicine online in india or online medicine delivery service medicine purchase online
https://cse.google.co.mz/url?sa=t&url=https://pharmastash.com online medicine delivery service and http://www.garmoniya.uglich.ru/user/qgwqnbssru/ buy meds online
indian pharmacy official website online medicine sites or ordering medicines online indian medicine online
reputable canadian online…
reputable canadian online pharmacy: canadian pharmacies - canadian pharmacy drugs online
online medicine website: buy…
online medicine website: buy medicine without prescription - shop medicine online
https://pharmastash.com/#…
https://pharmastash.com/# indian medicine in usa
the canadian pharmacy and…
the canadian pharmacy and vipps approved canadian online pharmacy canadian pharmacy checker
https://cse.google.ad/url?sa=i&url=https://vesperarx.com canadian pharmacy compare or https://gikar.it/user/pbglkeiuga/ canadian pharmacy world
canadian pharmacy 365 canadian pharmacies that ship to usa and canadian pharmacy legit canadian pharmacies
online pharma: PharmaStash -…
online pharma: PharmaStash - order medicine without prescription
https://botica24.com/#…
https://botica24.com/# mexican pharmacy for hair loss pills
https://botica24.shop/#…
https://botica24.shop/# online pharmacy in mexico
canadian pharmacy compare:…
canadian pharmacy compare: canadian mail order pharmacy - canadian pharmacy drugs online
canadian pharmacy comparison…
canadian pharmacy comparison or canada rx pharmacy canada pharmacy store
http://ocmw-info-cpas.be/?URL=https://vesperarx.shop canadian pharmacy ratingsand http://lostfilmhd.com/user/blahihxqer/ canadian pharmacies that deliver to the us
canadian pharmacy checker canadian pharmacy serviceand canadian pharmacies that deliver to the us certified canadian pharmacy
http://pharmastash.com/#…
http://pharmastash.com/# drug store online
снос здания демонтаж и снос…
снос здания демонтаж и снос зданий
canadian pharmacy ltd and…
canadian pharmacy ltd and canada pharmacy online canada drug pharmacy
https://maps.google.bt/url?q=https://vesperarx.com canadian pharmacy website or https://www.liveviolet.net/user/hcxysghnyw/videos cipa canada online pharmacy
online canadian pharmacy legit canadian pharmacy and canadian pharmacy service canadian pharmacy official website
order meds from mexico:…
order meds from mexico: Botica 24 - the purple pharmacy mexico
http://pharmastash.com/#…
http://pharmastash.com/# reputable indian pharmacies
Pagination
Add new comment