Přejít k hlavnímu obsahu

PHP Drupal modul

text hard

Generování kompletního Drupal 11 modulu — controller, routing, service, DB schema, info.yml.

Prompt

Create a complete Drupal 11 module called "hello_world_benchmark". It must include: an info.yml file, a routing.yml with a hello page at /hello-world-benchmark, a controller that returns "Hello World from AI Arena benchmark!", a services.yml with a simple service, and an install file with a database schema for a table "hello_benchmark_messages". Use proper Drupal 11 namespacing and PHP 8.3 syntax with readonly properties.

Výsledky napříč modely

Model Run Tokens/s Délka (s) Tokeny GPU VRAM OK Kvalita Datum Výstup
deepseek-coder-v2:16b #1 16.09 112.2 1125 15476 MB - 19.05.2026
qwen3-coder:30b #1 10.80 140.6 1495 15458 MB - 19.05.2026
gemma4:e4b #1 18.88 105.3 1951 14292 MB - 19.05.2026
deepseek-coder-v2:16b #1 17.26 83.1 1386 15536 MB - 19.05.2026
qwen3-coder:30b #1 10.31 190.3 1111 15461 MB - 18.05.2026
deepseek-coder-v2:16b #1 17.69 86.8 921 15552 MB - 18.05.2026
gemma4:e4b #1 19.89 91.6 1787 14310 MB - 18.05.2026

Vygenerované výstupy

deepseek-coder-v2:16b Run #1 · 1125 tok · 16.1 tok/s
 To create a complete Drupal 11 module called `hello_world_benchmark`, you'll need to set up several files including an info file, routing configuration, controller, services definition, and an install file for the database schema. Below is a step-by-step guide on how to structure these files within your module directory:

### 1. Module Directory Structure
Your module should be placed under `modules/custom` or `web/modules/custom` (depending on your Drupal installation setup). For this example, let's assume you are placing it under `modules/custom`.

```
hello_world_benchmark/
├── hello_world_benchmark.info.yml
├── src/
│   └── Controller/
│       └── HelloWorldBenchmarkController.php
├── controllers/
│   └── HelloWorldBenchmarkController.php
├── hello_world_benchmark.services.yml
├── hello_world_benchmark.install
└── hello_world_benchmark.routing.yml
```

### 2. `hello_world_benchmark.info.yml`
This file contains basic information about your module:

```yaml
name: 'Hello World Benchmark'
type: module
description: 'A simple module to demonstrate Drupal 11 module creation.'
core_version_requirement: ^8 || ^9 || ^10 || ^11
package: Custom
dependencies:
  - drupal:node
```

### 3. `hello_world_benchmark.routing.yml`
This file defines the route for your module's hello world page:

```yaml
hello_world_benchmark.hello:
  path: '/hello-world-benchmark'
  defaults:
    _controller: '\Drupal\hello_world_benchmark\Controller\HelloWorldBenchmarkController::hello'
  methods: GET
  requirements:
    _permission: 'access content'
```

### 4. `src/Controller/HelloWorldBenchmarkController.php` or `controllers/HelloWorldBenchmarkController.php`
This file contains the controller class, which will handle the request for your hello world page:

```php
<?php

namespace Drupal\hello_world_benchmark\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloWorldBenchmarkController extends ControllerBase {

  public function hello(): array {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('Hello World from AI Arena benchmark!'),
    ];
  }
}
```

### 5. `hello_world_benchmark.services.yml`
This file defines a simple service if needed:

```yaml
services:
  hello_world_benchmark.example_service:
    class: Drupal\hello_world_benchmark\ExampleService
    arguments: ['@config.factory']
```

### 6. `hello_world_benchmark.install`
This file contains the database schema for the table `hello_benchmark_messages`:

```php
<?php

namespace Drupal\hello_world_benchmark\Install;

use Drupal\Core\Entity\Schema\SqlContentEntityBaseSchemaHandlerInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\node\Entity\NodeType;
use Symfony\Component\DependencyInjection\ContainerInterface;

class HelloWorldBenchmarkInstall implements SqlContentEntityBaseSchemaHandlerInterface {
  use MessengerTrait;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  public function __construct(Connection $database) {
    $this->database = $database;
  }

  public static function create(ContainerInterface $container) {
    return new static($container->get('database'));
  }

  /**
   * {@inheritdoc}
   */
  public function getSchema() {
    $schema['hello_benchmark_messages'] = [
      'description' => 'Stores benchmark messages.',
      'fields' => [
        'id' => [
          'type' => 'serial',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ],
        'message' => [
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
        ],
      ],
      'primary key' => ['id'],
    ];

    return $schema;
  }
}
```

### 7. Enable the Module
After setting up all files, you need to enable your module through the Drupal admin interface or using Drush:

```bash
drush en hello_world_benchmark -y
```

This setup provides a basic structure for your custom Drupal module. You can expand this by adding more functionality and services as needed.
qwen3-coder:30b Run #1 · 1495 tok · 10.8 tok/s
Here's a complete Drupal 11 module called "hello_world_benchmark" with all the requested components:

### hello_world_benchmark.info.yml
```yaml
name: Hello World Benchmark
type: module
description: A benchmark module for demonstration purposes
core_version_requirement: ^11
package: Custom
```

### hello_world_benchmark.routing.yml
```yaml
hello_world_benchmark.hello:
  path: '/hello-world-benchmark'
  defaults:
    _controller: '\Drupal\hello_world_benchmark\Controller\HelloWorldController::hello'
    _title: 'Hello World Benchmark'
  requirements:
    _access: 'TRUE'
```

### src/Controller/HelloWorldController.php
```php
<?php

namespace Drupal\hello_world_benchmark\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;

/**
 * Hello world controller.
 */
class HelloWorldController extends ControllerBase {

  /**
   * Returns a simple hello world response.
   */
  public function hello(): Response {
    return new Response('Hello World from AI Arena benchmark!');
  }

}
```

### src/Service/HelloWorldService.php
```php
<?php

namespace Drupal\hello_world_benchmark\Service;

/**
 * Simple service for hello world benchmark.
 */
class HelloWorldService {

  /**
   * Returns a greeting message.
   */
  public function getGreeting(): string {
    return 'Hello World from AI Arena benchmark!';
  }

}
```

### hello_world_benchmark.services.yml
```yaml
services:
  hello_world_benchmark.hello_world_service:
    class: Drupal\hello_world_benchmark\Service\HelloWorldService
    public: true
```

### src/Plugin/Block/HelloWorldBlock.php
```php
<?php

namespace Drupal\hello_world_benchmark\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\hello_world_benchmark\Service\HelloWorldService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'HelloWorldBlock' block.
 *
 * @Block(
 *   id = "hello_world_benchmark_block",
 *   admin_label = @Translation("Hello World Benchmark Block"),
 * )
 */
class HelloWorldBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The hello world service.
   */
  protected readonly HelloWorldService $helloWorldService;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, HelloWorldService $hello_world_service) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->helloWorldService = $hello_world_service;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('hello_world_benchmark.hello_world_service')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $message = $this->helloWorldService->getGreeting();
    return [
      '#markup' => $message,
    ];
  }

}
```

### hello_world_benchmark.install
```php
<?php

/**
 * @file
 * Install, update and uninstall functions for the hello_world_benchmark module.
 */

use Drupal\Core\Database\Database;

/**
 * Implements hook_schema().
 */
function hello_world_benchmark_schema(array &$schema) {
  $schema['hello_benchmark_messages'] = [
    'description' => 'Stores benchmark messages.',
    'fields' => [
      'id' => [
        'description' => 'The primary identifier for a message.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'message' => [
        'description' => 'The message content.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'created' => [
        'description' => 'The timestamp when the message was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'updated' => [
        'description' => 'The timestamp when the message was last updated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => ['id'],
  ];
}
```

### hello_world_benchmark.module
```php
<?php

/**
 * @file
 * Main module file for hello_world_benchmark.
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function hello_world_benchmark_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'hello_world_benchmark.hello':
      return '<p>' . t('This is the hello world benchmark page.') . '</p>';
  }
}
```

