drupal8入门学习笔记

前言

Java转Drupal开发,有很多不习惯,非全面向对象开发。该博客首写于2017, 博客迁移, 仅做记录。

Drupal8渲染twig示例

在modules/custom建立test_twig文件夹(模块根目录)

模块结构:

-css
-----test.css
-js
-----test.js
-src
-----Controller
--------TestTwigController.php
-templates
-----my-template.html.twig
-test_twig.info.yml
-test_twig.module
-test_twig.routing.yml


test_twig.info.yml

1
2
3
4
5
name: "Test twig"
type: module
description: "自定义模块自定义模板"
package: "Example modules"
core: 8.x

test_twig.module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php


/**
* @param $existing
* @param $type
* @param $theme
* @param $path
*
* @return array
*
* my_element 模板名
* test_var 参数名
*/
function test_twig_theme($existing, $type, $theme, $path) {
return [
'my_template' => [
'variables' => ['test_var' => NULL,'test_var1' => NULL],
],
];

return $theme;
}

test_twig.routing.yml

1
2
3
4
5
6
test_twig_test:
path: 'test_twig/test'
defaults:
_controller: '\Drupal\test_twig\Controller\TestTwigController::content'
requirements:
_access: 'TRUE'

test.css

1
2
3
.acme-hello-text {
background-color: #dedede;
}

TestTwigController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

namespace Drupal\test_twig\Controller;

use Drupal\Core\Controller\ControllerBase;

class TestTwigController extends ControllerBase {

public function content() {

return [
'#theme' => 'my_template',
'#test_var' => $this->t('Test Value7'),
"#test_var1" => $this->t("Test Value1")
];

}
}

my-template.html.twig

1
2
3
4
<p>Test twig template!</p>

<p>test_var: {{ test_var }}</p>
<p>test_var1: {{ test_var1 }}</p>t

Twig渲染表单

结构目录同1,创建test_form模块

test_form.info.yml

1
2
3
4
5
name: 'test_form'
type: module
description: '表单twig'
package: "Example modules"
core: 8.x

test_form.routing.yml

1
2
3
4
5
6
7
test_form.test:
path: 'test_form/test'
defaults:
_form: '\Drupal\test_form\Form\TestForm'
_title: 'Twig form'
requirements:
_permission: 'access content'

test_form.module

1
2
3
4
5
6
7
8
9
<?php

function test_form_theme() {
return [
'test_form' => [
'render element' => 'form',
],
];
}

test_form.libraries.yml

1
2
3
4
5
6
7
8
9
10
11
12
sample_library:
css:
# For some reason, you need to put css under 'theme'.
theme:
css/example.css: {}
dependencies:
# jQuery is not included by default, so we add it as a dependency
- core/jquery
# We are also going to use jQuery.once so that code doesn't trigger multiple times.
- core/jquery.once
# drupal and drupalSettings are not included by default either.
- core/drupal

在src/Form下创建TestForm.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

namespace Drupal\test_form\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class TestForm extends FormBase{

/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return "test_form";
}

/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attributes'] = array('class' => 'editForm editForm-thread');

$form['postfeed_reply_Message'] = array(
'#type' => 'textarea',
'#title' => t(''),
'#required' => TRUE,
'#attributes' => array('placeholder' => $this->t('postfeed_reply_Message') )
);

$form['first_name'] = array(
'#type' => 'textfield',
'#title' => $this->t('First Name'),
'#default_value' => $this->t('default_value'),
'#required' => TRUE,

);

$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#default_value' => $this->t('刘德华'),
'#required' => TRUE,
);

$form['metier'] = array(
'#type' => 'search',
'#title' => $this->t('metier-title'),
'#default_value' => '',
'#placeholder'=>'metier提示',
'#attributes' => array('title' => $this->t('att-metierr')),
);

$form['position'] = array(
'#type' => 'search',
'#title' => $this->t('position-title'),
'#placeholder'=>'position提示',
'#attributes' => array('title' => $this->t('att-position')),
);

$form['submit_btn'] = array(
'#type' => 'submit',
'#value' => $this->t('Submit'),
);

// 添加样式
$form['#attached'] = [
'library' => [
'test_form/sample_library',
],
];

$form['#theme'] = 'test_form';

return $form;
}

/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// TODO: Implement submitForm() method.
}
}

templates/test-form.html.twig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div class="row">
<h1>测试Twig</h1>
{{ form.postfeed_reply_Message }}
<form method="post">
<div class="form-group">
<label for="name">First Name</label>
<div class="input-group">
{{form.first_name}}
{{ form.last_name }}
</div>
</div>
<div class="form-group test">
{{ form.submit_btn }} {# btn btn-default #}
</div>
<div class="col-md-12 myElement">{{ form.metier }}</div>
<div class="col-md-12">{{ form.position }}</div>
</form>
</div>