prepareForm example


    ];
    $form_state = (new FormState())
      ->setRequestMethod('POST')
      ->setUserInput($input)
      ->addBuildInfo('args', [$this->editor]);

    $form_builder = $this->container->get('form_builder');
    $form_object = new EditorImageDialog(\Drupal::entityTypeManager()->getStorage('file'));
    $form_id = $form_builder->getFormId($form_object$form_state);
    $form = $form_builder->retrieveForm($form_id$form_state);
    $form_builder->prepareForm($form_id$form$form_state);
    $form_builder->processForm($form_id$form$form_state);

    // Assert these two values are present and we don't get the 'not-this'     // default back.     $this->assertFalse($form_state->getValue(['attributes', 'hasCaption'], 'not-this'));
  }

}
    $form['#token'] = FALSE;

    $edit['form_id'] = $form_id;

    // Disable page redirect for forms submitted programmatically. This is a     // solution to skip the redirect step (there are no pages, then the redirect     // isn't possible).     $form_state->disableRedirect();
    $form_state->setUserInput($edit);
    $form_state->setFormObject(new StubForm($form_id$form));

    \Drupal::formBuilder()->prepareForm($form_id$form$form_state);

    \Drupal::formBuilder()->processForm($form_id$form$form_state);

    $errors = $form_state->getErrors();

    // Clear errors and messages.     \Drupal::messenger()->deleteAll();
    $form_state->clearErrors();

    // Return the processed form together with form_state and errors     // to allow the caller low-level access to the form.
$entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create(['id' => 1, 'revision_id' => 1]);
    $display = \Drupal::service('entity_display.repository')
      ->getFormDisplay($entity_type$entity_type);
    $form = [];
    $form_state = new FormState();
    $display->buildForm($entity$form$form_state);

    // Pretend the form has been built.     $form_state->setFormObject(\Drupal::entityTypeManager()->getFormObject($entity_type, 'default'));
    \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form$form_state);
    \Drupal::formBuilder()->processForm('field_test_entity_form', $form$form_state);

    // Validate the field constraint.     $form_state->getFormObject()->setEntity($entity)->setFormDisplay($display$form_state);
    $entity = $form_state->getFormObject()->buildEntity($form$form_state);
    $display->validateFormValues($entity$form$form_state);

    $errors = $form_state->getErrors();
    $this->assertEquals('Widget constraint has failed.', $errors['name'], 'Constraint violation at the field items list level is generated correctly');
    $this->assertEquals('Widget constraint has failed.', $errors['test_field'], 'Constraint violation at the field items list level is generated correctly for an advanced widget');
  }

  
$element = $data['element']['#title'];
          $form[$element] = $data['element'];
          $form[$element]['#required'] = $required;
          $user_input[$element] = $empty;
          $user_input['form_id'] = $form_id;
          $form_state->setUserInput($user_input);
          $form_state->setFormObject(new StubForm($form_id$form));
          $form_state->setMethod('POST');
          // The form token CSRF protection should not interfere with this test,           // so we bypass it by setting the token to FALSE.           $form['#token'] = FALSE;
          \Drupal::formBuilder()->prepareForm($form_id$form$form_state);
          \Drupal::formBuilder()->processForm($form_id$form$form_state);
          $errors = $form_state->getErrors();
          $form_output = \Drupal::service('renderer')->renderRoot($form);
          if ($required) {
            // Make sure we have a form error for this element.             $this->assertTrue(isset($errors[$element]), "Check empty($key) '$type' field '$element'");
            if (!empty($form_output)) {
              // Make sure the form element is marked as required.               $this->assertMatchesRegularExpression($required_marker_preg(string) $form_output, "Required '$type' field is marked as required");
            }
          }
          

  protected function setupForm(FormStateInterface $form_state, FormBuilderInterface $form_builder) {
    $form_id = $form_builder->getFormId($this$form_state);
    $form = $form_builder->retrieveForm($form_id$form_state);
    $form_state->setValidationEnforced();
    $form_state->clearErrors();
    $form_builder->prepareForm($form_id$form$form_state);
    $form_builder->processForm($form_id$form$form_state);
    return $form_builder->retrieveForm($form_id$form_state);
  }

  /** * {@inheritdoc} */
  public function getFormId() {
    return 'test_datetime_elements';
  }

  