### composer.json (if needed for module dependencies)
```json
{
    "name": "drupal/hello_world_benchmark",
    "description": "A benchmark module for demonstration purposes",
    "type": "drupal-module",
    "require": {
        "php": ">=8.3"
    },
    "autoload": {
        "psr-4": {
            "Drupal\\hello_world_benchmark\\": "src/"
        }
    }
}
```

### Directory Structure
```
hello_world_benchmark/
├── hello_world_benchmark.info.yml
├── hello_world_benchmark.routing.yml
├── hello_world_benchmark.install
├── hello_world_benchmark.module
├── src/
│   ├── Controller/
│   │   └── HelloWorldController.php
│   ├── Plugin/
│   │   └── Block/
│   │       └── HelloWorldBlock.php
│   └── Service/
│       └── HelloWorldService.php
└── hello_world_benchmark.services.yml
```

This module includes:

1. **Module Info File** - Defines the module with proper Drupal 11 naming conventions
2. **Routing File** - Defines the `/hello-world-benchmark` route
3. **Controller** - Returns the required "Hello World from AI Arena benchmark!" message
4. **Service** - A simple service with proper namespacing
5. **Database Schema** - Creates a `hello_benchmark_messages` table with id, message, created, and updated fields
6. **Block Plugin** - Demonstrates service usage in a block
7. **Proper PHP 8.3 Syntax** - Uses readonly properties where appropriate
8. **Drupal 11 Namespacing** - Follows Drupal 11 coding standards

To install:
1. Place the module in `modules/custom/hello_world_benchmark`
2. Enable the module via `drush en hello_world_benchmark` or through the UI
3. Visit `/hello-world-benchmark` to see the page
4. The database table will be created automatically during module installation
gemma4:e4b Run #1 · 1951 tok · 18.9 tok/s
This is a complete, modern Drupal 11 module structure.

## Module Structure

```
hello_world_benchmark/
├── hello_world_benchmark.info.yml
├── hello_world_benchmark.services.yml
├── hello_world_benchmark.routing.yml
├── src/
│   ├── Controller/
│   │   └── HelloWorldBenchmarkController.php
│   └── Form/
│       └── InstallSchemaForm.php  // Using a form/hook approach for schema
└── hello_world_benchmark.install
```

***

## 1. `hello_world_benchmark.info.yml`

This file provides basic metadata about the module.

```yaml
# hello_world_benchmark/hello_world_benchmark.info.yml
name: 'Hello World Benchmark'
type: module
description: 'A benchmark module demonstrating modern Drupal 11 development practices.'
package: Custom
core_version_requirement: ^11
version: 1.0
```

## 2. `hello_world_benchmark.services.yml`

This file registers a simple service.

```yaml
# hello_world_benchmark/hello_world_benchmark.services.yml
services:
  hello_world_benchmark.calculator:
    class: Drupal\hello_world_benchmark\CalculatorService
    arguments: ['@database']
```

### `src/CalculatorService.php` (The Service Class)

We need to create the service class file.

```php
<?php

namespace Drupal\hello_world_benchmark;

/**
 * A simple service to demonstrate dependency injection.
 */
class CalculatorService {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  private readonly \Drupal\Core\Database\Connection $database;

  /**
   * Constructs a CalculatorService object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection service.
   */
  public function __construct(\Drupal\Core\Database\Connection $database) {
    $this->database = $database;
  }

  /**
   * Adds a sample record to the benchmark table.
   *
   * @param string $message
   *   The message content to record.
   *
   * @return bool
   *   TRUE if the record was inserted successfully.
   */
  public function recordBenchmark(string $message): bool {
    try {
      $this->database->insert('hello_benchmark_messages')
        ->fields([
          'message_content' => $message,
          'created' => time(),
        ])
        ->execute();
      return true;
    } catch (\Exception $e) {
      \Drupal::logger('hello_world_benchmark')->error('Failed to record benchmark message: @error', ['@error' => $e->getMessage()]);
      return false;
    }
  }
}
```

## 3. `hello_world_benchmark.routing.yml`

This defines the path and maps it to the controller.

