-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEntityForm.php
More file actions
87 lines (78 loc) · 2.63 KB
/
EntityForm.php
File metadata and controls
87 lines (78 loc) · 2.63 KB
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
<?php
namespace DrupalCodeBuilder\Generator;
use CaseConverter\CaseString;
use DrupalCodeBuilder\Definition\PropertyDefinition;
/**
* Generator class for entity form handlers.
*
* Extend from EntityHandler rather than Form, as core's base EntityForm class
* does a lot of the form work.
*/
class EntityForm extends EntityHandler {
/**
* {@inheritdoc}
*/
public static function getPropertyDefinition(): PropertyDefinition {
$definition = parent::getPropertyDefinition();
$definition->addProperties([
// The entity link template that the form save() method redirects to.
'redirect_link_template' => PropertyDefinition::create('string')
->setInternal(TRUE),
]);
return $definition;
}
/**
* {@inheritdoc}
*/
public function requiredComponents(): array {
$components = [
// Request the form functions.
// Note that for entity forms, buildForm() shouldn't be used, but form()
// instead. (DrupalWTF!)
'form' => [
'component_type' => 'FormBuilder',
'containing_component' => '%requester',
'docblock_inherit' => TRUE,
'function_name' => 'form',
'body' => [
'$form = parent::form($form, $form_state);',
'return $form;',
],
],
'validateForm' => [
'component_type' => 'PHPFunction',
'function_name' => 'validateForm',
'containing_component' => '%requester',
'docblock_inherit' => TRUE,
'declaration' => 'public function validateForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state)',
'body' => [
'parent::validateForm($form, $form_state);'
],
],
'submitForm' => [
'component_type' => 'PHPFunction',
'function_name' => 'submitForm',
'containing_component' => '%requester',
'docblock_inherit' => TRUE,
'declaration' => 'public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state)',
'body' => [
'parent::submitForm($form, $form_state);'
],
],
'save' => [
'component_type' => 'PHPFunction',
'function_name' => 'save',
'containing_component' => '%requester',
'docblock_inherit' => TRUE,
'declaration' => 'public function save(array $form, \Drupal\Core\Form\FormStateInterface $form_state)',
'body' => [
'$saved = parent::save($form, $form_state);',
"£form_state->setRedirectUrl(£this->entity->toUrl('{$this->component_data['redirect_link_template']}'));",
'',
'return $saved;',
],
],
];
return $components;
}
}