Level up your skills at DrupalCon Chicago 2026 with focused Summits and immersive, hands-on Trainings led by community experts.
In QueueExampleForm.php, line #92, $form_state->get('insert_counter'), always return an empty value.
The consequence of this is that the following code will always set the value to add to the queue to `'item #1'`.
$form['insert_fieldset']['string_to_add'] = [
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $this->t('item @counter', ['@counter' => $form_state->get('insert_counter')]),
];
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 2978404_2.patch | 2.71 KB | mile23 |
| #2 | 2969741_7.patch | 2.93 KB | mile23 |
Issue fork examples-2978404
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
mile23It turns out #default_value for a form element is cached, and only used if the #value is not present.
Maybe we shouldn't have this incrementing textfield.
Comment #3
mile23Wow that was entirely the wrong patch.
Comment #5
ostry.sn commentedIn my opinion, we can keep this incrementing textfield.
by removing the setting/getting of the useless arbitrary property "insert_counter" and exploit the call of existing instruction : $items = $this->retrieveQueue($queue_name);
In resume, we can just replace :
'#default_value' => $this->t('item @counter', ['@counter' => $form_state->get('insert_counter')]),
by
'#default_value' => $this->t('item @counter', ['@counter' => count($items) + 1]),
And remove the $form_state->setRebuild(); inside the addqueue submit handler
Comment #6
mile23There's no issue with
$form_state->set($form_state->get('insert_counter') + 1). That part works.The problem is that
#default_valueisn't changed on subsequent submits. You can try to set it to a new value but it will always say 'item 1'.So then you can change
#valueto increment properly, by setting it dynamically. However, when you do that, you end up overwriting whatever value the user puts into the textbox.If there's a better solution I'd love to see it, but we're not really demonstrating dynamic default values in this module. So the added complexity only confuses matters.
Comment #7
avpadernoAs a comment in the code says, the idea behind using a counter is to make entering a value for the string_to_add form element optional.
Given that
$queue->createItem()will always create a new item, even when it gets a value already passed in a previous call, I think there is no need to use a counter to build a new queue item. The string_to_add form element should require a value, and that value should always be used to create a new queue item.It is not a bug entering two or more times the same value in the queue, since the purpose of that example form is adding to the queue whichever value entered for the string_to_add form element. The bug is that the code tries to use a different value for the default value of that form element, but that code ends up with using the same value as default value, for the reason said by @Miles23 in comment #2.
I agree with what said in that comment: There is no need to have a text field with an auto-incrementing value.
Let's keep it simple: The string_to_add form element requires a value; whichever value is entered, that is added to the queue.
Eventually, I would not accept a value containing only spaces or other non-visible characters.
Comment #9
avpadernoComment #11
avpaderno