```yaml
# hello_world_benchmark/hello_world_benchmark.routing.yml
hello_world_benchmark.page:
  path: '/hello-world-benchmark'
  defaults:
    _controller: '\Drupal\hello_world_benchmark\Controller\HelloWorldBenchmarkController::content'
    _title: 'Hello World Benchmark'
  requirements:
    _permission: 'access content'
```

### `src/Controller/HelloWorldBenchmarkController.php` (The Controller)

The controller handles the request and returns the "Hello World" content.

```php
<?php

namespace Drupal\hello_world_benchmark\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\hello_world_benchmark\CalculatorService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;

/**
 * Returns responses for the Hello World Benchmark module.
 */
class HelloWorldBenchmarkController extends ControllerBase {

  /**
   * The CalculatorService instance.
   *
   * @var \Drupal\hello_world_benchmark\CalculatorService
   */
  private readonly CalculatorService $calculatorService;

  /**
   * Constructs a HelloWorldBenchmarkController object.
   *
   * @param CalculatorService $calculatorService
   *   The calculator service.
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container.
   */
  public function __construct(CalculatorService $calculatorService, ContainerInterface $container) {
    $this->calculatorService = $calculatorService;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('hello_world_benchmark.calculator'),
      $container
    );
  }

  /**
   * Displays the "Hello World" message and uses the service.
   *
   * @return array
   *   A render array containing the success message.
   */
  public function content(): array {
    // Use the injected service to demonstrate functionality.
    $message = 'Hello World from AI Arena benchmark!';
    $this->calculatorService->recordBenchmark($message);

    return [
      '#markup' => $this->t('<h1>@message</h1><p>This page was successfully rendered using a custom controller and service injection.</p>', ['@message' => $message]),
    ];
  }

}
```

## 4. `hello_world_benchmark.install` (Database Schema)

This file runs the database schema creation when the module is installed.

```php
<?php

/**
 * Implements hook_install().
 *
 * Creates the 'hello_benchmark_messages' table.
 */
function hello_world_benchmark_install() {
  $schema = \Drupal::database()->schema();

  // Define the table structure.
  $schema['hello_benchmark_messages'] = [
    'field' => [
      'description' => 'Stores benchmark messages.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
    ],
    'message_content' => [
      'type' => 'varchar',
      'length' => 500,
      'not null' => TRUE,
    ],
    'created' => [
      'type' => 'int',
      'size' => 'big',
      'not null' => TRUE,
    ],
  ];

  try {
    $transaction = \Drupal::database()->startTransaction();
    $schema->createTable('hello_benchmark_messages');
    \Drupal::messenger()->addStatus('The "hello_benchmark_messages" table has been created successfully.');
  } catch (\Exception $e) {
    \Drupal::logger('hello_world_benchmark')->error('Failed to create table: @error', ['@error' => $e->getMessage()]);
  }
}
```

***

## Summary of Modern Features Used

1.  **PHP 8.3 Syntax:** Used `readonly` properties in `HelloWorldBenchmarkController` and `CalculatorService`.
2.  **Namespacing:** Strict use of `Drupal\hello_world_benchmark` namespace.
3.  **Dependency Injection (DI):** The controller and service both receive their dependencies (`CalculatorService`, `ContainerInterface`) through the constructor, adhering to modern Drupal best practices.
4.  **Separation of Concerns:** Logic is separated into dedicated Controller, Service, and Install files.
5.  **Database Schema:** Schema creation is handled cleanly in `hello_world_benchmark.install`.
deepseek-coder-v2:16b Run #1 · 1386 tok · 17.3 tok/s
 To create a complete Drupal 11 module called `hello_world_benchmark`, you'll need to set up several files and directories as specified in your requirements. Here’s how you can structure the module:

### Directory Structure
```
hello_world_benchmark/
├── hello_world_benchmark.info.yml
├── hello_world_benchmark.routing.yml
├── src/
│   └── Controller/
│       └── HelloWorldBenchmarkController.php
├── src/
│   └── Entity/
│       └── HelloBenchmarkMessages.php
├── hello_world_benchmark.services.yml
├── hello_world_benchmark.install
```

