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

Gram tu od jakichs czterech miesiecy, to chyba moge cos napisac. Wpadlem tam przez znajomego z pracy, bo mialem dosc gdzie sloty laduja sie normalnie. Gier jest tam od groma — gdzies kolo 3500 pozycji, co prawda i tak grywam glownie na kilku. Pragmatic, NetEnt oraz Betsoft maja tam najwiecej swojego, no i klasyki typu Gates of Olympus, Sweet Bonanza i Book of Dead.

Pakiet powitalny w Fiery Play to 100% do pierwszego depozytu do 2000 zl razem z 200 free spinow, rozbitych na trzy dni. Wymog obrotu wynosi x35, czyli standardowo, nic rewelacyjnego, ale zerknijcie na limity — limit stawki podczas obrotu jest ograniczony i mozna to przegapic. Byl tez jakis kod bez depozytu na 60 darmowych obrotow, ale nie wiem czy dalej dziala. Biezace oferty widac na [url=https://kargoport.com.tr/fieri-play-krok-po-kroku-rejestracja-i-weryfik…] jak was to interesuje.

Zakladanie konta to bylo doslownie chwila, najnizsza wplata wynosi 50 zl. Laduje karta Visa albo przez Skrilla, wyplaty robilem pare razy — zwykle w granicach doby, ale raz meczylem sie dwoch dni, bo weryfikacja dokumentow. Bitcoin tez obsluguja i to leci najszybciej.

Sekcja live to dla mnie najwiekszy plus — to Evolution stoi za stolami, na kilku stolach sa nasi krupierzy, co sie rzadko trafia. Crazy Time oczywiscie jest, ale to loteria, nie strategia. Blackjack i bakarat maja przyzwoity wybor.

Nie ma osobnej aplikacji, ale strona na telefonie i szczerze — nie brakuje mi jej, chociaz na LTE w tramwaju live czasem przycina. Maja papiery z Curacao, co nie jest MGA, wiadomo — u mnie kasa zawsze przyszla. Support Fiery Play odpisuje po polsku choc pierwsze dwie odpowiedzi to bot. Najbardziej denerwuje mnie to spam promocyjny na maila — wylaczylem w ustawieniach i spokoj.

Permalink

Siedze na Mostbet jakies pol roku, w wiekszosci sloty, sporadycznie cos z live. Wpadlem tam bo kolega z pracy gadal o tym, szczerze mowiac bez entuzjazmu. Katalog jest naprawde duzy — gdzies 3000 gier, Pragmatic, NetEnt, Microgaming. Gates of Olympus mam w ulubionych, chociaz ostatnio ogrywam Big Time Gaming.

Jedna rzecz mnie denerwuje to nawigacja w kategoriach — potrafi zwrocic bzdury. Poza tym jakos zyje. Sekcja live to glownie Evolution i w tym miejscu jest naprawde dobrze — stoly blackjacka sa tez PL, a Monopoly Live wciaga bardziej niz wiekszosc automatow.

Pakiet powitalny w Mostbet to mniej wiecej 125% i do tego paczka 250 spinow, wrzucane partiami. Warunek obrotu to x60, czyli standard, ale nie prezent, min. depozyt okolo 20 zlotych. Gdyby ktos potrzebowal biezacych promocji, to zajrzyj na [url=https://4threv.co.uk/mostbet-voucher-free-spins-2026-przewodnik-po-apli…] zanim zalozysz konto. Rejestracja trwala doslownie chwile, potwierdzenie dokumentow dzien roboczy.

Wyplacam najczesciej na Skrilla, schodzi w pol dnia. Karta czekalem dwa dni, crypto ponoc idzie najszybciej, nie testowalem osobiscie. Neteller tez jest, oraz e-portfele.

Czat w Mostbet jest po polsku, ale momentami brzmi jak z translatora. Raz mialem problem z zaliczeniem obrotu — zalatwili to dosc sprawnie. Dzialaja na licencji Curacao, wiec bez euforii. Apka na Androida nie krzaczy sie, choc instalka na iPhone to lekka gimnastyka.

Permalink

Gram tu od zeszlej jesieni, wiec chyba moge cos napisac. Trafilem tu przez kumpla, co wczesniej gral na jakiejs budce bez licencji. Lobby na 888starz jest naprawde spore — mowimy o ponad 4000 automatow, choc umowmy sie i tak grasz w te same 10 gier. Play'n GO, Pragmatic i Yggdrasil zajmuja wiekszosc polki, Sweet Bonanza i Book of Dead wisza w topce.

Oferta na start kreci sie wokol 100% doladowania plus jakies 30 free spinow. Wpisujac 888starz kod promocyjny przy rejestracji warunki sa odrobine lepsze. Natomiast jedna rzecz mnie wkurzyla — obrot bonusem to x40, w dodatku czas leci szybko. Za pierwszym razem mi przepadlo. Swieze kody na 2026 wisza na [url=https://idplugofficial.com/888starz-czy-legalny-w-polsce-przeglad-licen…] zanim sie zarejestrujecie.

Wplaty schodza od 20 zl, Visa, Mastercard, Skrill, Neteller — wszystko jest. Ja wyplacam w krypto bo nie czekam na weryfikacje banku. Wyplaty w 888starz na e-wallet leci tego samego dnia, natomiast przelew na karte to juz loteria czasowa. Dokumenty trzeba wrzucic — zatwierdzili nastepnego dnia.

Sekcja live to glownie Evolution i naprawde czuc roznice. Crazy Time, Lightning Roulette, blackjack — krupierzy realni, jakosc obrazu ok. Szkoda tylko ze po polsku prawie nic nie ma. Z komorki gram najczesciej i nie ma zaciec, dostepna jest appka, ale ja jej nie uzywam.

Support 888starz odpowiada po polsku i to nie brzmi jak automat. Raz czekalem kwadrans, zwykle szybciej. Dzialaja na licencji Curacao, wiec nie jest to UKGC, warto wiedziec. Mnie osobiscie nie przeszkadza, ale trzeba miec to z tylu glowy.

Add new comment