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
best canadian pharmacy and…
best canadian pharmacy and canadian pharmacy compare safe canadian pharmacy
http://andreasgraef.de/url?q=https://canpointerx.com:: top canadian pharmacyand https://brueckrachdorf.de/user/wzkbxbjfao/ the canadian pharmacy
safe online pharmacies in canada legit canadian pharmacyor canadian pharmacy online canadian pharmacy meds
https://zenithindusrx.shop/#…
https://zenithindusrx.shop/# indian pharmacy delivery to usa
best rated canadian pharmacy…
best rated canadian pharmacy: CanPointe - the canadian pharmacy
http://canpointerx.com/#…
http://canpointerx.com/# online pharmacy canada
online medicine booking:…
online medicine booking: ZenithIndusRx - buy medicines online in india
generic medicine store…
generic medicine store online: ordering medicines online - e pharmacy in india
indian pharmacies that ship…
indian pharmacies that ship to usa: ZenithIndus Rx - reputable indian pharmacies online
canadian pharmacy world and…
canadian pharmacy world and canadian pharmacies online trusted canadian pharmacy
http://www.cabinsonline.com/email_a_friend.php?referralPage=canpointerx… canadian pharmacies shipping to usa and https://lifnest.site/user/tkjfxpajyztkjfxpajyz/?um_action=edit canadian pharmacy website
canadian pharmacy meds canada rx pharmacy and canadian online pharmacy best rated canadian pharmacy
mexico pharmacy: reliable…
mexico pharmacy: reliable mexican pharmacies - mexican pharmacy that ships to usa
canadian drug pharmacy:…
canadian drug pharmacy: CanPointe - canadian pharmacies that deliver to the us
http://zenithindusrx.com/#…
http://zenithindusrx.com/# best indian pharmacy
mexican pharma and…
mexican pharma and prescriptions from mexico mexican pharmacy store
http://images.google.co.id/url?sa=t&url=http://fronteracaredirect.shop mexican pharmacy ship to usa and http://la-maison-des-amis.com/user/qxywbfxjrs/ best mexican online pharmacies
mexican medicine store mexican pharmacies that ship or farmacia mexicana en linea order antibiotics from mexico
order medicine from india to…
order medicine from india to usa ZenithIndusRx indian pharmacy website
https://zenithindusrx.com/#…
https://zenithindusrx.com/# rx india online shopping
reputable indian pharmacies…
reputable indian pharmacies or medicine online order online medicine shopping india
https://cse.google.com.pr/url?q=https://zenithindusrx.shop online shopping medicine and https://boyerstore.com/user/qkwmfynslb/?um_action=edit international drug mart
online chemists prescription drugs from india or best medicine website medicine online order
canadian pharmacy online:…
canadian pharmacy online: CanPointe - reputable canadian pharmacy
best mexican online…
best mexican online pharmacies: FronteraCareDirect - mexican pharmacy online reviews
canadian pharmacy rx or safe…
canadian pharmacy rx or safe canadian pharmacy real canadian pharmacy
http://www.fsou.com/redirect/index.asp?url=https://canpointerx.shop cipa canada online pharmacy or https://shockingbritain.com/user/ifikealeqy/ canadian pharmacies shipping to usa
canadian pharmacy legit canadian online pharmacy or my canadian pharmacy canadian pharmacy rx
https://canpointerx.com/#…
https://canpointerx.com/# canadian pharmacy official site
best indian pharmacy online…
best indian pharmacy online reviews: best indian pharmacy online - indian online pharmacy USA
canadian online pharmacy:…
canadian online pharmacy: CanPointeRx - online pharmacy canada
mexican meds:…
mexican meds: FronteraCareDirect - order meds from mexico
mexican online mail order…
mexican online mail order pharmacy and online pharmacies in mexico order medication from mexico
https://horizoninteractiveawards.com/index.php?URL=http://fronteracared… mexican online pharmacy and https://www.trendyxxx.com/user/jbllhvmlfx/videos pharmacia mexico
mexican pharmacies that ship largest online pharmacies in mexico and meds from mexico farmacia mexicana online
india drug store:…
india drug store: ZenithIndus Rx - buy medicine online in india
https://canpointerx.com/#…
https://canpointerx.com/# canadian pharmacy ltd
legit canadian pharmacy:…
legit canadian pharmacy: CanPointe Rx - canada rx pharmacy
cheap prescription drugs…
cheap prescription drugs india or indian pharmacy legit legitimate indian pharmacy online
http://www.masekaihatsu.com/feed2js/feed2js.php?src=https://zenithindus… legitimate indian pharmacy online and https://allchoicesmatter.org/user/iskrxwmeqf/?um_action=edit online medical shopping
order medicine online in india buy generic medicines online india and indian online pharmacy ship to usa online medicine delivery app in india
canadian pharmacy compare:…
canadian pharmacy compare: canadian pharmacy king - canadian pharmacy king
pharmacies in mexico that…
pharmacies in mexico that ship to usa: FronteraCare Direct - mexican meds
http://fronteracaredirect…
http://fronteracaredirect.com/# purple pharmacy mexico
trusted canadian pharmacy or…
trusted canadian pharmacy or reputable canadian pharmacy safe online pharmacies in canada
https://cse.google.co.ck/url?sa=t&url=https://canpointerx.shop canadian pharmacy official website or http://lenhong.fr/user/tmzlnnzoub/ canadian pharmacy world
canadian pharmacies that deliver to the us my canadian pharmacy and canadian online pharmacy reputable canadian online pharmacy
https://canpointerx.shop/#…
https://canpointerx.shop/# online canadian pharmacy
canadian pharmacy official…
canadian pharmacy official site: canada online pharmacy - legitimate canadian pharmacy
online medicine websites and…
online medicine websites and best medicine website prescription drugs in india
https://toolbarqueries.google.co.bw/url?q=https://zenithindusrx.shop buy medicine online or https://polyphonia.eu/forum/index.php?action=profile;u=124699 india rx
india pharmacy international shipping online medicine delivery app in india and generic medicine buy online generic medicine app india
indian pharmacy official…
indian pharmacy official website: online medicine websites - best indian pharmacy online
buy generic medicines online…
buy generic medicines online india: medicine online delivery - generic medicine store online
Pagination
Add new comment