3 namespace Drupal\workflows\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
11 * Class WorkflowStateEditForm.
13 class WorkflowStateEditForm extends EntityForm {
16 * The ID of the state that is being edited.
25 public function getFormId() {
26 return 'workflow_state_edit_form';
32 public function buildForm(array $form, FormStateInterface $form_state, $workflow_state = NULL) {
33 $this->stateId = $workflow_state;
34 return parent::buildForm($form, $form_state);
40 public function form(array $form, FormStateInterface $form_state) {
41 $form = parent::form($form, $form_state);
43 /* @var \Drupal\workflows\WorkflowInterface $workflow */
44 $workflow = $this->getEntity();
45 $state = $workflow->getState($this->stateId);
47 '#type' => 'textfield',
48 '#title' => $this->t('Label'),
50 '#default_value' => $state->label(),
51 '#description' => $this->t('Label for the state.'),
56 '#type' => 'machine_name',
57 '#default_value' => $this->stateId,
59 'exists' => [$this, 'exists'],
64 // Add additional form fields from the workflow type plugin.
65 $form['type_settings'] = [
66 $workflow->get('type') => $workflow->getTypePlugin()->buildStateConfigurationForm($form_state, $workflow, $state),
71 'label' => $this->t('Transition'),
72 'state' => $this->t('To'),
73 'operations' => $this->t('Operations'),
75 $form['transitions'] = [
78 '#empty' => $this->t('There are no transitions to or from this state yet.'),
80 foreach ($state->getTransitions() as $transition) {
82 'title' => $this->t('Edit'),
83 'url' => Url::fromRoute('entity.workflow.edit_transition_form', [
84 'workflow' => $workflow->id(),
85 'workflow_transition' => $transition->id()
89 'title' => t('Delete'),
90 'url' => Url::fromRoute('entity.workflow.delete_transition_form', [
91 'workflow' => $workflow->id(),
92 'workflow_transition' => $transition->id()
95 $form['transitions'][$transition->id()] = [
97 '#markup' => $transition->label(),
100 '#markup' => $transition->to()->label(),
103 '#type' => 'operations',
113 * Copies top-level form values to entity properties
115 * This form can only change values for a state, which is part of workflow.
117 * @param \Drupal\Core\Entity\EntityInterface $entity
118 * The entity the current form should operate upon.
120 * A nested array of form elements comprising the form.
121 * @param \Drupal\Core\Form\FormStateInterface $form_state
122 * The current state of the form.
124 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
125 /** @var \Drupal\workflows\WorkflowInterface $entity */
126 $values = $form_state->getValues();
127 $entity->setStateLabel($values['id'], $values['label']);
128 if (isset($values['type_settings'])) {
129 $configuration = $entity->getTypePlugin()->getConfiguration();
130 $configuration['states'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
131 $entity->set('type_settings', $configuration);
138 public function save(array $form, FormStateInterface $form_state) {
139 /** @var \Drupal\workflows\WorkflowInterface $workflow */
140 $workflow = $this->entity;
142 drupal_set_message($this->t('Saved %label state.', [
143 '%label' => $workflow->getState($this->stateId)->label(),
145 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
151 protected function actions(array $form, FormStateInterface $form_state) {
152 $actions['submit'] = [
154 '#value' => $this->t('Save'),
155 '#submit' => ['::submitForm', '::save'],
158 $actions['delete'] = [
160 '#title' => $this->t('Delete'),
161 '#access' => $this->entity->access('delete-state:' . $this->stateId),
163 'class' => ['button', 'button--danger'],
165 '#url' => Url::fromRoute('entity.workflow.delete_state_form', [
166 'workflow' => $this->entity->id(),
167 'workflow_state' => $this->stateId