Drupal module demo - module_hero_v3
文件:
module_hero.info.yml
module_hero.module
module_hero.routing.yml
src/Controller/HeroController.php
templates/hero-list.html.twig
module_hero.info.yml:
```
name: "Module Hero"
description: "We are going to be Drupal 8 module heros at the end"
type: module
core: 8.x
```
module_hero.module
```
<?php
function module_hero_theme($existing, $type, $theme, $path)
{
return
[
'hero_list' =>
[
'variables' => ['items' => [], 'title' => ''],
]
];
}
```
module_hero.routing.yml:
```
module_hero.herolist:
path: '/herolist'
defaults:
_controller: '\Drupal\module_hero\Controller\HeroController::heroList'
_title: 'Our super heroes list'
requirements:
_permission: 'access content'
```
src/Controller/HeroController.php:
```
<?php
namespace Drupal\module_hero\Controller;
use Drupal\Core\Controller\ControllerBase;
class HeroController extends ControllerBase
{
public function heroList()
{
$heroes =
[
['name' => 'Neo1'],
['name' => 'Neo2'],
['name' => 'Neo3'],
['name' => 'Neo4'],
['name' => 'Neo5'],
['name' => 'Neo6'],
['name' => 'Neo7'],
['name' => 'Neo8']
];
return
[
'#theme' => 'hero_list',
'#items' => $heroes,
'#title' => $this->t('Our wonderful heroes list'),
];
}
}
```
templates/hero-list.html.twig
```
<h4>{{ title }}</h4>
<ol>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ol>
<strong>This is coming from our custom twig file.</strong>
```
.