setRequestMethod example


  public function buildForm($form_arg, FormStateInterface &$form_state) {
    // Ensure the form ID is prepared.     $form_id = $this->getFormId($form_arg$form_state);

    $request = $this->requestStack->getCurrentRequest();

    // Inform $form_state about the request method that's building it, so that     // it can prevent persisting state changes during HTTP methods for which     // that is disallowed by HTTP: GET and HEAD.     $form_state->setRequestMethod($request->getMethod());

    // Initialize the form's user input. The user input should include only the     // input meant to be treated as part of what is submitted to the form, so     // we base it on the form's method rather than the request's method. For     // example, when someone does a GET request for     // /node/add/article?destination=foo, which is a form that expects its     // submission method to be POST, the user input during the GET request     // should be initialized to empty rather than to ['destination' => 'foo'].     $input = $form_state->getUserInput();
    if (!isset($input)) {
      $input = $form_state->isMethodType('get') ? $request->query->all() : $request->request->all();
      
'mail' => 'foobar@example.com',
    ]);
    $test_user->save();
    \Drupal::service('current_user')->setAccount($test_user);
  }

  /** * 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);
  }

}
/** * {@inheritdoc} */
  public function isMethodType($method_type) {
    return $this->decoratedFormState->isMethodType($method_type);
  }

  /** * {@inheritdoc} */
  public function setRequestMethod($method) {
    $this->decoratedFormState->setRequestMethod($method);

    return $this;
  }

  /** * {@inheritdoc} */
  public function setValidationEnforced($must_validate = TRUE) {
    $this->decoratedFormState->setValidationEnforced($must_validate);

    return $this;
  }
// @todo Remove this in https://www.drupal.org/node/2524408, because form     // cache immutability is no longer necessary, because we no longer cache     // forms during safe HTTP methods. In the meantime, because     // Drupal\system\Tests\Form still has test coverage for a poisoned form     // cache following a GET request, trick $form_state into caching the form     // to keep that test working until we either remove it or change it in     // that issue.     if ($this->getRequest()->get('immutable')) {
      $form_state->addBuildInfo('immutable', TRUE);
      if ($this->getRequest()->get('cache') && $this->getRequest()->isMethodCacheable()) {
        $form_state->setRequestMethod('FAKE');
        $form_state->setCached();
      }
    }

    return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    
->willReturn($form_id);
    $form_arg->expects($this->exactly(4))
      ->method('buildForm')
      ->willReturn($expected_form);

    // Do an initial build of the form and track the build ID.     $form_state = new FormState();
    $form = $this->formBuilder->buildForm($form_arg$form_state);
    $original_build_id = $form['#build_id'];

    $this->request->setMethod('POST');
    $form_state->setRequestMethod('POST');

    // Rebuild the form, and assert that the build ID has not changed.     $form_state->setRebuild();
    $input['form_id'] = $form_id;
    $form_state->setUserInput($input);
    $form_state->addRebuildInfo('copy', ['#build_id' => TRUE]);
    $this->formBuilder->processForm($form_id$form$form_state);
    $this->assertSame($original_build_id$form['#build_id']);
    $this->assertTrue($form_state->isCached());

    // Rebuild the form again, and assert that there is a new build ID.

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->assertTrue(TRUE);
    $form_state->setRebuild();
  }

  /** * 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);
  }

}
'data-entity-type' => 'media',
          'data-entity-uuid' => $media->uuid(),
          'data-align' => 'center',
        ],
        'hasCaption' => 'false',
        'label' => $media->label(),
        'link' => '',
        'hostEntityLangcode' => $media->language()->getId(),
        'classes' => '',
      ],
    ]);
    $form_state->setRequestMethod('POST');

    $form = EditorMediaDialog::create($this->container)
      ->buildForm([]$form_state$editor->reveal());
    $this->assertNotNull($form, 'Form should have been built without errors.');
  }

}
/** @var \Drupal\Core\Form\FormStateInterface $preview */

      $form_state->setStorage($preview->getStorage());
      $form_state->setUserInput($preview->getUserInput());

      // Rebuild the form.       $form_state->setRebuild();

      // The combination of having user input and rebuilding the form means       // that it will attempt to cache the form state which will fail if it is       // a GET request.       $form_state->setRequestMethod('POST');

      $this->entity = $preview->getFormObject()->getEntity();
      $this->entity->in_preview = NULL;

      $form_state->set('has_been_previewed', TRUE);
    }

    /** @var \Drupal\node\NodeInterface $node */
    $node = $this->entity;

    if ($this->operation == 'edit') {
      
'dialogClass' => 'editor-image-dialog',
        'autoResize' => 'true',
      ],
      '_drupal_ajax' => '1',
      'ajax_page_state' => [
        'theme' => 'olivero',
        'theme_token' => 'some-token',
        'libraries' => '',
      ],
    ];
    $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'
FALSE, 'GET'],
      [FALSE, 'POST'],
    ];
  }

  /** * @covers ::setRequestMethod * * @dataProvider providerSetRequestMethod */
  public function testSetRequestMethod($method) {
    $this->decoratedFormState->setRequestMethod($method)
      ->shouldBeCalled();

    $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setRequestMethod($method));
  }

  /** * Provides data to self::testSetMethod(). */
  public function providerSetRequestMethod() {
    return [
      ['GET'],
      [
NULL,
      FALSE,
    ];
    return $data;
  }

  /** * @covers ::setCached */
  public function testSetCachedPost() {
    $form_state = new FormState();
    $form_state->setRequestMethod('POST');
    $form_state->setCached();
    $this->assertTrue($form_state->isCached());
  }

  /** * @covers ::setCached */
  public function testSetCachedGet() {
    $form_state = new FormState();
    $form_state->setRequestMethod('GET');
    $this->expectException(\LogicException::class);
    
$test_user->save();
    \Drupal::service('current_user')->setAccount($test_user);
  }

  /** * Tests db log injection serialization. */
  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);
  }

}
Home | Imprint | This part of the site doesn't use cookies.