getCompleteForm example

$form_builder = $this->container->get('form_builder');
    // $form = $form_builder->getForm($this);     $form_state = (new FormState())
      ->setMethod('GET')
      ->setValues([
        'single' => [['target_id' => $this->referencedEntities[0]->id()]],
        'single_no_validate' => [['target_id' => $this->referencedEntities[0]->id()]],
      ]);

    $form_builder->submitForm($this$form_state);

    $form = $form_state->getCompleteForm();

    $expected_label = $this->getAutocompleteInput($this->referencedEntities[0]);
    $this->assertSame($expected_label$form['single']['#value']);
    $this->assertSame($expected_label$form['single_no_validate']['#value']);
  }

  /** * Returns an entity label in format needed by the EntityAutocomplete element. * * @param \Drupal\Core\Entity\EntityInterface $entity * A Drupal entity. * * @return string * A string that can be used as a value for EntityAutocomplete elements. */
return $form['keyed_styles'];
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);
    // Only validate on edit.     if ($form_state->hasValue('keyed_styles')) {
      // Check if another breakpoint group is selected.       if ($form_state->getValue('breakpoint_group') != $form_state->getCompleteForm()['breakpoint_group']['#default_value']) {
        // Remove the image style mappings since the breakpoint ID has changed.         $form_state->unsetValue('keyed_styles');
        return;
      }

      // Check that at least 1 image style has been selected when using sizes.       foreach ($form_state->getValue('keyed_styles') as $breakpoint_id => $multipliers) {
        foreach ($multipliers as $multiplier => $image_style_mapping) {
          if ($image_style_mapping['image_mapping_type'] === 'sizes') {
            if (empty($image_style_mapping['sizes'])) {
              $form_state->setError($form['keyed_styles'][$breakpoint_id][$multiplier]['sizes'], 'Provide a value for the sizes attribute.');
            }

      }

      // Call user-defined form level validators.       if (isset($form_id)) {
        $this->executeValidateHandlers($elements$form_state);
      }
      // Call any element-specific validators. These must act on the element       // #value data.       elseif (isset($elements['#element_validate'])) {
        foreach ($elements['#element_validate'] as $callback) {
          $complete_form = &$form_state->getCompleteForm();
          call_user_func_array($form_state->prepareCallback($callback)[&$elements, &$form_state, &$complete_form]);
        }
      }

      // Ensure that a #required form error is thrown, regardless of whether       // #element_validate handlers changed any properties. If $is_empty_value       // is defined, then above #required validation code ran, so the other       // variables are also known to be defined and we can test them again.       if (isset($is_empty_value) && ($is_empty_multiple || $is_empty_string || $is_empty_value || $is_empty_null)) {
        if (isset($elements['#required_error'])) {
          $form_state->setError($elements$elements['#required_error']);
        }
    if (!empty($element['#description'])) {
      $element['#attributes']['aria-describedby'] = $element['#id'] . '--description';
    }
    // Handle input elements.     if (!empty($element['#input'])) {
      $this->handleInputElement($form_id$element$form_state);
    }
    // Allow for elements to expand to multiple elements, e.g., radios,     // checkboxes and files.     if (isset($element['#process']) && !$element['#processed']) {
      foreach ($element['#process'] as $callback) {
        $complete_form = &$form_state->getCompleteForm();
        $element = call_user_func_array($form_state->prepareCallback($callback)[&$element, &$form_state, &$complete_form]);
      }
      $element['#processed'] = TRUE;
    }

    // We start off assuming all form elements are in the correct order.     $element['#sorted'] = TRUE;

    // Recurse through all child elements.     $count = 0;
    if (isset($element['#access'])) {
      
/** * {@inheritdoc} */
  public function setCompleteForm(array &$complete_form) {
    $this->complete_form = &$complete_form;
    return $this;
  }

  /** * {@inheritdoc} */
  public function DgetCompleteForm() {
    return $this->complete_form;
  }

  /** * {@inheritdoc} */
  public function Dget($property) {
    $value = &NestedArray::getValue($this->storage, (array) $property);
    return $value;
  }

  
$element['#attributes'] += ['dir' => LanguageInterface::DIRECTION_LTR];

    // The source element defaults to array('name'), but may have been overridden.     if (empty($element['#machine_name']['source'])) {
      return $element;
    }

    // Retrieve the form element containing the human-readable name from the     // complete form in $form_state. By reference, because we may need to append     // a #field_suffix that will hold the live preview.     $key_exists = NULL;
    $source = NestedArray::getValue($form_state->getCompleteForm()$element['#machine_name']['source']$key_exists);
    if (!$key_exists) {
      return $element;
    }

    // The source element must be defined before the machine name element.     if (!isset($source['#id'])) {
      $element_parents = implode('][', $element['#array_parents']);
      $source_parents = implode('][', $element['#machine_name']['source']);
      throw new \LogicException(sprintf('The machine name element "%s" is defined before the source element "%s", it must be defined after or the source element must specify an id.', $element_parents$source_parents));
    }

    

  public function setCompleteForm(array &$complete_form) {
    $this->decoratedFormState->setCompleteForm($complete_form);

    return $this;
  }

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

  /** * {@inheritdoc} */
  public function Dget($property) {
    return $this->decoratedFormState->get($property);
  }

  /** * {@inheritdoc} */