### 1. `hello_world_benchmark.info.yml`
This file contains metadata about the module:
```yaml
name: 'Hello World Benchmark'
type: module
description: 'A simple module to demonstrate Drupal 11 module creation.'
core_version_requirement: ^8 || ^9 || ^10 || ^11
package: Custom
dependencies:
  - drupal:node
```

### 2. `hello_world_benchmark.routing.yml`
This file defines the routes for your module:
```yaml
hello_world_benchmark.hello:
  path: '/hello-world-benchmark'
  defaults:
    _controller: '\Drupal\hello_world_benchmark\Controller\HelloWorldBenchmarkController::hello'
  methods: ['GET']
```

### 3. `src/Controller/HelloWorldBenchmarkController.php`
This is the controller that returns a simple message:
```php
<?php

namespace Drupal\hello_world_benchmark\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloWorldBenchmarkController extends ControllerBase {

  public function hello() {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('Hello World from AI Arena benchmark!'),
    ];
  }
}
```

### 4. `src/Entity/HelloBenchmarkMessages.php`
This is a placeholder for an entity, as per your requirement. Since Drupal 10+ does not use entities in the same way as earlier versions, this might need adjustment based on intended functionality:
```php
<?php

namespace Drupal\hello_world_benchmark\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Defines the HelloBenchmarkMessages entity.
 *
 * @ConfigEntityType(
 *   id = "hello_benchmark_messages",
 *   label = @Translation("Hello Benchmark Messages"),
 *   handlers = {
 *     "view_builder" => "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" => "Drupal\Core\Entity\EntityListBuilder",
 *     "form" => {
 *       "add" => "Drupal\hello_world_benchmark\Form\HelloBenchmarkMessagesForm",
 *       "edit" => "Drupal\hello_world_benchmark\Form\HelloBenchmarkMessagesForm",
 *       "delete" => "Drupal\hello_world_benchmark\Form\HelloBenchmarkMessagesDeleteForm",
 *     },
 *   },
 *   config_prefix = "hello_benchmark_messages",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "message",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "canonical" => "/hello-benchmark-messages/{hello_benchmark_messages}",
 *     "edit-form" => "/admin/config/system/hello_benchmark_messages/{hello_benchmark_messages}/edit",
 *     "delete-form" => "/admin/config/system/hello_benchmark_messages/{hello_benchmark_messages}/delete",
 *     "collection" => "/admin/config/system/hello_benchmark_messages"
 *   },
 *   config_export = {
 *     "id",
 *     "message",
 *   }
 * )
 */
class HelloBenchmarkMessages extends ConfigEntityBase {

  /**
   * The ID of the entity.
   *
   * @var string
   */
  protected $id;

  /**
   * The message content.
   *
   * @var string
   */
  protected $message;
}
```

### 5. `hello_world_benchmark.services.yml`
This file defines a simple service, which is not used in the provided example but can be useful for dependency injection:
```yaml
services:
  hello_world_benchmark.example_service:
    class: Drupal\hello_world_benchmark\ExampleService
    arguments: ['@config.factory']
```

### 6. `hello_world_benchmark.install`
This file defines the database schema for a table "hello_benchmark_messages":
```php
<?php

namespace Drupal\hello_world_benchmark\Install;

use Drupal\Core\Config\ConfigInstaller;
use Drupal\Core\Extension\ModuleUninstallValidatorInterface;
use Drupal\Core\State\StateInterface;

class HelloWorldBenchmarkInstall {

  public function schema(SchemaSetup $schema) {
    $schema->createTable('hello_benchmark_messages', [
      'fields' => [
        'id' => ['type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The ID of the message.'],
        'message' => ['type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'description' => 'The content of the message.'],
      ],
      'primary key' => ['id'],
    ]);
  }
}
```

This setup provides a basic structure for your Drupal module. You may need to adjust paths and methods according to Drupal’s conventions and API changes specific to version 11, especially concerning entity definitions and service configurations.
qwen3-coder:30b Run #1 · 1111 tok · 10.3 tok/s
Here's a complete Drupal 11 module called "hello_world_benchmark" with all the requested components:

### 1. hello_world_benchmark.info.yml
```yaml
name: Hello World Benchmark
type: module
description: A simple benchmark module for demonstration purposes
core_version_requirement: ^11
package: Custom
```

