getElementsDecoded don't return correct values during script execution

Created on 18 December 2020, almost 4 years ago
Updated 1 July 2024, 5 months ago

Problem/Motivation

Hi,
I'm encountering an issue when I export data from a Webform using PHP workers and differed action.
I've created a small PHP script to reproduce easily.

Steps to reproduce

Open 2 pages and run those scripts :
Script 1 : this one will loop during 30sec, each 5 sec, it will store the number of elements in the loaded Webform. (I load the Webform everytime)

      $i = 0;
      $values = [];
      while ($i <= 6) {
        sleep(5);
        $wid = '1090_1608282899';
        $webform = Webform::load($wid);
        $count = count($webform->getElementsDecoded());
        $values[] = $count;
        $i++;
      }
      var_dump($values);die;

Script 2 : you need to call this one during the script 1 execution, to add elements programmaticaly.

    $wid = '1090_1608282899';
    $webform = Webform::load($wid);
    $new_field = [
      'new_field_' . time() => [
        '#type' => 'textfield',
        '#title' => 'Question ' . time(),
      ],
    ];
    $new_element_arrangment = array_merge($webform->getElementsDecoded(), $new_field);
    $webform->setElements($new_element_arrangment)->save();
    $count = count($webform->getElementsDecoded());
    var_dump($count);die;

At the end of the script 1, you can see that the number is not increasing, elements are the same, even if during this script run, elements has been added.

Proposed resolution

I've tried a lot of thing, invalidate key from cache on this webform... etc.
The only thing that make it's work is to call the clear cache function at each loop during the execution, so I've went deeper in the clear cache function and if you add in the script 1 this, it works, but I don't think it's a good option... :

\Drupal::service('kernel')->rebuildContainer();

My real life issue is that I'm using PHP workers (looping script to listen new messages from RabbitMQ) so those are long time running script, restarted every 6 hours.
In our app, sometimes, the user can make an extract of data from the webform, and it's sent in assynchronous way by email, with rabbitmq and PHP workers.
So if at 1pm, I'm making an extract, data will be fresh, if webform creator add a new question, and at 1pm30 I'm starting a new extract, data will be same as 1pm...
It works with any webform.

Thanks for you investigation.
Hope you will be able to reproduce with those piece of code :-)

See you

🐛 Bug report
Status

Closed: works as designed

Version

5.20

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024