-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEntityForm.php
More file actions
72 lines (65 loc) · 2.04 KB
/
EntityForm.php
File metadata and controls
72 lines (65 loc) · 2.04 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
<?php
namespace DrupalCodeBuilder\Generator;
use CaseConverter\CaseString;
/**
* 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 componentDataDefinition() {
$data_definition = parent::componentDataDefinition() + [
// The entity link template that the form save() method redirects to.
'redirect_link_template' => [
'internal' => TRUE,
],
];
return $data_definition;
}
/**
* {@inheritdoc}
*/
public function requiredComponents() {
$components = array(
// Request the form functions.
// Note that for entity forms, buildForm() shouldn't be used, but form()
// instead. (DrupalWTF!)
'form' => array(
'component_type' => 'FormBuilder',
'containing_component' => '%requester',
'docblock_inherit' => TRUE,
'function_name' => 'form',
'body' => [
'$form = parent::form($form, $form_state);',
'return $form;',
],
),
'submitForm' => array(
'component_type' => 'PHPFunction',
'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',
'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;
}
}