### 2. hello_world_benchmark.routing.yml
```yaml
hello_world_benchmark.hello:
  path: '/hello-world-benchmark'
  defaults:
    _controller: '\Drupal\hello_world_benchmark\Controller\HelloWorldController::hello'
    _title: 'Hello World Benchmark'
  requirements:
    _access: 'TRUE'
```

### 3. src/Controller/HelloWorldController.php
```php
<?php

namespace Drupal\hello_world_benchmark\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;

/**
 * Hello world controller.
 */
class HelloWorldController extends ControllerBase {

  /**
   * Returns a simple hello world message.
   */
  public function hello(): Response {
    return new Response('Hello World from AI Arena benchmark!');
  }

}
```

### 4. src/Service/HelloWorldBenchmarkService.php
```php
<?php

namespace Drupal\hello_world_benchmark\Service;

/**
 * Simple benchmark service.
 */
class HelloWorldBenchmarkService {

  /**
   * The service name.
   */
  public readonly string $name;

  /**
   * Constructs a new HelloWorldBenchmarkService object.
   */
  public function __construct() {
    $this->name = 'hello_world_benchmark_service';
  }

  /**
   * Returns the service name.
   */
  public function getName(): string {
    return $this->name;
  }

}
```

### 5. hello_world_benchmark.services.yml
```yaml
services:
  hello_world_benchmark.service:
    class: Drupal\hello_world_benchmark\Service\HelloWorldBenchmarkService
    public: true
```

### 6. hello_world_benchmark.install
```php
<?php

/**
 * @file
 * Install, update and uninstall functions for the hello_world_benchmark module.
 */

use Drupal\Core\Database\Database;

/**
 * Implements hook_schema().
 */
function hello_world_benchmark_schema(array &$schema) {
  $schema['hello_benchmark_messages'] = [
    'description' => 'Stores benchmark messages.',
    'fields' => [
      'id' => [
        'description' => 'The primary identifier for a message.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'message' => [
        'description' => 'The benchmark message.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'created' => [
        'description' => 'The timestamp when the message was created.',
        'type' => 'timestamp',
        'not null' => TRUE,
        'default' => 0,
      ],
      'status' => [
        'description' => 'The status of the message.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ],
    ],
    'primary key' => ['id'],
    'indexes' => [
      'created' => ['created'],
      'status' => ['status'],
    ],
  ];
}
```

### 7. hello_world_benchmark.module
```php
<?php

/**
 * @file
 * Main module file for hello_world_benchmark.
 */

/**
 * Implements hook_help().
 */
function hello_world_benchmark_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'hello_world_benchmark.hello':
      return '<p>' . t('This is a simple benchmark module that displays a hello world message.') . '</p>';
  }
}
```

### Directory Structure
```
modules/custom/hello_world_benchmark/
├── hello_world_benchmark.info.yml
├── hello_world_benchmark.routing.yml
├── hello_world_benchmark.services.yml
├── hello_world_benchmark.install
├── hello_world_benchmark.module
└── src/
    ├── Controller/
    │   └── HelloWorldController.php
    └── Service/
        └── HelloWorldBenchmarkService.php
```

### Key Features:

1. **Proper Drupal 11 Namespacing**: Uses `Drupal\hello_world_benchmark` namespace
2. **PHP 8.3 Syntax**: Uses readonly properties in service class
3. **Routing**: Defines `/hello-world-benchmark` route
4. **Controller**: Returns "Hello World from AI Arena benchmark!" message
5. **Service**: Includes a simple service with readonly property
6. **Database Schema**: Creates `hello_benchmark_messages` table with proper fields and indexes
7. **Module Structure**: Follows Drupal 11 module structure conventions

To install:
1. Place the module in `modules/custom/hello_world_benchmark`
2. Enable the module via `drush en hello_world_benchmark` or admin UI
3. Visit `/hello-world-benchmark` to see the message
4. The database table will be created automatically during installation
deepseek-coder-v2:16b Run #1 · 921 tok · 17.7 tok/s
 Below is a complete example of a Drupal 11 module named `hello_world_benchmark`. This module includes all the required files as specified in your request, adhering to Drupal's naming conventions and best practices for module development.

