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
https://getscriptkinetics…
https://getscriptkinetics.com/# safe online pharmacies
generic antibiotics online…
generic antibiotics online pharmacy bactribatch generic antibiotics online cheap
prescribed antibiotics…
prescribed antibiotics online: cheap indian generic antibiotics - buy antibiotics online
Цікавлять новини? https:/…
Цікавлять новини? https://channel.com.ua основні події країни, нові публікації та важливі інформаційні матеріали. Читайте про політику, економіку, суспільство, бізнес та міжнародні відносини. Оперативні огляди, аналітика та подробиці подій з різних регіонів.
https://getscriptkinetics…
https://getscriptkinetics.xyz/# online pharmacies
https://getscriptkinetics…
https://getscriptkinetics.xyz/# online pharmacy no rx
mexican pharmacy ship to usa…
mexican pharmacy ship to usa: MexiVex - mexican pharmacy menu
mexico rx: order mexican…
mexico rx: order mexican pharmaceuticals online - mexican drugstore
http://mexivexpharma.com/#…
http://mexivexpharma.com/# medicine from mexico
cheapest online pharmacy…
cheapest online pharmacy india: order generic pills from india - best online pharmacy india
cross border pharmacy canada…
cross border pharmacy canada cheap meds from canada canada pharmacy online website shopping
https://mexivexpharma.com/#…
https://mexivexpharma.com/# mexican pharmacy las vegas
adderall canadian pharmacy:…
adderall canadian pharmacy: TrueNorthVitals - reliable canadian pharmacy reviews
is mexipharmacy legit order…
is mexipharmacy legit order mexican pharmaceuticals online mexican pharmacy online
safe canadian pharmacy:…
safe canadian pharmacy: truenorthvitals canada - ed meds online canada
canadian online pharmacy…
canadian online pharmacy reviews: legit canadian online pharmacy - buy drugs from canada
http://truenorthtabs.com/#…
http://truenorthtabs.com/# canada drug pharmacy
top online pharmacy india:…
top online pharmacy india: vedalytic pharma - india pharmacy
mexican drug stores or…
mexican drug stores or mexico pharmacy order online mexico pharmacy online
https://www.google.lu/url?sa=t&url=https://mexivexpharma.com online pharmacy in mexico and http://lostfilmhd.com/user/cjzkginlyb/ order medication from mexico
pharmacy in mexico city online mexico pharmacy and mexican meds meds from mexico
https://bactribatchindia.xyz…
https://bactribatchindia.xyz/# generic antibiotics online pharmacy
cheapest online pharmacy…
cheapest online pharmacy india or online pharmacy india top 10 pharmacies in india
https://maps.google.dz/url?q=https://vedalyticpharma.com online shopping pharmacy india and http://lostfilmhd.com/user/mwnigslqlh/ п»їlegitimate online pharmacies india
Online medicine order world pharmacy india and india pharmacy mail order pharmacy website india
mexico drug store online:…
mexico drug store online: Mexi Vex Pharma - medicine mexico
mexican pharmacies near me:…
mexican pharmacies near me: order mexican pharmaceuticals online - mexican pharmacy las vegas
https://getscriptkinetics…
https://getscriptkinetics.com/# new pharmacy online
best india pharmacy cheap…
best india pharmacy cheap indian generic meds indian pharmacy
prescriptions from mexico…
prescriptions from mexico and mexican drugstore mexican online pharmacies
https://www.google.ci/url?q=https://mexivexpharma.com farmacia mexicana online and https://www.trendyxxx.com/user/vyeqljkuhc/videos best online mexican pharmacy
order meds from mexico medication from mexico or mexican meds progreso, mexico pharmacy online
https://vedalyticpharma.com/…
https://vedalyticpharma.com/# online shopping pharmacy india
world pharmacy india and top…
world pharmacy india and top online pharmacy india top online pharmacy india
http://3gbug.com/gourl.asp?ve=2&ff=931&url=http_vedalyticpharma.com Online medicine home delivery and https://www.trendyxxx.com/user/vdqiqeerxk/videos pharmacy website india
online pharmacy india buy medicines online in india or mail order pharmacy india indian pharmacy paypal
canadapharmacyonline legit…
canadapharmacyonline legit and pharmacies in canada that ship to the us canadian pharmacy 365
https://toolbarqueries.google.si/url?sa=t&url=https://truenorthtabs.com canadian pharmacy scam and https://lifnest.site/user/qtjpyhpknbqtjpyhpknb/?um_action=edit canada pharmacy online legit
pharmacy canadian best canadian online pharmacy reviews and legit canadian online pharmacy canadian discount pharmacy
mexican pharmacys: cross…
mexican pharmacys: cross border mexican pharmacy - mexican pharmacy near me
mexico pharmacy: cross…
mexico pharmacy: cross border mexican pharmacy - medicine mexico
Pagination
Add new comment