From e5038a59116b9548fd56b45e58b8ddb906b5291d Mon Sep 17 00:00:00 2001 From: wotnak Date: Tue, 20 Jun 2023 20:36:12 +0200 Subject: [PATCH] Allow setting default values in the quick form using url params --- src/Plugin/QuickForm/Eggs.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Plugin/QuickForm/Eggs.php b/src/Plugin/QuickForm/Eggs.php index 1160047..971ccf6 100644 --- a/src/Plugin/QuickForm/Eggs.php +++ b/src/Plugin/QuickForm/Eggs.php @@ -11,6 +11,7 @@ use Drupal\farm_quick\Plugin\QuickForm\QuickFormBase; use Drupal\farm_quick\Traits\QuickLogTrait; use Psr\Container\ContainerInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * Eggs harvest quick form. @@ -36,6 +37,13 @@ class Eggs extends QuickFormBase { */ protected $entityTypeManager; + /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + /** * Constructs a Eggs object. * @@ -51,6 +59,8 @@ class Eggs extends QuickFormBase { * The string translation service. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. */ public function __construct( array $configuration, @@ -59,10 +69,12 @@ public function __construct( MessengerInterface $messenger, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager, + RequestStack $request_stack, ) { parent::__construct($configuration, $plugin_id, $plugin_definition, $messenger); $this->stringTranslation = $string_translation; $this->entityTypeManager = $entity_type_manager; + $this->requestStack = $request_stack; } /** @@ -81,6 +93,7 @@ public static function create( $container->get('messenger'), $container->get('string_translation'), $container->get('entity_type.manager'), + $container->get('request_stack'), ); } @@ -96,6 +109,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array { '#required' => TRUE, '#min' => 0, '#step' => 1, + '#default_value' => $this->requestStack->getCurrentRequest()->query->get('quantity'), ]; // Load active assets with the "produces_eggs" field checked. @@ -118,6 +132,16 @@ public function buildForm(array $form, FormStateInterface $form_state): array { if (count($assetsOptions) === 1) { $form['assets']['#default_value'] = array_keys($assetsOptions); } + + // If assets are provided in the query params, select them by default. + $assetsParam = $this->requestStack->getCurrentRequest()->query->get('assets'); + if (!empty($assetsParam)) { + $assetsIds = explode(',', $assetsParam); + $selectedAssets = array_intersect($assetsIds, array_keys($assetsOptions)); + if (!empty($selectedAssets)) { + $form['assets']['#default_value'] = $selectedAssets; + } + } } // Otherwise, show some text about adding assets. else {