comparison core/modules/action/src/ActionFormBase.php @ 0:4c8ae668cc8c

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