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: legit mexican pharmacy - mexico pharmacy online free shipping
real canadian pharmacy or…
real canadian pharmacy or canadian pharmacy rx canadian drug pharmacy
http://images.google.com.ai/url?q=https://vesperarx.com canada online pharmacy and https://allchoicesmatter.org/user/gthuqlhmrv/?um_action=edit best rated canadian pharmacy
reputable canadian pharmacy legit canadian online pharmacy or legitimate canadian pharmacy best canadian online pharmacy
https://vesperarx.shop/#…
https://vesperarx.shop/# canadian pharmacy online
https://botica24.com/# best…
https://botica24.com/# best online pharmacies in mexico
best pharmacy in mexico:…
best pharmacy in mexico: mexican pharmacy prescription savings - mexican pharmacy that ships to the us
https://botica24.com/#…
https://botica24.com/# mexican pharmacies online
my canadian pharmacy and…
my canadian pharmacy and best rated canadian pharmacy canada pharmacy store
https://www.google.ge/url?q=https://vesperarx.shop canada online pharmacyand https://boyerstore.com/user/gdesjdqhve/?um_action=edit canada pharmacy store
legitimate canadian pharmacy canada pharmacyand canada online pharmacy canadian pharmacy compare
http://vesperarx.com/# best…
http://vesperarx.com/# best canadian online pharmacy
best canadian pharmacy:…
best canadian pharmacy: canada drug pharmacy - canadian pharmacy world
online medicine sale and…
online medicine sale and pharmacy website medication from india
https://cse.google.co.th/url?q=https://pharmastash.com medicines from india to usa online and https://lifnest.site/user/mukozxeccumukozxeccu/?um_action=edit generic medicine store online
drugstore online buy online medicine or online medicine delivery best online medicine store
п»їcanadian pharmacy or…
п»їcanadian pharmacy or canada online pharmacy canadian pharmacy online
http://www.openlaw.com.pl/wikka.php?wakka=vesperarx.com canada online pharmacy and http://georgiantheatre.ge/user/umwepzxhrp/ canadian pharmacy website
legit canadian online pharmacy top canadian pharmacy and canadian pharmacies online canadian pharmacy official site
https://pharmastash.shop/#…
https://pharmastash.shop/# buy medicines online
Попал в реестр рнп? грозит…
Попал в реестр рнп? грозит рнп что делать анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
https://vesperarx.shop/#…
https://vesperarx.shop/# canadian pharmacy checker
Попал в реестр рнп? юрист…
Попал в реестр рнп? юрист для заседания уфас по рнп анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
Попал в реестр рнп? защита…
Попал в реестр рнп? защита поставщика в фас по рнп анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
http://vesperarx.com/#…
http://vesperarx.com/# canadian pharmacies that deliver to the us
canada pharmacy world or…
canada pharmacy world or canadian pharmacy website online canadian pharmacy
https://image.google.com.bz/url?q=https://vesperarx.shop canadian pharmacy official websiteand http://www.1gmoli.com/home.php?mod=space&uid=1271386 reliable canadian pharmacy
cipa canada online pharmacy canadian mail order pharmacyand certified canadian pharmacy canadian pharmacy ltd
reputable canadian online…
reputable canadian online pharmacy: Vespera - canada pharmacy store
https://vesperarx.shop/#…
https://vesperarx.shop/# canada rx pharmacy
best rated canadian pharmacy…
best rated canadian pharmacy certified canadian international pharmacy canadian pharmacy compare
https://pharmastash.com/# п…
https://pharmastash.com/# п»їindian pharmacy
mexican pharmacy order…
mexican pharmacy order online: mexican online pharmacy - legitimate mexican pharmacy online
http://vesperarx.com/# real…
http://vesperarx.com/# real canadian pharmacy
mexican pharma: Botica 24 -…
mexican pharma: Botica 24 - mexican pharmacies
Попал в реестр рнп? https:/…
Попал в реестр рнп? https://zaschita-rnp.ru анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
Попал в реестр рнп? защита…
Попал в реестр рнп? защита от рнп по 223 фз анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
Попал в реестр рнп? юрист…
Попал в реестр рнп? юрист для заседания уфас по рнп анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
mexican pharmacy price list:…
mexican pharmacy price list: Botica 24 - order antibiotics from mexico
Попал в реестр рнп? https:/…
Попал в реестр рнп? https://zaschita-rnp.ru анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
Попал в реестр рнп?…
Попал в реестр рнп? подготовка возражений по включению в рнп анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
https://botica24.com/#…
https://botica24.com/# mexican pharmacy store
Попал в реестр рнп?…
Попал в реестр рнп? поставщик не смог исполнить контракт рнп анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
Попал в реестр рнп? защита…
Попал в реестр рнп? защита поставщика в фас по рнп анализ обстоятельств закупки, подготовка документов, составление возражений и защита интересов участника в ФАС. Профессиональное сопровождение на всех этапах рассмотрения дела.
https://vesperarx.shop/#…
https://vesperarx.shop/# canadian pharmacy drugs online
canada pharmacy store or…
canada pharmacy store or cipa canada online pharmacy canadian pharmacy service
https://www.google.com.ni/url?sa=t&url=https://vesperarx.com canada rx pharmacy world and https://brueckrachdorf.de/user/gzrwjyllfn/ real canadian pharmacy
canadian pharmacies that deliver to the us canadian pharmacy service and canadian pharmacy website legit canadian pharmacy
prescription drugs available…
prescription drugs available in mexico: Botica 24 - mexican pharmacy prices
http://botica24.com/#…
http://botica24.com/# mexican pharmacies online
Which meme coins https://top…
Which meme coins https://top-meme-coins.com reached their highest price peaks? Reviews of Dogecoin, Shiba Inu, Pepe, Dogwifhat, and other popular tokens. Historical charts, price records, major corrections, market trends, and cryptocurrency dynamics analysis.
Which meme coins https://top…
Which meme coins https://top-meme-coins.com reached their highest price peaks? Reviews of Dogecoin, Shiba Inu, Pepe, Dogwifhat, and other popular tokens. Historical charts, price records, major corrections, market trends, and cryptocurrency dynamics analysis.
indian pharmacy in usa:…
indian pharmacy in usa: PharmaStash - best online pharmacy in india
Which meme coins https://top…
Which meme coins https://top-meme-coins.com reached their highest price peaks? Reviews of Dogecoin, Shiba Inu, Pepe, Dogwifhat, and other popular tokens. Historical charts, price records, major corrections, market trends, and cryptocurrency dynamics analysis.
https://botica24.com/#…
https://botica24.com/# online mexico pharmacy usa
Which meme coins https://top…
Which meme coins https://top-meme-coins.com reached their highest price peaks? Reviews of Dogecoin, Shiba Inu, Pepe, Dogwifhat, and other popular tokens. Historical charts, price records, major corrections, market trends, and cryptocurrency dynamics analysis.
Which meme coins https://top…
Which meme coins https://top-meme-coins.com reached their highest price peaks? Reviews of Dogecoin, Shiba Inu, Pepe, Dogwifhat, and other popular tokens. Historical charts, price records, major corrections, market trends, and cryptocurrency dynamics analysis.
Which meme coins https://top…
Which meme coins https://top-meme-coins.com reached their highest price peaks? Reviews of Dogecoin, Shiba Inu, Pepe, Dogwifhat, and other popular tokens. Historical charts, price records, major corrections, market trends, and cryptocurrency dynamics analysis.
canadian pharmacy meds:…
canadian pharmacy meds: canadian pharmacy ltd - canadian pharmacy rx
https://vesperarx.shop/#…
https://vesperarx.shop/# canadian pharmacy legit
best indian pharmacy online:…
best indian pharmacy online: cheapest indian pharmacy online - indian pharmacies
http://botica24.com/# best…
http://botica24.com/# best online pharmacies in mexico
Pagination
Add new comment