You are here

FormStateValueResolver.php in MongoDB 8.2

File

modules/mongodb_watchdog/src/Controller/ArgumentResolver/FormStateValueResolver.php
View source
<?php

namespace Drupal\mongodb_watchdog\Controller\ArgumentResolver;

use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;

/**
 * Yields a form_state argument for FormStateInterface $formState arguments.
 *
 * This resolver supports form methods with a FormStateInterface argument
 * regardless of its name.
 */
class FormStateValueResolver implements ArgumentValueResolverInterface {
  const NAME_LEGACY = 'form_state';

  /**
   * {@inheritdoc}
   */
  public function supports(Request $request, ArgumentMetadata $argument) {
    $argumentInterfaceMatches = $argument
      ->getType() === FormStateInterface::class;
    $requestAttributeExists = $request->attributes
      ->has(static::NAME_LEGACY);
    return $argumentInterfaceMatches || $requestAttributeExists;
  }

  /**
   * {@inheritdoc}
   */
  public function resolve(Request $request, ArgumentMetadata $argument) {
    $formState = $request->attributes
      ->has(static::NAME_LEGACY) ? $request->attributes
      ->get(static::NAME_LEGACY) : NULL;
    (yield $formState);
  }

}

Classes

Namesort descending Description
FormStateValueResolver Yields a form_state argument for FormStateInterface $formState arguments.