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 plugin being configured.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var \Drupal\Core\Action\ActionInterface
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $plugin;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The action storage.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $storage;
|
Chris@0
|
29
|
Chris@0
|
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 buildForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
53 $this->plugin = $this->entity->getPlugin();
|
Chris@0
|
54 return parent::buildForm($form, $form_state);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
61 $form['label'] = [
|
Chris@0
|
62 '#type' => 'textfield',
|
Chris@0
|
63 '#title' => $this->t('Label'),
|
Chris@0
|
64 '#default_value' => $this->entity->label(),
|
Chris@0
|
65 '#maxlength' => '255',
|
Chris@0
|
66 '#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
|
67 ];
|
Chris@0
|
68
|
Chris@0
|
69 $form['id'] = [
|
Chris@0
|
70 '#type' => 'machine_name',
|
Chris@0
|
71 '#default_value' => $this->entity->id(),
|
Chris@0
|
72 '#disabled' => !$this->entity->isNew(),
|
Chris@0
|
73 '#maxlength' => 64,
|
Chris@0
|
74 '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
|
Chris@0
|
75 '#machine_name' => [
|
Chris@0
|
76 'exists' => [$this, 'exists'],
|
Chris@0
|
77 ],
|
Chris@0
|
78 ];
|
Chris@0
|
79 $form['plugin'] = [
|
Chris@0
|
80 '#type' => 'value',
|
Chris@0
|
81 '#value' => $this->entity->get('plugin'),
|
Chris@0
|
82 ];
|
Chris@0
|
83 $form['type'] = [
|
Chris@0
|
84 '#type' => 'value',
|
Chris@0
|
85 '#value' => $this->entity->getType(),
|
Chris@0
|
86 ];
|
Chris@0
|
87
|
Chris@0
|
88 if ($this->plugin instanceof PluginFormInterface) {
|
Chris@0
|
89 $form += $this->plugin->buildConfigurationForm($form, $form_state);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 return parent::form($form, $form_state);
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Determines if the action already exists.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @param string $id
|
Chris@0
|
99 * The action ID
|
Chris@0
|
100 *
|
Chris@0
|
101 * @return bool
|
Chris@0
|
102 * TRUE if the action exists, FALSE otherwise.
|
Chris@0
|
103 */
|
Chris@0
|
104 public function exists($id) {
|
Chris@0
|
105 $action = $this->storage->load($id);
|
Chris@0
|
106 return !empty($action);
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * {@inheritdoc}
|
Chris@0
|
111 */
|
Chris@0
|
112 protected function actions(array $form, FormStateInterface $form_state) {
|
Chris@0
|
113 $actions = parent::actions($form, $form_state);
|
Chris@0
|
114 unset($actions['delete']);
|
Chris@0
|
115 return $actions;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
122 parent::validateForm($form, $form_state);
|
Chris@0
|
123
|
Chris@0
|
124 if ($this->plugin instanceof PluginFormInterface) {
|
Chris@0
|
125 $this->plugin->validateConfigurationForm($form, $form_state);
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 /**
|
Chris@0
|
130 * {@inheritdoc}
|
Chris@0
|
131 */
|
Chris@0
|
132 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
133 parent::submitForm($form, $form_state);
|
Chris@0
|
134
|
Chris@0
|
135 if ($this->plugin instanceof PluginFormInterface) {
|
Chris@0
|
136 $this->plugin->submitConfigurationForm($form, $form_state);
|
Chris@0
|
137 }
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * {@inheritdoc}
|
Chris@0
|
142 */
|
Chris@0
|
143 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
144 $this->entity->save();
|
Chris@0
|
145 drupal_set_message($this->t('The action has been successfully saved.'));
|
Chris@0
|
146
|
Chris@0
|
147 $form_state->setRedirect('entity.action.collection');
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 }
|