prepareCallback example

->shouldBeCalled();

    $this->assertSame($expected$this->formStateDecoratorBase->hasInvalidToken());
  }

  /** * @covers ::prepareCallback * * @dataProvider providerPrepareCallback */
  public function testPrepareCallback($unprepared_callback, callable $prepared_callback) {
    $this->decoratedFormState->prepareCallback(Argument::is($unprepared_callback))
      ->willReturn($prepared_callback)
      ->shouldBeCalled();

    $this->assertSame($prepared_callback$this->formStateDecoratorBase->prepareCallback($unprepared_callback));
  }

  /** * Provides data to self::testPrepareCallback(). */
  public function providerPrepareCallback() {
    $function = 'sleep';
    
/** * {@inheritdoc} */
  public function hasInvalidToken() {
    return $this->decoratedFormState->hasInvalidToken();
  }

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

  /** * {@inheritdoc} */
  public function setFormObject(FormInterface $form_object) {
    $this->decoratedFormState->setFormObject($form_object);

    return $this;
  }

  


    // We need to return the part of the form (or some other content) that needs     // to be re-rendered so the browser can update the page with changed     // content. It is up to the #ajax['callback'] function of the element (may     // or may not be a button) that triggered the Ajax request to determine what     // needs to be rendered.     $callback = NULL;
    if (($triggering_element = $form_state->getTriggeringElement()) && isset($triggering_element['#ajax']['callback'])) {
      $callback = $triggering_element['#ajax']['callback'];
    }
    $callback = $form_state->prepareCallback($callback);
    if (empty($callback) || !is_callable($callback)) {
      throw new HttpException(500, 'The specified #ajax callback is empty or not callable.');
    }
    $result = call_user_func_array($callback[&$form, &$form_state$request]);

    // If the callback is an #ajax callback, the result is a render array, and     // we need to turn it into an AJAX response, so that we can add any commands     // we got earlier; typically the UpdateBuildIdCommand when handling an AJAX     // submit from a cached page.     if ($result instanceof AjaxResponse) {
      $response = $result;
    }
/** * {@inheritdoc} */
  public function buildEntity(array $form, FormStateInterface $form_state) {
    $entity = clone $this->entity;
    $this->copyFormValuesToEntity($entity$form$form_state);

    // Invoke all specified builders for copying form values to entity     // properties.     if (isset($form['#entity_builders'])) {
      foreach ($form['#entity_builders'] as $function) {
        call_user_func_array($form_state->prepareCallback($function)[$entity->getEntityTypeId()$entity, &$form, &$form_state]);
      }
    }

    return $entity;
  }

  /** * Copies top-level form values to entity properties. * * This should not change existing entity properties that are not being edited * by this form. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity the current form should operate upon. * @param array $form * A nested array of form elements comprising the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */

  public function executeValidateHandlers(&$form, FormStateInterface &$form_state) {
    // If there was a button pressed, use its handlers.     $handlers = $form_state->getValidateHandlers();
    // Otherwise, check for a form-level handler.     if (!$handlers && isset($form['#validate'])) {
      $handlers = $form['#validate'];
    }

    foreach ($handlers as $callback) {
      call_user_func_array($form_state->prepareCallback($callback)[&$form, &$form_state]);
    }
  }

  /** * {@inheritdoc} */
  public function validateForm($form_id, &$form, FormStateInterface &$form_state) {
    // If this form is flagged to always validate, ensure that previous runs of     // validation are ignored.     if ($form_state->isValidationEnforced()) {
      $form_state->setValidationComplete(FALSE);
    }
$this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Form errors cannot be set after form validation has finished.');
    $form_state->setErrorByName('test', 'message');
  }

  /** * @covers ::prepareCallback */
  public function testPrepareCallbackValidMethod() {
    $form_state = new FormState();
    $form_state->setFormObject(new PrepareCallbackTestForm());
    $processed_callback = $form_state->prepareCallback('::buildForm');
    $this->assertEquals([$form_state->getFormObject(), 'buildForm']$processed_callback);
  }

  /** * @covers ::prepareCallback */
  public function testPrepareCallbackInValidMethod() {
    $form_state = new FormState();
    $form_state->setFormObject(new PrepareCallbackTestForm());
    $processed_callback = $form_state->prepareCallback('not_a_method');
    // The callback was not changed as no such method exists.
$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'])) {
      $access = $element['#access'];
      
      // do not react to a batch that is already being processed (for instance       // if a batch operation performs a       // \Drupal\Core\Form\FormBuilderInterface::submitForm()).       if (($batch = &$this->batchGet()) && !isset($batch['id'])) {
        // Some previous submit handler has set a batch. To ensure correct         // execution order, store the call in a special 'control' batch set.         // See _batch_next_set().         $batch['sets'][] = ['form_submit' => $callback];
        $batch['has_form_submits'] = TRUE;
      }
      else {
        call_user_func_array($form_state->prepareCallback($callback)[&$form, &$form_state]);
      }
    }
  }

  /** * {@inheritdoc} */
  public function redirectForm(FormStateInterface $form_state) {
    $redirect = $form_state->getRedirect();

    // Allow using redirect responses directly if needed.
Home | Imprint | This part of the site doesn't use cookies.