Crafting Greeting Cards: A Dive into Programmatic Content Creation in Drupal 9

Welcome to a realm where Drupal's prowess meets the artistry of content creation!

Are you eager to weave custom content types and wield fields effortlessly within Drupal 9? Join us on an exhilarating journey as we unravel the mystique of programmatic content creation within the Drupal ecosystem.

In this voyage, we'll uncover the power of code—specifically, an ingenious script that automates the establishment of a bespoke content structure for crafting personalized greeting cards. Picture this: You desire a content type tailored for these cards, complete with image thumbnails, PDF attachments, and a taxonomy for categorization. But instead of navigating through Drupal's graphical interface, we'll take a different route—code!

This blog post brings you an insightful piece of code wizardry. Imagine a script that, when triggered, conjures a custom content type titled "Greeting Cards" into existence. It extends its magic by crafting dedicated fields for thumbnail images and PDF attachments seamlessly. Not stopping there, it forges a taxonomy vocabulary, ready to organize these cards into distinct categories.

And the pièce de résistance? An engaging conclusion that guides you through the post-installation steps. It prompts you to fine-tune the content type's form display, ensuring a seamless user experience. A personalized message from Drupal's messenger warmly greets you, directing you to a configuration page, urging a few swift drags and drops to enable key fields—PDF and Category.

Are you ready to embrace the elegance of programmatic content creation in Drupal 9? Let's dive in, unraveling the enchanting code and guiding you through its execution, making the creation of greeting cards an enchanting, automated experience.

Step 1: Defining the Greeting Cards Content Type The journey begins by defining a new content type, "Greeting Cards," capable of holding the essence of your card creations. It's a simple command that sets the stage for what's to come.

// Define the content type.
$content_type = [
  'type' => 'greeting_cards',
  'name' => 'Greeting Cards',
  'description' => 'Content type for creating thumbnail from pdf cards',
  'base_table' => 'node',
  'custom' => TRUE,
];
$content_type = NodeType::create($content_type);
$content_type->save();

Step 2: Crafting Fields for Thumbnails and PDF Attachments Next, we venture into the creation of two essential fields: field_thumbnail_image for captivating thumbnail images and field_pdf for attaching PDFs to your greeting cards. Watch as these fields seamlessly intertwine with our content type.

// Define the field settings for thumbnail image.
$field_storage = FieldStorageConfig::create([
  'field_name' => 'field_thumbnail_image',
  'entity_type' => 'node',
  'type' => 'image',
]);
$field_storage->save();

// Create the field instance for thumbnail image.
$field = FieldConfig::create([
  'field_name' => 'field_thumbnail_image',
  'entity_type' => 'node',
  'bundle' => 'greeting_cards',
  'label' => 'Thumbnail Image',
  'required' => FALSE,
  'settings' => [
    'file_extensions' => 'png jpg jpeg',
  ],
]);
$field->save();

// Define the field settings for the PDF field.
$field_storage = FieldStorageConfig::create([
  'field_name' => 'field_pdf',
  'entity_type' => 'node',
  'type' => 'file',
]);
$field_storage->save();

// Create the field instance for PDF.
$field = FieldConfig::create([
  'field_name' => 'field_pdf',
  'entity_type' => 'node',
  'bundle' => 'greeting_cards',
  'label' => 'PDF',
  'required' => FALSE,
  'settings' => [
    'file_extensions' => 'pdf',
  ],
]);
$field->save();

Step 3: Establishing a Taxonomy Vocabulary for Categorization With our content type taking shape, we pivot to organize these greeting cards. Behold the birth of a taxonomy vocabulary named "Card Categories," ready to harmonize and categorize your card creations.

// Define the vocabulary for card categories.
$vocabulary = Vocabulary::create([
  'vid' => 'card_categories',
  'name' => 'Card Categories',
  'description' => 'Vocabulary for organizing card categories.',
]);
$vocabulary->save();

Step 4: Creating a Term Reference Field for Categorization To complete our content type's structure, a term reference field, field_category, is sculpted, linking each greeting card to its rightful category within the taxonomy vocabulary.

// Add a term reference field to the content type.
$field_storage = FieldStorageConfig::create([
  'field_name' => 'field_category',
  'entity_type' => 'node',
  'type' => 'entity_reference',
  'settings' => [
    'target_type' => 'taxonomy_term',
  ],
]);
$field_storage->save();

// Create the field instance for Category.
$field = FieldConfig::create([
  'field_name' => 'field_category',
  'entity_type' => 'node',
  'bundle' => 'greeting_cards',
  'label' => 'Category',
  'required' => FALSE,
  'settings' => [
    'handler' => 'default:taxonomy_term',
    'handler_settings' => [
      'target_bundles' => ['card_categories'],
    ],
  ],
]);
$field->save();

And as our code gracefully concludes its wizardry, a friendly Drupal messenger message awaits! It warmly guides you through the final steps—configuring the content type's form display. Follow the link to the Form Display configuration page, where a few swift drags and drops enable the vital fields—PDF and Category.

Share on social media

Add new comment