Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\action;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityForm;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
7 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
8 use Drupal\Core\Plugin\PluginFormInterface;
|
Chris@0
|
9 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Provides a base form for action forms.
|
Chris@0
|
13 */
|
Chris@0
|
14 abstract class ActionFormBase extends EntityForm {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The action storage.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $storage;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@14
|
24 * The action entity.
|
Chris@14
|
25 *
|
Chris@14
|
26 * @var \Drupal\system\ActionConfigEntityInterface
|
Chris@14
|
27 */
|
Chris@14
|
28 protected $entity;
|
Chris@14
|
29
|
Chris@14
|
30 /**
|
Chris@0
|
31 * Constructs a new action form.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
|
Chris@0
|
34 * The action storage.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function __construct(EntityStorageInterface $storage) {
|
Chris@0
|
37 $this->storage = $storage;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 public static function create(ContainerInterface $container) {
|
Chris@0
|
44 return new static(
|
Chris@0
|
45 $container->get('entity.manager')->getStorage('action')
|
Chris@0
|
46 );
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * {@inheritdoc}
|
Chris@0
|
51 */
|
Chris@0
|
52 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
53 $form['label'] = [
|
Chris@0
|
54 '#type' => 'textfield',
|
Chris@0
|
55 '#title' => $this->t('Label'),
|
Chris@0
|
56 '#default_value' => $this->entity->label(),
|
Chris@0
|
57 '#maxlength' => '255',
|
Chris@0
|
58 '#description' => $this->t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
|
Chris@0
|
59 ];
|
Chris@0
|
60
|
Chris@0
|
61 $form['id'] = [
|
Chris@0
|
62 '#type' => 'machine_name',
|
Chris@0
|
63 '#default_value' => $this->entity->id(),
|
Chris@0
|
64 '#disabled' => !$this->entity->isNew(),
|
Chris@0
|
65 '#maxlength' => 64,
|
Chris@0
|
66 '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
|
Chris@0
|
67 '#machine_name' => [
|
Chris@0
|
68 'exists' => [$this, 'exists'],
|
Chris@0
|
69 ],
|
Chris@0
|
70 ];
|
Chris@0
|
71 $form['plugin'] = [
|
Chris@0
|
72 '#type' => 'value',
|
Chris@0
|
73 '#value' => $this->entity->get('plugin'),
|
Chris@0
|
74 ];
|
Chris@0
|
75 $form['type'] = [
|
Chris@0
|
76 '#type' => 'value',
|
Chris@0
|
77 '#value' => $this->entity->getType(),
|
Chris@0
|
78 ];
|
Chris@0
|
79
|
Chris@14
|
80 if ($plugin = $this->getPlugin()) {
|
Chris@14
|
81 $form += $plugin->buildConfigurationForm($form, $form_state);
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 return parent::form($form, $form_state);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Determines if the action already exists.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @param string $id
|
Chris@14
|
91 * The action ID.
|
Chris@0
|
92 *
|
Chris@0
|
93 * @return bool
|
Chris@0
|
94 * TRUE if the action exists, FALSE otherwise.
|
Chris@0
|
95 */
|
Chris@0
|
96 public function exists($id) {
|
Chris@0
|
97 $action = $this->storage->load($id);
|
Chris@0
|
98 return !empty($action);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * {@inheritdoc}
|
Chris@0
|
103 */
|
Chris@0
|
104 protected function actions(array $form, FormStateInterface $form_state) {
|
Chris@0
|
105 $actions = parent::actions($form, $form_state);
|
Chris@0
|
106 unset($actions['delete']);
|
Chris@0
|
107 return $actions;
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * {@inheritdoc}
|
Chris@0
|
112 */
|
Chris@0
|
113 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
114 parent::validateForm($form, $form_state);
|
Chris@14
|
115 if ($plugin = $this->getPlugin()) {
|
Chris@14
|
116 $plugin->validateConfigurationForm($form, $form_state);
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * {@inheritdoc}
|
Chris@0
|
122 */
|
Chris@0
|
123 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
124 parent::submitForm($form, $form_state);
|
Chris@14
|
125 if ($plugin = $this->getPlugin()) {
|
Chris@14
|
126 $plugin->submitConfigurationForm($form, $form_state);
|
Chris@0
|
127 }
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 /**
|
Chris@0
|
131 * {@inheritdoc}
|
Chris@0
|
132 */
|
Chris@0
|
133 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
134 $this->entity->save();
|
Chris@17
|
135 $this->messenger()->addStatus($this->t('The action has been successfully saved.'));
|
Chris@0
|
136
|
Chris@0
|
137 $form_state->setRedirect('entity.action.collection');
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@14
|
140 /**
|
Chris@14
|
141 * Gets the action plugin while ensuring it implements configuration form.
|
Chris@14
|
142 *
|
Chris@14
|
143 * @return \Drupal\Core\Action\ActionInterface|\Drupal\Core\Plugin\PluginFormInterface|null
|
Chris@14
|
144 * The action plugin, or NULL if it does not implement configuration forms.
|
Chris@14
|
145 */
|
Chris@14
|
146 protected function getPlugin() {
|
Chris@14
|
147 if ($this->entity->getPlugin() instanceof PluginFormInterface) {
|
Chris@14
|
148 return $this->entity->getPlugin();
|
Chris@14
|
149 }
|
Chris@14
|
150 return NULL;
|
Chris@14
|
151 }
|
Chris@14
|
152
|
Chris@0
|
153 }
|