3 namespace Drupal\workflows\Form;
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;
13 * Form for adding workflows.
15 class WorkflowAddForm extends EntityForm {
18 * The workflow type plugin manager.
20 * @var \Drupal\Component\Plugin\PluginManagerInterface
22 protected $workflowTypePluginManager;
25 * WorkflowAddForm constructor.
27 * @param \Drupal\Component\Plugin\PluginManagerInterface $workflow_type_plugin_manager
28 * The workflow type plugin manager.
30 public function __construct(PluginManagerInterface $workflow_type_plugin_manager) {
31 $this->workflowTypePluginManager = $workflow_type_plugin_manager;
37 public static function create(ContainerInterface $container) {
39 $container->get('plugin.manager.workflows.type')
46 public function form(array $form, FormStateInterface $form_state) {
47 $form = parent::form($form, $form_state);
49 /* @var \Drupal\workflows\WorkflowInterface $workflow */
50 $workflow = $this->entity;
52 '#type' => 'textfield',
53 '#title' => $this->t('Label'),
55 '#default_value' => $workflow->label(),
60 '#type' => 'machine_name',
61 '#default_value' => $workflow->id(),
63 'exists' => [Workflow::class, 'load'],
67 $workflow_types = array_column($this->workflowTypePluginManager->getDefinitions(), 'label', 'id');
69 $form['workflow_type'] = [
71 '#title' => $this->t('Workflow type'),
73 '#options' => $workflow_types,
82 public function save(array $form, FormStateInterface $form_state) {
83 /* @var \Drupal\workflows\WorkflowInterface $workflow */
84 $workflow = $this->entity;
85 $return = $workflow->save();
86 if (empty($workflow->getTypePlugin()->getStates())) {
87 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.', [
88 '%label' => $workflow->label(),
90 $form_state->setRedirectUrl($workflow->toUrl('add-state-form'));
93 drupal_set_message($this->t('Created the %label Workflow.', [
94 '%label' => $workflow->label(),
96 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
104 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
105 // This form can only set the workflow's ID, label and the weights for each
107 /** @var \Drupal\workflows\WorkflowInterface $entity */
108 $values = $form_state->getValues();
109 $entity->set('label', $values['label']);
110 $entity->set('id', $values['id']);
111 $entity->set('type', $values['workflow_type']);