Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting default values in the quick form using url params #9

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Plugin/QuickForm/Eggs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,6 +37,13 @@ class Eggs extends QuickFormBase {
*/
protected $entityTypeManager;

/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* Constructs a Eggs object.
*
Expand All @@ -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,
Expand All @@ -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;
}

/**
Expand All @@ -81,6 +93,7 @@ public static function create(
$container->get('messenger'),
$container->get('string_translation'),
$container->get('entity_type.manager'),
$container->get('request_stack'),
);
}

Expand All @@ -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.
Expand All @@ -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 {
Expand Down