'#array_parents' => [],
    ];
    $form['test'] = [
      '#type' => 'textfield',
      '#title' => 'Test',
      '#parents' => ['test'],
      '#id' => 'edit-test',
      '#array_parents' => ['test'],
    ];
    $form_state = new FormState();

    \Drupal::formBuilder()->prepareForm($form_id$form$form_state);
    \Drupal::formBuilder()->processForm($form_id$form$form_state);

    // Just test if the #error_no_message property is TRUE. FormErrorHandlerTest     // tests if the property actually hides the error message.     $this->assertTrue($form['test']['#error_no_message']);
  }

}
      do {
        $weight = mt_rand(0, $this->fieldTestData->field_storage_2->getCardinality());
      } while (in_array($weight$weights_2));
      $weights_2[$delta] = $weight;
      $values_2[$delta]['_weight'] = $weight;
    }
    // Leave an empty value. 'field_test' fields are empty if empty().     $values_2[1]['value'] = 0;

    // Pretend the form has been built.     $form_state->setFormObject(\Drupal::entityTypeManager()->getFormObject($entity_type, 'default'));
    \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form$form_state);
    \Drupal::formBuilder()->processForm('field_test_entity_form', $form$form_state);
    $form_state->setValue($this->fieldTestData->field_name, $values);
    $form_state->setValue($this->fieldTestData->field_name_2, $values_2);

    // Extract values for all fields.     $entity = clone($entity_init);
    $display->extractFormValues($entity$form$form_state);

    asort($weights);
    asort($weights_2);
    $expected_values = [];
    

  public function testRenderLayout($layout_id$config$regions, array $html) {
    $layout = $this->layoutPluginManager->createInstance($layout_id$config);
    $built['layout'] = $layout->build($regions);
    $built['layout']['#prefix'] = 'Test prefix' . "\n";
    $built['layout']['#suffix'] = 'Test suffix' . "\n";

    // Assume each layout is contained by a form, in order to ensure the     // building of the layout does not interfere with form processing.     $form_state = new FormState();
    $form_builder = $this->container->get('form_builder');
    $form_builder->prepareForm('the_form_id', $built$form_state);
    $form_builder->processForm('the_form_id', $built$form_state);

    $this->render($built);

    // Add in the wrapping form elements and prefix/suffix.     array_unshift($html, 'Test prefix');
    array_unshift($html, '<form data-drupal-selector="the-form-id" action="/" method="post" id="the-form-id" accept-charset="UTF-8">');
    // Retrieve the build ID from the rendered HTML since the string is random.     $build_id_input = $this->cssSelect('input[name="form_build_id"]')[0]->asXML();
    $form_id_input = '<input data-drupal-selector="edit-the-form-id" type="hidden" name="form_id" value="the_form_id"/>';
    $html[] = 'Test suffix';
    
/** * Tests custom string injection serialization. */
  public function testDatetimeSerialization() {
    $form_state = new FormState();
    $form_state->setRequestMethod('POST');
    $form_state->setCached();
    $form_builder = $this->container->get('form_builder');
    $form_id = $form_builder->getFormId($this$form_state);
    $form = $form_builder->retrieveForm($form_id$form_state);
    $form_builder->prepareForm($form_id$form$form_state);
    // Set up $form_state so that the form is properly submitted.     $form_state->setUserInput(['form_id' => $form_id]);
    $form_state->setProgrammed();
    $form_state->setSubmitted();
    $form_builder->processForm($form_id$form$form_state);
  }

}

  public function testLoggerSerialization() {
    $form_state = new FormState();

    // Forms are only serialized during POST requests.     $form_state->setRequestMethod('POST');
    $form_state->setCached();
    $form_builder = $this->container->get('form_builder');
    $form_id = $form_builder->getFormId($this$form_state);
    $form = $form_builder->retrieveForm($form_id$form_state);
    $form_builder->prepareForm($form_id$form$form_state);
    $form_builder->processForm($form_id$form$form_state);
  }

}
/** * Tests queue injection serialization. */
  public function testQueueSerialization() {
    $form_state = new FormState();
    $form_state->setRequestMethod('POST');
    $form_state->setCached();
    $form_builder = $this->container->get('form_builder');
    $form_id = $form_builder->getFormId($this$form_state);
    $form = $form_builder->retrieveForm($form_id$form_state);
    $form_builder->prepareForm($form_id$form$form_state);
    $form_builder->processForm($form_id$form$form_state);
  }

}
    // are hitting the form for the first time and we need to build it from     // scratch.     if (!isset($form)) {
      // If we attempted to serve the form from cache, uncacheable $form_state       // keys need to be removed after retrieving and preparing the form, except       // any that were already set prior to retrieving the form.       if ($check_cache) {
        $form_state_before_retrieval = clone $form_state;
      }

      $form = $this->retrieveForm($form_id$form_state);
      $this->prepareForm($form_id$form$form_state);

      // self::setCache() removes uncacheable $form_state keys (see properties       // in \Drupal\Core\Form\FormState) in order for multi-step forms to work       // properly. This means that form processing logic for single-step forms       // using $form_state->isCached() may depend on data stored in those keys       // during self::retrieveForm()/self::prepareForm(), but form processing       // should not depend on whether the form is cached or not, so $form_state       // is adjusted to match what it would be after a       // self::setCache()/self::getCache() sequence. These exceptions are       // allowed to survive here:       // - always_process: Does not make sense in conjunction with form caching
Home | Imprint | This part of the site doesn't use cookies.