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
certified canadian…
certified canadian international pharmacy: top canadian pharmacy ~ top canadian pharmacy
real canadian pharmacy or…
real canadian pharmacy or legitimate canadian pharmacy best online canadian pharmacy
https://images.google.gl/url?sa=t&url=https://northrxcanada.com best canadian pharmacy or https://polyphonia.eu/forum/index.php?action=profile;u=78506 cipa canada online pharmacy
online canadian pharmacy safe online pharmacies in canada and canadian pharmacy ltd best rated canadian pharmacy
https://mexicogenerics.shop/…
https://mexicogenerics.shop/# pharmacy in mexico
https://northrxcanada.com/#…
https://northrxcanada.com/# canadian pharmacy checker
medications from india:…
medications from india: online pharmacy website - reputable indian pharmacies for otc drugs
https://mexicogenerics.shop/…
https://mexicogenerics.shop/# prescription drugs available in mexico
reputable canadian pharmacy…
reputable canadian pharmacy or best canadian online pharmacy canadian pharmacy price checker
https://cse.google.mw/url?sa=t&url=https://northrxcanada.com canadian drug pharmacy and http://www.empyrethegame.com/forum/memberlist.php?mode=viewprofile&u=49… canada pharmacy online
best rated canadian pharmacy certified canadian pharmacy or canadian pharmacy ratings canadian pharmacies
online canadian drugstore…
online canadian drugstore vipps approved canadian online pharmacy - pharmacies in canada that ship to the us
http://mexicogenerics.com/#…
http://mexicogenerics.com/# pharmacies in mexico
https://mexicogenerics.com/#…
https://mexicogenerics.com/# mexico prescription online
canadian pharmacy official…
canadian pharmacy official site or canadian pharmacy 365 best canadian pharmacy
http://www.hostdisplaythai.com/festival/queen/index.php?ww=northrxcanad… canadian pharmacy compare or https://www.liveviolet.net/user/tnzvoektla/videos best canadian online pharmacy
canadian drug pharmacy canada pharmacy and canadian pharmacy ratings canadian pharmacy service
prescriptions from india…
prescriptions from india India Generic Store and online drugstore
http://mexicogenerics.shop/#…
http://mexicogenerics.shop/# buying from online mexican pharmacy
pharmacy online shopping:…
pharmacy online shopping: online pharmacy in india :: order medicine online india
my canadian pharmacy or…
my canadian pharmacy or canada pharmacy world canadian drug pharmacy
https://www.google.az/url?sa=t&url=http://northrxcanada.com canadian pharmacy king and https://exhibitioncourthotel4.co.uk/user-2/kekkwyanmr/?um_action=edit canadian pharmacy store
online canadian drugstore best online canadian pharmacy and canadian pharmacy world canada pharmacy world
http://mexicogenerics.shop/#…
http://mexicogenerics.shop/# pharmacies in mexico
http://northrxcanada.shop/#…
http://northrxcanada.shop/# canadian pharmacy reviews
online medical shopping and…
online medical shopping and best indian pharmacy online indian pharmacies in usa
https://maps.google.es/url?q=https://indiagenericstore.com india pharmacies or http://lostfilmhd.com/user/ykniniuair/ india drug store
online pharmacy website reputable indian pharmacies or india pharmacy mail order online drugs
https://northrxcanada.shop/#…
https://northrxcanada.shop/# certified canadian international pharmacy
canadian pharmacy drugs…
canadian pharmacy drugs online or canadian drug pharmacy canadian pharmacy checker
https://images.google.ms/url?sa=t&url=https://northrxcanada.shop canadian pharmacies that deliver to the usand https://4k-porn-video.com/user/qpxrowijlx/ canadian pharmacy world
canadian pharmacy service canadian pharmacy official siteor canadian drug pharmacy canadian pharmacy official site
https://mexicogenerics.shop/…
https://mexicogenerics.shop/# mexico pharmacy order online delivery
https://northrxcanada.shop/#…
https://northrxcanada.shop/# canadian drugstore online
canadian pharmacy reviews…
canadian pharmacy reviews NorthRxCanada ~ canadian pharmacy legit
Pagination
Add new comment