Creating Custom Content Types Programmatically in Drupal 10

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:

  1. A working Drupal 10 or 9 website.
  2. Basic knowledge of Drupal site building and module development.
  3. 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:

  1. In your Drupal installation, navigate to the sites/all/modules directory (or your preferred module location).
  2. Create a new directory for your custom module. Give it a unique and descriptive name, such as my_custom_content_type.
  3. 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:
  - node

This 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.

  1. Create a file named my_custom_content_type.module in your module directory.
  2. 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.

  1. 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

  1. In your Drupal admin interface, navigate to Extend (admin/modules) and enable your custom module, "My Custom Content Type Module."
  2. 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.

Share on social media

Comment

Permalink

Нужны духи? купить духи в Минске недорого с удобным выбором ароматов для мужчин и женщин. Вниманию покупателей представлены свежие, цитрусовые, древесные, цветочные и восточные композиции. Сравните предложения магазинов и найдите парфюм, который соответствует вашим предпочтениям.

Permalink

Нужны духи? чем отличается духи от туалетной воды с удобным выбором ароматов для мужчин и женщин. Вниманию покупателей представлены свежие, цитрусовые, древесные, цветочные и восточные композиции. Сравните предложения магазинов и найдите парфюм, который соответствует вашим предпочтениям.

Permalink

A practical review of buy tiktok views should begin with how the buyer will record order status, delivery changes, support messages, and the final observed count. Document the steps, limits, and support options connected with checkout accuracy for the target URL, package size, total charge, currency, and recurring billing status. A transparent method makes it easier to choose another option when the documented conditions do not meet the brief.

Permalink

DarLink AI Review https://darlink-ai.carrd.co a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review darlink-ai.carrd.co a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

The first step in evaluating buy tiktok views and likes is to map whether the goal is a limited vendor test, a presentation metric, or another documented use. Do not finalize the choice until there is enough clarity about whether a dashboard clearly identifies the service, target, quantity, status, and support reference. That process helps the user compare a repeat test with the same timing and measurement definitions.

Permalink

A practical review of buy tiktok views and likes should begin with whether the goal is a limited vendor test, a presentation metric, or another documented use. Document the steps, limits, and support options connected with the privacy and security implications of every field requested during checkout or support contact. A transparent method makes it easier to judge the transaction by its written scope without claiming unrelated marketing effects.

Permalink

A shortlist for buy tiktok views and likes is easier to build after confirming the expected dashboard, receipt, order reference, and communication process. Look beyond promotional claims and verify the difference between a provider's operational delivery and the campaign's unrelated marketing outcomes. The resulting notes make it easier to choose another option when the documented conditions do not meet the brief.

Permalink

Questions about buy real tiktok views become easier to answer once the user specifies the expected dashboard, receipt, order reference, and communication process. A small controlled test can reveal more about the privacy and security implications of every field requested during checkout or support contact. The comparison remains useful when it can decide whether the provider should remain on the shortlist after a small test.

Permalink

Before choosing among buy real tiktok views, write down the quantity boundaries that keep the first order small enough to evaluate. The strongest comparison separates assumptions from evidence about how the service describes natural variation, count movement, and conditions outside its control. The chosen option should make it possible to evaluate the final status after the planned observation period rather than at a convenient moment.

Permalink

A structured comparison of buy real tiktok views should first address which proof should be saved before checkout, during delivery, and after completion. Before committing, confirm checkout accuracy for the target URL, package size, total charge, currency, and recurring billing status. The review is complete when the user can compare providers using the same evidence instead of headline price alone.

Permalink

A structured comparison of buy tiktok views should first address the acceptable timing range and clear stop conditions for an incorrect or delayed order. Before committing, confirm screenshots with timestamps, the order confirmation, payment reference, dashboard status, and support correspondence. The review is complete when the user can make a reversible decision based on a limited, traceable transaction.

Permalink

Помогаем попасть https://bemodel.io в модельный бизнес и организовать работу над профессиональным портфолио. Изучайте предложения агентств, следите за новыми кастингами, отправляйте заявки и находите возможности для сотрудничества.

Permalink

DarLink AI Review https://darlink-ai.carrd.co a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co/ a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co/ a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co/ a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co/ a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co/ a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

DarLink AI Review https://darlink-ai.carrd.co/ a detailed guide to setting up your virtual character and testing your memory within 48 hours. We analyze the creation of a profile, individual communication settings, saving dialogue details and the ability of artificial intelligence to remember information.

Permalink

Нужны духи? какой парфюм купить с удобным выбором ароматов для мужчин и женщин. Вниманию покупателей представлены свежие, цитрусовые, древесные, цветочные и восточные композиции. Сравните предложения магазинов и найдите парфюм, который соответствует вашим предпочтениям.

Permalink

Нужны духи? духи туфелька с удобным выбором ароматов для мужчин и женщин. Вниманию покупателей представлены свежие, цитрусовые, древесные, цветочные и восточные композиции. Сравните предложения магазинов и найдите парфюм, который соответствует вашим предпочтениям.

Add new comment