3 namespace Drupal\workflows\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormStateInterface;
10 * Class WorkflowStateAddForm.
12 class WorkflowStateAddForm extends EntityForm {
17 public function getFormId() {
18 return 'workflow_state_add_form';
24 public function form(array $form, FormStateInterface $form_state) {
25 $form = parent::form($form, $form_state);
27 /* @var \Drupal\workflows\WorkflowInterface $workflow */
28 $workflow = $this->getEntity();
30 '#type' => 'textfield',
31 '#title' => $this->t('Label'),
33 '#default_value' => '',
34 '#description' => $this->t('Label for the state.'),
39 '#type' => 'machine_name',
41 'exists' => [$this, 'exists'],
45 // Add additional form fields from the workflow type plugin.
46 $form['type_settings'] = [
47 $workflow->get('type') => $workflow->getTypePlugin()->buildStateConfigurationForm($form_state, $workflow),
55 * Determines if the workflow state already exists.
57 * @param string $state_id
58 * The workflow state ID.
61 * TRUE if the workflow state exists, FALSE otherwise.
63 public function exists($state_id) {
64 /** @var \Drupal\workflows\WorkflowInterface $original_workflow */
65 $original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
66 return $original_workflow->hasState($state_id);
70 * Copies top-level form values to entity properties
72 * This form can only change values for a state, which is part of workflow.
74 * @param \Drupal\Core\Entity\EntityInterface $entity
75 * The entity the current form should operate upon.
77 * A nested array of form elements comprising the form.
78 * @param \Drupal\Core\Form\FormStateInterface $form_state
79 * The current state of the form.
81 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
82 /** @var \Drupal\workflows\WorkflowInterface $entity */
83 $values = $form_state->getValues();
85 // This is fired twice so we have to check that the entity does not already
87 if (!$entity->hasState($values['id'])) {
88 $entity->addState($values['id'], $values['label']);
89 if (isset($values['type_settings'])) {
90 $configuration = $entity->getTypePlugin()->getConfiguration();
91 $configuration['states'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
92 $entity->set('type_settings', $configuration);
100 public function save(array $form, FormStateInterface $form_state) {
101 /** @var \Drupal\workflows\WorkflowInterface $workflow */
102 $workflow = $this->entity;
104 drupal_set_message($this->t('Created %label state.', [
105 '%label' => $workflow->getState($form_state->getValue('id'))->label(),
107 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
113 protected function actions(array $form, FormStateInterface $form_state) {
114 $actions['submit'] = [
116 '#value' => $this->t('Save'),
117 '#submit' => ['::submitForm', '::save'],