### Directory Structure
The module will be structured within a directory under `modules/custom/hello_world_benchmark/`.

### 1. info.yml File
This file provides metadata about the module:
```yaml
name: 'hello_world_benchmark'
type: module
description: 'A simple hello world benchmark module.'
core_version_requirement: ^8 || ^9 || ^10 || ^11
package: Custom
dependencies:
  - drupal:node
```
Place this file at `modules/custom/hello_world_benchmark/hello_world_benchmark.info.yml`.

### 2. routing.yml File
This defines the route for the module's hello page:
```yaml
hello_world_benchmark.hello:
  path: '/hello-world-benchmark'
  defaults:
    _controller: '\Drupal\hello_world_benchmark\Controller\HelloWorldBenchmarkController::hello'
  requirements:
    _permission: 'access content'
```
Place this file at `modules/custom/hello_world_benchmark/hello_world_benchmark.routing.yml`.

### 3. Controller File
This is where the logic for handling requests to `/hello-world-benchmark` will be defined:
```php
<?php

namespace Drupal\hello_world_benchmark\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloWorldBenchmarkController extends ControllerBase {

  public function hello() {
    return [
      '#type' => 'markup',
      '#markup' => $this->t('Hello World from AI Arena benchmark!'),
    ];
  }
}
```
Place this file at `modules/custom/hello_world_benchmark/src/Controller/HelloWorldBenchmarkController.php`.

### 4. services.yml File
This defines a simple service:
```yaml
services:
  hello_world_benchmark.example_service:
    class: Drupal\hello_world_benchmark\ExampleService
    arguments: []
```
Place this file at `modules/custom/hello_world_benchmark/hello_world_benchmark.services.yml`.

### 5. ExampleService File
Implement the service logic if needed:
```php
<?php

namespace Drupal\hello_world_benchmark;

class ExampleService {
  public function greet() {
    return "Greetings from AI Arena benchmark!";
  }
}
```
Place this file at `modules/custom/hello_world_benchmark/src/ExampleService.php`.

### 6. install/install.info.yml File
This defines the schema for a new table:
```yaml
schema: hello_benchmark_messages
```
Place this file at `modules/custom/hello_world_benchmark/install/hello_world_benchmark.install.info.yml`.

### 7. install/schema/hello_benchmark_messages.schema.yml File
This defines the structure of the table:
```yaml
fields:
  id:
    type: integer
    unsigned: true
    not null: true
    description: 'The primary key for the message.'
    default: 0
  message:
    type: text
    not null: true
    description: 'The content of the message.'
```
Place this file at `modules/custom/hello_world_benchmark/install/schema/hello_benchmark_messages.schema.yml`.

### Note
- Ensure that the module path is correct in all files (`modules/custom/hello_world_benchmark/...`).
- The example service and its methods are placeholders. You might need to adjust them according to your specific requirements or remove if not needed.
- Make sure Drupal's Composer setup allows for custom modules under `web/modules/custom`.

This setup provides a basic structure for your module, which you can expand upon as required for more complex functionality.
gemma4:e4b Run #1 · 1787 tok · 19.9 tok/s
This is a comprehensive setup for a Drupal 11 module, adhering to modern standards, PHP 8.3 syntax, and Drupal best practices.

### 📂 Module Structure

Your module directory structure should look like this:

```
hello_world_benchmark/
├── hello_world_benchmark.info.yml
├── hello_world_benchmark.services.yml
├── hello_world_benchmark.routing.yml
├── src/
│   ├── Controller/
│   │   └── BenchmarkController.php
│   └── Service/
│       └── BenchmarkLogger.php
└── hello_world_benchmark.install
```

---

### 📄 1. `hello_world_benchmark.info.yml`

This file defines the module metadata.

```yaml
# hello_world_benchmark/hello_world_benchmark.info.yml
name: 'Hello World Benchmark'
type: module
description: 'Provides a basic benchmark module demonstrating modern Drupal development practices.'
core_version_requirement: ^11
package: Custom
```

### 📄 2. `hello_world_benchmark.services.yml`