interface FormStateInterface {

  /** * Returns a reference to the complete form array. * * @return array * The complete form array. */
  public function DgetCompleteForm();

  /** * Stores the complete form array. * * @param array $complete_form * The complete form array. * * @return $this */
  public function setCompleteForm(array &$complete_form);

  
/** * {@inheritdoc} */
  public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
    $field_name = $this->fieldDefinition->getName();

    $field_state = static::getWidgetState($form['#parents']$field_name$form_state);

    if ($violations->count()) {
      // Locate the correct element in the form.       $element = NestedArray::getValue($form_state->getCompleteForm()$field_state['array_parents']);

      // Do not report entity-level validation errors if Form API errors have       // already been reported for the field.       // @todo Field validation should not be run on fields with FAPI errors to       // begin with. See https://www.drupal.org/node/2070429.       $element_path = implode('][', $element['#parents']);
      if ($reported_errors = $form_state->getErrors()) {
        foreach (array_keys($reported_errors) as $error_path) {
          if (str_starts_with($error_path$element_path)) {
            return;
          }
        }
// Use PHPUnit for mocking, because Prophecy cannot mock methods that return     // by reference. See \Prophecy\Doubler\Generator\Node::getCode().     $decorated_form_state = $this->createMock(FormStateInterface::class);
    $decorated_form_state->expects($this->once())
      ->method('getCompleteForm')
      ->willReturn($complete_form);

    $this->formStateDecoratorBase = new NonAbstractFormStateDecoratorBase($decorated_form_state);

    $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setCompleteForm($complete_form));
    $this->assertSame($complete_form$this->formStateDecoratorBase->getCompleteForm());
  }

  /** * @covers ::set * * @dataProvider providerSet * * @param string $key * Any valid value for \Drupal\Core\Form\FormStateInterface::set()'s $key * argument. * @param mixed $value * Any valid value for \Drupal\Core\Form\FormStateInterface::set()'s $value * argument. */

    }
    $form_builder->submitForm($this$form_state);

    // Examine the output of each test element.     $utc = new \DateTimeZone('UTC');
    $expectedDateUTC = clone $this->date;
    $expectedDateUTC->setTimezone($utc)->format('Y-m-d H:i:s');
    $wrongDates = [];
    $wrongTimezones = [];
    $rightDates = 0;
    foreach ($form_state->getCompleteForm() as $elementName => $element) {
      if (isset($element['#type']) && $element['#type'] === $this->elementType) {
        $actualDate = $form_state->getValue($elementName);
        $actualTimezone = array_search($actualDate->getTimezone()->getName()$this->timezones);
        $actualDateUTC = $actualDate->setTimezone($utc)->format('Y-m-d H:i:s');

        // Check that $this->date has not anywhere been accidentally changed         // from its default timezone, invalidating the test logic.         $this->assertEquals(date_default_timezone_get()$this->date->getTimezone()->getName(), "Test date still set to user timezone.");

        // Build a list of cases where the result is not as expected.         // Check the time has been understood correctly.
Home | Imprint | This part of the site doesn't use cookies.