-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathRulesComponentFormBase.php
More file actions
113 lines (98 loc) · 3.1 KB
/
RulesComponentFormBase.php
File metadata and controls
113 lines (98 loc) · 3.1 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @file
* Contains \Drupal\rules\Form\RulesComponentFormBase.
*/
namespace Drupal\rules\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides the base form for rules add and edit forms.
*/
abstract class RulesComponentFormBase extends EntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
// Specify the wrapper div used by #ajax.
$form['#prefix'] = '<div id="rules-form-wrapper">';
$form['#suffix'] = '</div>';
$form['settings'] = [
'#type' => 'details',
'#title' => $this->t('Settings'),
'#open' => $this->entity->isNew(),
];
$form['settings']['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#default_value' => $this->entity->label(),
'#required' => TRUE,
];
$form['settings']['id'] = [
'#type' => 'machine_name',
'#description' => $this->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#disabled' => !$this->entity->isNew(),
'#default_value' => $this->entity->id(),
'#machine_name' => [
'exists' => [$this, 'exists'],
'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
'source' => ['settings', 'label'],
'error' => $this->t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'),
],
];
// @todo enter a real tag field here.
$form['settings']['tag'] = [
'#type' => 'textfield',
'#title' => $this->t('Tag'),
'#default_value' => $this->entity->getTag(),
'#description' => $this->t('Enter a tag here'),
'#required' => FALSE,
];
$form['settings']['description'] = [
'#type' => 'textarea',
'#default_value' => $this->entity->getDescription(),
'#description' => $this->t('Enter a description for this component.'),
'#title' => $this->t('Description'),
];
return parent::form($form, $form_state);
}
/**
* Machine name exists callback.
*
* @param string $id
* The machine name ID.
*
* @return bool
* TRUE if an entity with the same name already exists, FALSE otherwise.
*/
public function exists($id) {
$type = $this->entity->getEntityTypeId();
return (bool) $this->entityTypeManager->getStorage($type)->load($id);
}
/**
* Get default form #ajax properties.
*
* @param string $effect
* (optional) The jQuery effect to use when placing the new HTML (used with
* 'wrapper'). Valid options are 'none' (default), 'slide', or 'fade'.
*
* @return array
*/
public function getDefaultAjax($effect = 'none') {
return array(
'callback' => '::reloadForm',
'wrapper' => 'rules-form-wrapper',
'effect' => $effect,
'speed' => 'fast',
);
}
/**
* Ajax callback to reload the form.
*
* @return array
* The reloaded form.
*/
public function reloadForm(array $form, FormStateInterface $form_state) {
return $form;
}
}