3 namespace Drupal\workflows\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Form\SubformState;
9 use Drupal\Core\Plugin\PluginFormFactoryInterface;
11 use Drupal\workflows\StateInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Class WorkflowStateEditForm.
17 class WorkflowStateEditForm extends EntityForm {
20 * The ID of the state that is being edited.
27 * The plugin form factory.
29 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
31 protected $pluginFormFactory;
34 * Creates an instance of WorkflowStateEditForm.
36 * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $pluginFormFactory
37 * The plugin form factory.
39 public function __construct(PluginFormFactoryInterface $pluginFormFactory) {
40 $this->pluginFormFactory = $pluginFormFactory;
46 public static function create(ContainerInterface $container) {
48 $container->get('plugin_form.factory')
55 public function getFormId() {
56 return 'workflow_state_edit_form';
62 public function buildForm(array $form, FormStateInterface $form_state, $workflow_state = NULL) {
63 $this->stateId = $workflow_state;
64 return parent::buildForm($form, $form_state);
70 public function form(array $form, FormStateInterface $form_state) {
71 $form = parent::form($form, $form_state);
73 /* @var \Drupal\workflows\WorkflowInterface $workflow */
74 $workflow = $this->getEntity();
75 $workflow_type = $workflow->getTypePlugin();
76 $state = $workflow->getTypePlugin()->getState($this->stateId);
79 '#type' => 'textfield',
80 '#title' => $this->t('Label'),
82 '#default_value' => $state->label(),
83 '#description' => $this->t('Label for the state.'),
88 '#type' => 'machine_name',
89 '#default_value' => $this->stateId,
91 'exists' => [$this, 'exists'],
96 // Add additional form fields from the workflow type plugin.
97 if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
98 $form['type_settings'] = [
101 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
102 $subform_state->set('state', $state);
103 $form['type_settings'] += $this->pluginFormFactory
104 ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
105 ->buildConfigurationForm($form['type_settings'], $subform_state);
109 'label' => $this->t('Transition'),
110 'state' => $this->t('To'),
111 'operations' => $this->t('Operations'),
113 $form['transitions'] = [
115 '#header' => $header,
116 '#empty' => $this->t('There are no transitions to or from this state yet.'),
118 foreach ($state->getTransitions() as $transition) {
120 'title' => $this->t('Edit'),
121 'url' => Url::fromRoute('entity.workflow.edit_transition_form', [
122 'workflow' => $workflow->id(),
123 'workflow_transition' => $transition->id()
127 'title' => t('Delete'),
128 'url' => Url::fromRoute('entity.workflow.delete_transition_form', [
129 'workflow' => $workflow->id(),
130 'workflow_transition' => $transition->id()
133 $form['transitions'][$transition->id()] = [
135 '#markup' => $transition->label(),
138 '#markup' => $transition->to()->label(),
141 '#type' => 'operations',
151 * Copies top-level form values to entity properties
153 * This form can only change values for a state, which is part of workflow.
155 * @param \Drupal\Core\Entity\EntityInterface $entity
156 * The entity the current form should operate upon.
158 * A nested array of form elements comprising the form.
159 * @param \Drupal\Core\Form\FormStateInterface $form_state
160 * The current state of the form.
162 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
163 /** @var \Drupal\workflows\WorkflowInterface $entity */
164 $values = $form_state->getValues();
165 $entity->getTypePlugin()->setStateLabel($values['id'], $values['label']);
171 public function validateForm(array &$form, FormStateInterface $form_state) {
172 parent::validateForm($form, $form_state);
173 /** @var \Drupal\workflows\WorkflowTypeInterface $workflow_type */
174 $workflow = $this->entity;
175 $workflow_type = $workflow->getTypePlugin();
177 if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
178 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
179 $subform_state->set('state', $workflow_type->getState($this->stateId));
180 $this->pluginFormFactory
181 ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
182 ->validateConfigurationForm($form['type_settings'], $subform_state);
189 public function save(array $form, FormStateInterface $form_state) {
190 /** @var \Drupal\workflows\WorkflowInterface $workflow */
191 $workflow = $this->entity;
192 $workflow_type = $workflow->getTypePlugin();
194 if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
195 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
196 $subform_state->set('state', $workflow_type->getState($this->stateId));
197 $this->pluginFormFactory
198 ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
199 ->submitConfigurationForm($form['type_settings'], $subform_state);
203 drupal_set_message($this->t('Saved %label state.', [
204 '%label' => $workflow->getTypePlugin()->getState($this->stateId)->label(),
206 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
212 protected function actions(array $form, FormStateInterface $form_state) {
213 $actions['submit'] = [
215 '#value' => $this->t('Save'),
216 '#submit' => ['::submitForm', '::save'],
219 $actions['delete'] = [
221 '#title' => $this->t('Delete'),
222 '#access' => $this->entity->access('delete-state:' . $this->stateId),
224 'class' => ['button', 'button--danger'],
226 '#url' => Url::fromRoute('entity.workflow.delete_state_form', [
227 'workflow' => $this->entity->id(),
228 'workflow_state' => $this->stateId