Drupal website module demo1
文件:
hello\_world.info.yml
hello\_world.links.menu.yml
hello\_world.routing.yml
src/Controller/HelloController.php
src/Plugin/Block/HelloBlock.php
hello\_world.info.yml:
```
name: drupal website demo1
description: Creates a page showing "Hello World".
package: Custom
type: module
core: 8.x
```
hello\_world.links.menu.yml
```
hello_world.admin:
title: 'Hello module settings'
description: 'example of how to make an admin settings page link'
parent: system.admin_config_development
route_name: hello_world.content
weight: 100
```
hello\_world.routing.yml
```
hello_world.content:
path: '/hello'
defaults:
_controller: '\Drupal\hello_world\Controller\HelloController::content'
_title: 'Hello World'
requirements:
_permission: 'access content'
```
src/Controller/HelloController.php
```
<?php
namespace Drupal\hello_world\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Defines HelloController class.
*/
class HelloController extends ControllerBase {
/**
* Display the markup.
*
* @return array
* Return markup array.
*/
public function content() {
return [
'#type' => 'markup',
'#markup' => $this->t('Hello, World!'),
];
}
}
```
src/Plugin/Block/HelloBlock.php
```
<?php
namespace Drupal\hello_world\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'Hello' Block.
*
* @Block(
* id = "hello_block",
* admin_label = @Translation("Hello block"),
* category = @Translation("Hello World"),
* )
*/
class HelloBlock extends BlockBase implements BlockPluginInterface {
/**
* {@inheritdoc}
*/
public function build() {
return array(
'#markup' => $this->t('Hello, World!'),
);
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
$form['hello_block_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Who'),
'#description' => $this->t('Who do you want to say hello to?'),
'#default_value' => isset($config['hello_block_name']) ? $config['hello_block_name'] : '',
];
$form['actions']['custom_submit'] = [
'#type' => 'submit',
'#name' => 'custom_submit',
'#value' => $this->t('Custom Submit'),
'#submit' => array(array($this, 'custom_submit_form')),
];
return $form;
}
}
```
.