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')]),
];
CommentFileSizeAuthor
#3 2978404_2.patch2.71 KBmile23
#2 2969741_7.patch2.93 KBmile23

Issue fork examples-2978404

Command icon 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

ostry.sn created an issue. See original summary.

mile23’s picture

Title: [Queue example] $form_state->get('XXX') is alway empty during submit process » 'string_to_add' form element never increments
Component: Other » Queue Example
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new2.93 KB

It 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.

mile23’s picture

StatusFileSize
new2.71 KB

Wow that was entirely the wrong patch.

The last submitted patch, 2: 2969741_7.patch, failed testing. View results

ostry.sn’s picture

In 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

mile23’s picture

There's no issue with $form_state->set($form_state->get('insert_counter') + 1). That part works.

The problem is that #default_value isn'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 #value to 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.

avpaderno’s picture

Version: 8.x-1.x-dev » 4.0.x-dev
Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs reroll

As 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.

$form['insert_fieldset']['string_to_add'] = [
  '#type' => 'textfield',
  '#required' => TRUE,
  '#size' => 10,
  '#default_value' => $this->t('Change me'),
];

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.

avpaderno’s picture

Status: Needs work » Needs review

  • apaderno committed 3ddcca23 on 4.0.x
    Issue #2978404: 'string_to_add' form element never increments
    
avpaderno’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.