This file registers the custom service.

```yaml
# hello_world_benchmark/hello_world_benchmark.services.yml
services:
  hello_world_benchmark.logger:
    class: Drupal\hello_world_benchmark\Service\BenchmarkLogger
    arguments: ['@database']
    tags:
      - { name: logger }
```

### 📄 3. `hello_world_benchmark.routing.yml`

This file defines the route for the "Hello World" page.

```yaml
# hello_world_benchmark/hello_world_benchmark.routing.yml
hello_world_benchmark.page:
  path: '/hello-world-benchmark'
  defaults:
    _controller: '\Drupal\hello_world_benchmark\Controller\BenchmarkController::helloPage'
    _title: 'AI Arena Benchmark'
  requirements:
    _permission: 'access content'
```

### 📄 4. The Controller (`src/Controller/BenchmarkController.php`)

This controller handles the request and returns the specified message.

```php
<?php

// hello_world_benchmark/src/Controller/BenchmarkController.php

namespace Drupal\hello_world_benchmark\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;

/**
 * Controller for the Hello World Benchmark module.
 */
class BenchmarkController extends ControllerBase {

  /**
   * Returns a basic rendered page message.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   A renderable response containing the benchmark message.
   */
  public function helloPage(): Response {
    $build = [
      '#markup' => $this->t('<h1>Hello World from AI Arena benchmark!</h1><p>This module demonstrates Drupal 11, PHP 8.3, and modern module development standards.</p>'),
    ];

    // Returning a Response object is often best practice for controllers.
    return $this->renderer()->render($build);
  }

}
```

### 📄 5. The Service (`src/Service/BenchmarkLogger.php`)

This simple service demonstrates dependency injection and usage of readonly properties.

```php
<?php

// hello_world_benchmark/src/Service/BenchmarkLogger.php

namespace Drupal\hello_world_benchmark\Service;

use Drupal\Core\Database\Connection;

/**
 * Service for logging benchmark messages.
 */
class BenchmarkLogger {

  /**
   * The database connection service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  private readonly Connection $database;

  /**
   * Constructs a BenchmarkLogger object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * Logs a message to the dedicated benchmark table.
   *
   * @param string $message
   *   The message content to log.
   */
  public function logMessage(string $message): void {
    $this->database->insert('hello_benchmark_messages')
      ->fields(['message' => $message, 'logged_at' => \Drupal::time()->getRequestTime()])
      ->execute();
  }

}
```

### 📄 6. Install File (`hello_world_benchmark.install`)

This file handles the database schema creation upon module installation.

```php
<?php

/**
 * @file
 * Implements hook_install for the Hello World Benchmark module.
 *
 * Creates the necessary database table.
 */

/**
 * Implements hook_install().
 */
function hello_world_benchmark_install() {
  $connection = \Drupal::database();
  $schema = $connection->schema();

  // Check if the table already exists to prevent errors.
  if (!$schema->tableExists('hello_benchmark_messages')) {
    $schema->createTable('hello_benchmark_messages', [
      'fields' => [
        'id' => [
          'type' => 'serial',
          'not null' => TRUE,
          'description' => 'The unique ID of the message.',
        ],
        'message' => [
          'type' => 'text',
          'size' => 'medium',
          'not null' => TRUE,
          'description' => 'The content of the benchmark message.',
        ],
        'logged_at' => [
          'type' => 'int',
          'size' => 'big',
          'not null' => TRUE,
          'description' => 'Timestamp of when the message was logged.',
        ],
      ],
      'primary key' => ['id'],
      'indexes' => [
        'logged_at' => ['logged_at'],
      ],
    ]);
  }
}
```

### ✅ How to Use It

1.  Place the `hello_world_benchmark` directory in your Drupal site's modules folder (`/modules/custom/`).
2.  Enable the module via the Drupal UI (or Drush: `drush en hello_world_benchmark`).
3.  The database table `hello_benchmark_messages` will be created.
4.  Navigate to **`/hello-world-benchmark`** in your browser to see the rendered "Hello World" page.
X

Nezmeškejte novinky!

Přihlaste se k odběru novinek a aktualit.