setUserInput example


  public function getFormId() {
    return 'form_test_storage_form';
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    if ($form_state->isRebuilding()) {
      $form_state->setUserInput([]);
    }
    // Initialize     $storage = $form_state->getStorage();
    $session = $this->getRequest()->getSession();
    if (empty($storage)) {
      $user_input = $form_state->getUserInput();
      if (empty($user_input)) {
        $session->set('constructions', 0);
      }
      // Put the initial thing into the storage       $storage = [
        
if (!empty($this->options['expose']['placeholder'])) {
        $form['value']['value']['#attributes']['placeholder'] = $this->options['expose']['placeholder'];
      }
      // Setup #states for all operators with one value.       foreach ($this->operatorValues(1) as $operator) {
        $form['value']['value']['#states']['visible'][] = [
          $source => ['value' => $operator],
        ];
      }
      if ($exposed && !isset($user_input[$identifier]['value'])) {
        $user_input[$identifier]['value'] = $this->value['value'];
        $form_state->setUserInput($user_input);
      }
    }
    elseif ($which == 'value') {
      // When exposed we drop the value-value and just do value if       // the operator is locked.       $form['value'] = [
        '#type' => 'textfield',
        '#title' => !$exposed ? $this->t('Value') : '',
        '#size' => 30,
        '#default_value' => $this->value['value'],
      ];
      
// Try to restore from temp store, this must be done before calling     // parent::form().     $store = $this->tempStoreFactory->get('node_preview');

    // Attempt to load from preview when the uuid is present unless we are     // rebuilding the form.     $request_uuid = \Drupal::request()->query->get('uuid');
    if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) {
      /** @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;

      
if (empty($plugin_definition['forms']['media_library_add'])) {
      return [];
    }

    // After the form to add new media is submitted, we need to rebuild the     // media library with a new instance of the media add form. The form API     // allows us to do that by forcing empty user input.     // @see \Drupal\Core\Form\FormBuilder::doBuildForm()     $form_state = new FormState();
    if ($state->get('_media_library_form_rebuild')) {
      $form_state->setUserInput([]);
      $state->remove('_media_library_form_rebuild');
    }
    $form_state->set('media_library_state', $state);
    return $this->formBuilder->buildForm($plugin_definition['forms']['media_library_add']$form_state);
  }

  /** * Get the media library view. * * @param \Drupal\media_library\MediaLibraryState $state * The current state of the media library, derived from the current request. * * @return array * The render array for the media library view. */
// 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();
      $form_state->setUserInput($input);
    }

    if (isset($_SESSION['batch_form_state'])) {
      // We've been redirected here after a batch processing. The form has       // already been processed, but needs to be rebuilt. See _batch_finished().       $form_state = $_SESSION['batch_form_state'];
      unset($_SESSION['batch_form_state']);
      return $this->rebuildForm($form_id$form_state);
    }

    // If the incoming input contains a form_build_id, we'll check the cache for
'#type' => 'select',
        '#title' => $this->options['limit'] ? $this->t('Select terms from vocabulary @voc', ['@voc' => $vocabulary->label()]) : $this->t('Select terms'),
        '#multiple' => TRUE,
        '#options' => $options,
        '#size' => min(9, count($options)),
        '#default_value' => $default_value,
      ];

      $user_input = $form_state->getUserInput();
      if ($exposed && isset($identifier) && !isset($user_input[$identifier])) {
        $user_input[$identifier] = $default_value;
        $form_state->setUserInput($user_input);
      }
    }

    if (!$form_state->get('exposed')) {
      // Retain the helper option       $this->helper->buildOptionsForm($form$form_state);

      // Show help text if not exposed to end users.       $form['value']['#description'] = $this->t('Leave blank for all. Otherwise, the first selected term will be the default instead of "Any".');
    }
  }

  
