Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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  * @internal
16  */
17 class WorkflowAddForm extends EntityForm {
18
19   /**
20    * The workflow type plugin manager.
21    *
22    * @var \Drupal\Component\Plugin\PluginManagerInterface
23    */
24   protected $workflowTypePluginManager;
25
26   /**
27    * WorkflowAddForm constructor.
28    *
29    * @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_plugin_manager
30    *   The workflow type plugin manager.
31    */
32   public function __construct(PluginManagerInterface $workflow_type_plugin_manager) {
33     $this->workflowTypePluginManager = $workflow_type_plugin_manager;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public static function create(ContainerInterface $container) {
40     return new static(
41       $container->get('plugin.manager.workflows.type')
42     );
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function form(array $form, FormStateInterface $form_state) {
49     $form = parent::form($form, $form_state);
50
51     /* @var \Drupal\workflows\WorkflowInterface $workflow */
52     $workflow = $this->entity;
53     $form['label'] = [
54       '#type' => 'textfield',
55       '#title' => $this->t('Label'),
56       '#maxlength' => 255,
57       '#default_value' => $workflow->label(),
58       '#required' => TRUE,
59     ];
60
61     $form['id'] = [
62       '#type' => 'machine_name',
63       '#default_value' => $workflow->id(),
64       '#machine_name' => [
65         'exists' => [Workflow::class, 'load'],
66       ],
67     ];
68
69     $workflow_types = array_column($this->workflowTypePluginManager->getDefinitions(), 'label', 'id');
70
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     $return = $workflow->save();
88     if (empty($workflow->getTypePlugin()->getStates())) {
89       $this->messenger()->addStatus($this->t('Created the %label Workflow. In order for the workflow to be enabled there needs to be at least one state.', [
90         '%label' => $workflow->label(),
91       ]));
92       $form_state->setRedirectUrl($workflow->toUrl('add-state-form'));
93     }
94     else {
95       $this->messenger()->addStatus($this->t('Created the %label Workflow.', [
96         '%label' => $workflow->label(),
97       ]));
98       $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
99     }
100     return $return;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
107     // This form can only set the workflow's ID, label and the weights for each
108     // state.
109     /** @var \Drupal\workflows\WorkflowInterface $entity */
110     $values = $form_state->getValues();
111     $entity->set('label', $values['label']);
112     $entity->set('id', $values['id']);
113     $entity->set('type', $values['workflow_type']);
114   }
115
116 }