56cbf699617659b53779af7a3038a6538633423a
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowAddForm.php
1 <?php
2
3 namespace Drupal\workflows\Form;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\workflows\Entity\Workflow;
7 use Drupal\Core\Entity\EntityForm;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Form for adding workflows.
14  */
15 class WorkflowAddForm extends EntityForm {
16
17   /**
18    * The workflow type plugin manager.
19    *
20    * @var \Drupal\Component\Plugin\PluginManagerInterface
21    */
22   protected $workflowTypePluginManager;
23
24   /**
25    * WorkflowAddForm constructor.
26    *
27    * @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_plugin_manager
28    *   The workflow type plugin manager.
29    */
30   public function __construct(PluginManagerInterface $workflow_type_plugin_manager) {
31     $this->workflowTypePluginManager = $workflow_type_plugin_manager;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static(
39       $container->get('plugin.manager.workflows.type')
40     );
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function form(array $form, FormStateInterface $form_state) {
47     $form = parent::form($form, $form_state);
48
49     /* @var \Drupal\workflows\WorkflowInterface $workflow */
50     $workflow = $this->entity;
51     $form['label'] = [
52       '#type' => 'textfield',
53       '#title' => $this->t('Label'),
54       '#maxlength' => 255,
55       '#default_value' => $workflow->label(),
56       '#description' => $this->t('Label for the Workflow.'),
57       '#required' => TRUE,
58     ];
59
60     $form['id'] = [
61       '#type' => 'machine_name',
62       '#default_value' => $workflow->id(),
63       '#machine_name' => [
64         'exists' => [Workflow::class, 'load'],
65       ],
66     ];
67
68     $workflow_types = array_map(function ($plugin_definition) {
69       return $plugin_definition['label'];
70     }, $this->workflowTypePluginManager->getDefinitions());
71     $form['workflow_type'] = [
72       '#type' => 'select',
73       '#title' => $this->t('Workflow type'),
74       '#required' => TRUE,
75       '#options' => $workflow_types,
76     ];
77
78     return $form;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function save(array $form, FormStateInterface $form_state) {
85     /* @var \Drupal\workflows\WorkflowInterface $workflow */
86     $workflow = $this->entity;
87     // Initialize the workflow using the selected type plugin.
88     $workflow = $workflow->getTypePlugin()->initializeWorkflow($workflow);
89     $return = $workflow->save();
90     if (empty($workflow->getStates())) {
91       drupal_set_message($this->t('Created the %label Workflow. In order for the workflow to be enabled there needs to be at least one state.', [
92         '%label' => $workflow->label(),
93       ]));
94       $form_state->setRedirectUrl($workflow->toUrl('add-state-form'));
95     }
96     else {
97       drupal_set_message($this->t('Created the %label Workflow.', [
98         '%label' => $workflow->label(),
99       ]));
100       $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
101     }
102     return $return;
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
109     // This form can only set the workflow's ID, label and the weights for each
110     // state.
111     /** @var \Drupal\workflows\WorkflowInterface $entity */
112     $values = $form_state->getValues();
113     $entity->set('label', $values['label']);
114     $entity->set('id', $values['id']);
115     $entity->set('type', $values['workflow_type']);
116   }
117
118 }