$form['value'] = [
      '#type' => $filter_form_type,
      '#title' => $this->value_value,
      '#options' => $this->valueOptions,
      '#default_value' => $this->value,
    ];
    if (!empty($this->options['exposed'])) {
      $identifier = $this->options['expose']['identifier'];
      $user_input = $form_state->getUserInput();
      if ($exposed && !isset($user_input[$identifier])) {
        $user_input[$identifier] = $this->value;
        $form_state->setUserInput($user_input);
      }
      // If we're configuring an exposed filter, add an - Any - option.       if (!$exposed || empty($this->options['expose']['required'])) {
        $form['value']['#options'] = ['All' => $this->t('- Any -')] + $form['value']['#options'];
      }
    }
  }

  protected function valueValidate($form, FormStateInterface $form_state) {
    if ($form_state->getValue(['options', 'value']) == 'All' && !$form_state->isValueEmpty(['options', 'expose', 'required'])) {
      $form_state->setErrorByName('value', $this->t('You must select a value unless this is an non-required exposed filter.'));
    }

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

}
'#type' => 'textfield',
      '#title' => $this->t('Value'),
      '#size' => 30,
      '#default_value' => $this->value,
    ];

    if ($form_state->get('exposed')) {
      $identifier = $this->options['expose']['identifier'];
      $user_input = $form_state->getUserInput();
      if (!isset($user_input[$identifier])) {
        $user_input[$identifier] = $this->value;
        $form_state->setUserInput($user_input);
      }
    }
  }

}

class WizardPluginBaseTest extends UnitTestCase {

  /** * @covers ::getSelected * * @dataProvider providerTestGetSelected */
  public function testGetSelected($expected$element = []$parents = []$user_input = []$not_rebuilding_expected = NULL) {
    $not_rebuilding_expected = $not_rebuilding_expected ?: $expected;
    $form_state = new FormState();
    $form_state->setUserInput($user_input);

    $actual = WizardPluginBase::getSelected($form_state$parents, 'the_default_value', $element);
    $this->assertSame($not_rebuilding_expected$actual);
    $this->assertSame($user_input$form_state->getUserInput());

    $form_state->setRebuild();
    $actual = WizardPluginBase::getSelected($form_state$parents, 'the_default_value', $element);
    $this->assertSame($expected$actual);
    $this->assertSame($user_input$form_state->getUserInput());
  }

  
$form['op'] = ['#type' => 'submit', '#value' => 'Submit'];
    // The form token CSRF protection should not interfere with this test, so we     // bypass it by setting the token to FALSE.     $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();

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

  /** * {@inheritdoc} */
  public function setUserInput(array $user_input) {
    $this->decoratedFormState->setUserInput($user_input);

    return $this;
  }

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

  
$form[$value]['#type'] = 'checkboxes';
        }
        else {
          $form[$value]['#type'] = 'select';
          $form[$value]['#size'] = 5;
          $form[$value]['#multiple'] = TRUE;
        }
        unset($form[$value]['#default_value']);
        $user_input = $form_state->getUserInput();
        if (empty($user_input[$value])) {
          $user_input[$value] = $this->group_info;
          $form_state->setUserInput($user_input);
        }
      }

      $this->options['expose']['label'] = '';
    }
  }

  /** * Render our chunk of the exposed filter form when selecting. * * You can override this if it doesn't do what you expect. */
foreach ($data['empty_values'] as $key => $empty) {
        foreach ([TRUE, FALSE] as $required) {
          $form_id = $this->randomMachineName();
          $form = [];
          $form_state = new FormState();
          $form['op'] = ['#type' => 'submit', '#value' => 'Submit'];
          $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.
'#title' => $this->t('Usernames'),
      '#description' => $this->t('Enter a comma separated list of user names.'),
      '#target_type' => 'user',
      '#tags' => TRUE,
      '#default_value' => $default_value,
      '#process_default_value' => $this->isExposed(),
    ];

    $user_input = $form_state->getUserInput();
    if ($form_state->get('exposed') && !isset($user_input[$this->options['expose']['identifier']])) {
      $user_input[$this->options['expose']['identifier']] = $default_value;
      $form_state->setUserInput($user_input);
    }
  }

  protected function valueValidate($form, FormStateInterface $form_state) {
    $uids = [];
    if ($values = $form_state->getValue(['options', 'value'])) {
      foreach ($values as $value) {
        $uids[] = $value['target_id'];
      }
      sort($uids);
    }
    
Home | Imprint | This part of the site doesn't use cookies.