3 namespace Drupal\workflows\Form;
5 use Drupal\Core\Form\SubformState;
6 use Drupal\Core\Plugin\PluginFormFactoryInterface;
7 use Drupal\workflows\Entity\Workflow;
8 use Drupal\workflows\State;
9 use Drupal\Core\Entity\EntityForm;
10 use Drupal\Core\Entity\EntityInterface;
11 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\workflows\WorkflowTypeInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * The form for editing workflows.
19 class WorkflowEditForm extends EntityForm {
22 * The plugin form factory.
24 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
26 protected $pluginFormFactory;
29 * Creates an instance of WorkflowStateEditForm.
31 * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $pluginFormFactory
32 * The plugin form factory.
34 public function __construct(PluginFormFactoryInterface $pluginFormFactory) {
35 $this->pluginFormFactory = $pluginFormFactory;
41 public static function create(ContainerInterface $container) {
43 $container->get('plugin_form.factory')
50 public function form(array $form, FormStateInterface $form_state) {
51 $form = parent::form($form, $form_state);
53 /* @var \Drupal\workflows\WorkflowInterface $workflow */
54 $workflow = $this->entity;
55 $workflow_type = $workflow->getTypePlugin();
56 $form['#title'] = $this->t('Edit %label workflow', ['%label' => $workflow->label()]);
59 '#type' => 'textfield',
60 '#title' => $this->t('Label'),
62 '#default_value' => $workflow->label(),
67 '#type' => 'machine_name',
68 '#default_value' => $workflow->id(),
70 'exists' => [Workflow::class, 'load'],
76 'state' => $this->t('State'),
77 'weight' => $this->t('Weight'),
78 'operations' => $this->t('Operations')
80 $form['states_container'] = [
82 '#title' => $this->t('States'),
84 '#collapsible' => 'FALSE',
86 $form['states_container']['states'] = [
89 '#title' => $this->t('States'),
90 '#empty' => $this->t('There are no states yet.'),
94 'relationship' => 'sibling',
95 'group' => 'state-weight',
100 $states = $workflow->getTypePlugin()->getStates();
102 // Warn the user if there are no states.
103 if (empty($states)) {
106 'This workflow has no states and will be disabled until there is at least one, <a href=":add-state">add a new state.</a>',
107 [':add-state' => $workflow->toUrl('add-state-form')->toString()]
113 foreach ($states as $state) {
116 'title' => $this->t('Edit'),
117 'url' => Url::fromRoute('entity.workflow.edit_state_form', ['workflow' => $workflow->id(), 'workflow_state' => $state->id()]),
120 if ($this->entity->access('delete-state:' . $state->id())) {
122 'title' => t('Delete'),
123 'url' => Url::fromRoute('entity.workflow.delete_state_form', [
124 'workflow' => $workflow->id(),
125 'workflow_state' => $state->id()
129 $form['states_container']['states'][$state->id()] = [
130 '#attributes' => ['class' => ['draggable']],
131 'state' => ['#markup' => $state->label()],
132 '#weight' => $state->weight(),
135 '#title' => t('Weight for @title', ['@title' => $state->label()]),
136 '#title_display' => 'invisible',
137 '#default_value' => $state->weight(),
138 '#attributes' => ['class' => ['state-weight']],
141 '#type' => 'operations',
146 $form['states_container']['state_add'] = [
147 '#markup' => $workflow->toLink($this->t('Add a new state'), 'add-state-form')->toString(),
151 'label' => $this->t('Label'),
152 'weight' => $this->t('Weight'),
153 'from' => $this->t('From'),
154 'to' => $this->t('To'),
155 'operations' => $this->t('Operations')
157 $form['transitions_container'] = [
158 '#type' => 'details',
159 '#title' => $this->t('Transitions'),
162 $form['transitions_container']['transitions'] = [
164 '#header' => $header,
165 '#title' => $this->t('Transitions'),
166 '#empty' => $this->t('There are no transitions yet.'),
170 'relationship' => 'sibling',
171 'group' => 'transition-weight',
175 foreach ($workflow->getTypePlugin()->getTransitions() as $transition) {
177 'title' => $this->t('Edit'),
178 'url' => Url::fromRoute('entity.workflow.edit_transition_form', ['workflow' => $workflow->id(), 'workflow_transition' => $transition->id()]),
181 'title' => t('Delete'),
182 'url' => Url::fromRoute('entity.workflow.delete_transition_form', ['workflow' => $workflow->id(), 'workflow_transition' => $transition->id()]),
184 $form['transitions_container']['transitions'][$transition->id()] = [
185 '#attributes' => ['class' => ['draggable']],
186 'label' => ['#markup' => $transition->label()],
187 '#weight' => $transition->weight(),
190 '#title' => t('Weight for @title', ['@title' => $transition->label()]),
191 '#title_display' => 'invisible',
192 '#default_value' => $transition->weight(),
193 '#attributes' => ['class' => ['transition-weight']],
196 '#theme' => 'item_list',
197 '#items' => array_map([State::class, 'labelCallback'], $transition->from()),
198 '#context' => ['list_style' => 'comma-list'],
200 'to' => ['#markup' => $transition->to()->label()],
202 '#type' => 'operations',
207 $form['transitions_container']['transition_add'] = [
208 '#markup' => $workflow->toLink($this->t('Add a new transition'), 'add-transition-form')->toString(),
211 if ($workflow_type->hasFormClass(WorkflowTypeInterface::PLUGIN_FORM_KEY)) {
212 $form['type_settings'] = [
215 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
216 $form['type_settings'] += $this->pluginFormFactory
217 ->createInstance($workflow_type, WorkflowTypeInterface::PLUGIN_FORM_KEY)
218 ->buildConfigurationForm($form['type_settings'], $subform_state);
227 public function validateForm(array &$form, FormStateInterface $form_state) {
228 /* @var \Drupal\workflows\WorkflowInterface $workflow */
229 $workflow = $this->entity;
230 $workflow_type = $workflow->getTypePlugin();
232 if ($workflow_type->hasFormClass(WorkflowTypeInterface::PLUGIN_FORM_KEY)) {
233 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
234 $this->pluginFormFactory
235 ->createInstance($workflow_type, WorkflowTypeInterface::PLUGIN_FORM_KEY)
236 ->validateConfigurationForm($form['type_settings'], $subform_state);
243 public function save(array $form, FormStateInterface $form_state) {
244 /* @var \Drupal\workflows\WorkflowInterface $workflow */
245 $workflow = $this->entity;
246 $workflow_type = $workflow->getTypePlugin();
248 if ($workflow_type->hasFormClass(WorkflowTypeInterface::PLUGIN_FORM_KEY)) {
249 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
250 $this->pluginFormFactory
251 ->createInstance($workflow_type, WorkflowTypeInterface::PLUGIN_FORM_KEY)
252 ->submitConfigurationForm($form['type_settings'], $subform_state);
256 drupal_set_message($this->t('Saved the %label Workflow.', ['%label' => $workflow->label()]));
262 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
263 // This form can only set the workflow's ID, label and the weights for each
265 /** @var \Drupal\workflows\WorkflowInterface $entity */
266 $values = $form_state->getValues();
267 $entity->set('label', $values['label']);
268 $entity->set('id', $values['id']);
269 foreach ($values['states'] as $state_id => $state_values) {
270 $entity->getTypePlugin()->setStateWeight($state_id, $state_values['weight']);
272 foreach ($values['transitions'] as $transition_id => $transition_values) {
273 $entity->getTypePlugin()->setTransitionWeight($transition_id, $transition_values['weight']);