Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / workflows / src / Form / WorkflowStateAddForm.php
index 0b368585b737675d906bd497a91e83fbe1b9cdd8..ab4a4c1c6501f6e279b6676a985e4f12700fb9b5 100644 (file)
@@ -5,12 +5,42 @@ namespace Drupal\workflows\Form;
 use Drupal\Core\Entity\EntityForm;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Form\SubformState;
+use Drupal\Core\Plugin\PluginFormFactoryInterface;
+use Drupal\workflows\StateInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Class WorkflowStateAddForm.
  */
 class WorkflowStateAddForm extends EntityForm {
 
+  /**
+   * The plugin form factory.
+   *
+   * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
+   */
+  protected $pluginFormFactory;
+
+  /**
+   * Creates an instance of WorkflowStateEditForm.
+   *
+   * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $pluginFormFactory
+   *   The plugin form factory.
+   */
+  public function __construct(PluginFormFactoryInterface $pluginFormFactory) {
+    $this->pluginFormFactory = $pluginFormFactory;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('plugin_form.factory')
+    );
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -26,6 +56,8 @@ class WorkflowStateAddForm extends EntityForm {
 
     /* @var \Drupal\workflows\WorkflowInterface $workflow */
     $workflow = $this->getEntity();
+    $workflow_type = $workflow->getTypePlugin();
+
     $form['label'] = [
       '#type' => 'textfield',
       '#title' => $this->t('Label'),
@@ -42,11 +74,15 @@ class WorkflowStateAddForm extends EntityForm {
       ],
     ];
 
-    // Add additional form fields from the workflow type plugin.
-    $form['type_settings'] = [
-      $workflow->get('type') => $workflow->getTypePlugin()->buildStateConfigurationForm($form_state, $workflow),
-      '#tree' => TRUE,
-    ];
+    if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
+      $form['type_settings'] = [
+        '#tree' => TRUE,
+      ];
+      $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
+      $form['type_settings'] += $this->pluginFormFactory
+        ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
+        ->buildConfigurationForm($form['type_settings'], $subform_state);
+    }
 
     return $form;
   }
@@ -63,7 +99,7 @@ class WorkflowStateAddForm extends EntityForm {
   public function exists($state_id) {
     /** @var \Drupal\workflows\WorkflowInterface $original_workflow */
     $original_workflow = \Drupal::entityTypeManager()->getStorage('workflow')->loadUnchanged($this->getEntity()->id());
-    return $original_workflow->hasState($state_id);
+    return $original_workflow->getTypePlugin()->hasState($state_id);
   }
 
   /**
@@ -81,16 +117,29 @@ class WorkflowStateAddForm extends EntityForm {
   protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
     /** @var \Drupal\workflows\WorkflowInterface $entity */
     $values = $form_state->getValues();
+    $type_plugin = $entity->getTypePlugin();
+
+    // Replicate the validation that Workflow::addState() does internally as the
+    // form values have not been validated at this point.
+    if (!$type_plugin->hasState($values['id']) && !preg_match('/[^a-z0-9_]+/', $values['id'])) {
+      $type_plugin->addState($values['id'], $values['label']);
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateForm(array &$form, FormStateInterface $form_state) {
+    parent::validateForm($form, $form_state);
+    /** @var \Drupal\workflows\WorkflowTypeInterface $workflow_type */
+    $workflow = $this->entity;
+    $workflow_type = $workflow->getTypePlugin();
 
-    // This is fired twice so we have to check that the entity does not already
-    // have the state.
-    if (!$entity->hasState($values['id'])) {
-      $entity->addState($values['id'], $values['label']);
-      if (isset($values['type_settings'])) {
-        $configuration = $entity->getTypePlugin()->getConfiguration();
-        $configuration['states'][$values['id']] = $values['type_settings'][$entity->getTypePlugin()->getPluginId()];
-        $entity->set('type_settings', $configuration);
-      }
+    if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
+      $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
+      $this->pluginFormFactory
+        ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
+        ->validateConfigurationForm($form['type_settings'], $subform_state);
     }
   }
 
@@ -100,9 +149,20 @@ class WorkflowStateAddForm extends EntityForm {
   public function save(array $form, FormStateInterface $form_state) {
     /** @var \Drupal\workflows\WorkflowInterface $workflow */
     $workflow = $this->entity;
+    $workflow_type = $workflow->getTypePlugin();
+    $state = $workflow_type->getState($form_state->getValue('id'));
+
+    if ($workflow_type->hasFormClass(StateInterface::PLUGIN_FORM_KEY)) {
+      $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
+      $subform_state->set('state', $state);
+      $this->pluginFormFactory
+        ->createInstance($workflow_type, StateInterface::PLUGIN_FORM_KEY)
+        ->submitConfigurationForm($form['type_settings'], $subform_state);
+    }
+
     $workflow->save();
     drupal_set_message($this->t('Created %label state.', [
-      '%label' => $workflow->getState($form_state->getValue('id'))->label(),
+      '%label' => $workflow->getTypePlugin()->getState($form_state->getValue('id'))->label(),
     ]));
     $form_state->setRedirectUrl($workflow->toUrl('edit-form'));
   }