You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Drupal中程序化创建页面?批量生成千级页面方案咨询

Best Approach to Generate 1000 Uniform Pages in Drupal

The most maintainable and Drupal-native way to handle this is to use custom content types + programmatic node creation + Pathauto for URL aliases. This leverages Drupal's core entity system, giving you built-in benefits like caching, permissions, and easy template customization. Here's a step-by-step breakdown:

1. Create a Custom Content Type

First, define a content type for your "name pages" to standardize the structure:

  • Go to Structure > Content types > Add content type
  • Name it something like "Name Page"
  • Add fields for the variable content (e.g., a "Bio Snippet" text field, or a "Unique ID" number field) alongside the default Title field (which will be "name1", "name2", etc.)
  • Save the content type

2. Configure URL Aliases with Pathauto

To get the exact URL structure you want (domain.com/names/name1.html):

  • Install and enable the Pathauto module (core-compatible, no external tools needed)
  • Go to Configuration > Search and metadata > URL aliases > Patterns
  • Add a new pattern for your "Name Page" content type:
    • Pattern: names/[node:title].html
    • This will automatically generate the correct alias for each node you create

3. Programmatically Create Nodes

You have two main options here, depending on whether you have existing data or need to generate sequential entries:

Option A: Generate Sequential Pages (name1 to name1000)

Use a custom Drush command or batch script to create nodes in bulk. Here's how to build a simple Drush command:

Step 1: Create a Custom Module

Create a module (e.g., name_generator) with these files:

name_generator.info.yml:

name: Name Page Generator
type: module
description: Generates bulk name pages for Drupal
core_version_requirement: ^9 || ^10
package: Custom

src/Command/GenerateNamePagesCommand.php:

<?php

namespace Drupal\name_generator\Command;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Commands\DrushCommands;
use Drupal\node\Entity\Node;

/**
 * Drush command to generate name pages.
 */
class GenerateNamePagesCommand extends DrushCommands {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new GenerateNamePagesCommand object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct();
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Generate 1000 name pages.
   *
   * @command name_generator:create-pages
   * @aliases ngcp
   * @usage name_generator:create-pages
   *   Creates 1000 name pages with sequential titles.
   */
  public function createPages() {
    $batch = [
      'title' => $this->t('Generating Name Pages'),
      'operations' => [],
      'finished' => '\Drupal\name_generator\Command\GenerateNamePagesCommand::batchFinished',
    ];

    // Add 1000 operations to the batch
    for ($i = 1; $i <= 1000; $i++) {
      $batch['operations'][] = [
        '\Drupal\name_generator\Command\GenerateNamePagesCommand::createSinglePage',
        [$i],
      ];
    }

    batch_set($batch);
    drush_backend_batch_process();
  }

  /**
   * Creates a single name page node.
   *
   * @param int $index
   *   The sequential index (1 to 1000).
   * @param array $context
   *   Batch context array.
   */
  public static function createSinglePage($index, &$context) {
    $title = "name{$index}";
    // Customize the variable content here
    $body_value = "This is the content for {$title}. Some minor unique detail: #{$index}";

    $node = Node::create([
      'type' => 'name_page', // Replace with your content type machine name
      'title' => $title,
      'body' => [
        'value' => $body_value,
        'format' => 'basic_html', // Use your desired text format
      ],
      // Add other fields here if needed, e.g., field_bio_snippet: 'Some text'
      'status' => 1, // Publish the node immediately
    ]);
    $node->save();

    $context['results'][] = $title;
    $context['message'] = t('Created page: @title', ['@title' => $title]);
  }

  /**
   * Batch finished callback.
   *
   * @param bool $success
   *   Whether the batch completed successfully.
   * @param array $results
   *   Results array.
   * @param array $operations
   *   Operations array.
   */
  public static function batchFinished($success, $results, $operations) {
    if ($success) {
      \Drupal::messenger()->addMessage(t('Successfully created @count name pages.', ['@count' => count($results)]));
    }
    else {
      $error_operation = reset($operations);
      \Drupal::messenger()->addError(t('An error occurred while processing @operation with arguments : @args', [
        '@operation' => $error_operation[0],
        '@args' => print_r($error_operation[1], TRUE),
      ]));
    }
  }

}

Step 2: Run the Command

  • Enable your module: drush en name_generator
  • Run the command: drush ngcp
  • This will create 1000 published nodes, each with a unique title and minor content differences, and Pathauto will automatically generate the correct URL aliases.

Option B: Import from a CSV File

If you have existing data (e.g., names and their unique details in a CSV), use Drupal's Migrate module (or the Migrate Plus/Migrate CSV contrib modules) to import nodes in bulk:

  1. Prepare your CSV with columns like title, body, field_bio_snippet
  2. Create a migration configuration that maps CSV columns to your content type fields
  3. Run the migration to create all nodes at once

4. Customize the Page Template

To ensure consistent formatting across all pages:

  • Create a custom template for your "Name Page" content type (e.g., node--name-page.html.twig)
  • Add your unified HTML structure, inserting the variable fields where needed (e.g., {{ content.body }}, {{ node.field_bio_snippet.value }})
  • Place this template in your theme's templates directory to override the default node template

Why This Approach Works

  • Maintainable: Nodes are Drupal's native content entities, so you can edit, delete, or update pages later using Drupal's admin interface
  • Scalable: Batch processing prevents timeouts/memory issues when creating 1000 pages
  • Consistent: Pathauto ensures uniform URLs, and templates enforce consistent page structure
  • Extensible: You can add fields, permissions, or caching rules later without rewriting the entire system

内容的提问来源于stack exchange,提问作者user7299543

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:27:59