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;
10 use Drupal\workflows\StateInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Class WorkflowStateAddForm.
16 class WorkflowStateAddForm extends EntityForm {
19 * The plugin form factory.
21 * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
23 protected $pluginFormFactory;
26 * Creates an instance of WorkflowStateEditForm.
28 * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $pluginFormFactory
29 * The plugin form factory.
31 public function __construct(PluginFormFactoryInterface $pluginFormFactory) {
32 $this->pluginFormFactory = $pluginFormFactory;
38 public static function create(ContainerInterface $container) {
40 $container->get('plugin_form.factory')
47 public function getFormId() {
48 return 'workflow_state_add_form';
54 public function form(array $form, FormStateInterface $form_state) {
55 $form = parent::form($form, $form_state);
57 /* @var \Drupal\workflows\WorkflowInterface $workflow */
58 $workflow = $this->getEntity();
59 $workflow_type = $workflow->getTypePlugin();
62 '#type' => 'textfield',
63 '#title' => $this->t('Label'),
65 '#default_value' => '',
66 '#description' => $this->t('Label for the state.'),
71 '#type' => 'machine_name',
73 'exists' => [$this, 'exists'],
77 if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
78 $form['type_settings'] = [
81 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
82 $form['type_settings'] += $this->pluginFormFactory
83 ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
84 ->buildConfigurationForm($form['type_settings'], $subform_state);
91 * Determines if the workflow state already exists.
93 * @param string $state_id
94 * The workflow state ID.
97 * TRUE if the workflow state exists, FALSE otherwise.
99 public function exists($state_id) {
100 /** @var \Drupal\workflows\WorkflowInterface $original_workflow */
101 $original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
102 return $original_workflow->getTypePlugin()->hasState($state_id);
106 * Copies top-level form values to entity properties
108 * This form can only change values for a state, which is part of workflow.
110 * @param \Drupal\Core\Entity\EntityInterface $entity
111 * The entity the current form should operate upon.
113 * A nested array of form elements comprising the form.
114 * @param \Drupal\Core\Form\FormStateInterface $form_state
115 * The current state of the form.
117 protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
118 /** @var \Drupal\workflows\WorkflowInterface $entity */
119 $values = $form_state->getValues();
120 $type_plugin = $entity->getTypePlugin();
122 // Replicate the validation that Workflow::addState() does internally as the
123 // form values have not been validated at this point.
124 if (!$type_plugin->hasState($values['id']) && !preg_match('/[^a-z0-9_]+/', $values['id'])) {
125 $type_plugin->addState($values['id'], $values['label']);
132 public function validateForm(array &$form, FormStateInterface $form_state) {
133 parent::validateForm($form, $form_state);
134 /** @var \Drupal\workflows\WorkflowTypeInterface $workflow_type */
135 $workflow = $this->entity;
136 $workflow_type = $workflow->getTypePlugin();
138 if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
139 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
140 $this->pluginFormFactory
141 ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
142 ->validateConfigurationForm($form['type_settings'], $subform_state);
149 public function save(array $form, FormStateInterface $form_state) {
150 /** @var \Drupal\workflows\WorkflowInterface $workflow */
151 $workflow = $this->entity;
152 $workflow_type = $workflow->getTypePlugin();
153 $state = $workflow_type->getState($form_state->getValue('id'));
155 if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
156 $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
157 $subform_state->set('state', $state);
158 $this->pluginFormFactory
159 ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
160 ->submitConfigurationForm($form['type_settings'], $subform_state);
164 drupal_set_message($this->t('Created %label state.', [
165 '%label' => $workflow->getTypePlugin()->getState($form_state->getValue('id'))->label(),
167 $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
173 protected function actions(array $form, FormStateInterface $form_state) {
174 $actions['submit'] = [
176 '#value' => $this->t('Save'),
177 '#submit' => ['::submitForm', '::save'],