Form objects cannot rely on object properties initialized via route parameters

Created on 15 February 2016, about 9 years ago
Updated 27 April 2024, 11 months ago

Problem

Let's consider the example we have when building the Rules UI. We have a form, where we make use of some route parameters. The route parameter is needed during the whole form workflow, so it is set to an object property - in our case $this->rulesUiHandler - in buildForm() as in the following example:

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, RulesUiHandlerInterface $rules_ui_handler = NULL) {
    $this->rulesUiHandler = $rules_ui_handler;
  }

Then, $this->rulesUiHandler is needed during validation etc. This actually works just fine as long as the form is not cached, so people we'll do it. However, as soon as the form is cached - e.g., because someone altered the form and added #ajax, the object is not initialized via buildForm() any more and thus $this->rulesUiHandler is unset. Consequently, code that relies on it fatals.

Once one uses storage in form state instead of object properties the problem is solved / worked around. However, as this is not obvious to break *later on* and the code is much simpler/nicer when using object properties, people will do it.

Fun fact: This is actually a similar issue as we had in d7 with initializing form state storage. At that time I've run into it while working on the Rules-7.x UI. :-)

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component
FormΒ  β†’

Last updated 21 minutes ago

Created by

πŸ‡¦πŸ‡ΉAustria fago Vienna

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.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    There has been no discussion here for 8 years and the last comment implies that this is not a problem?

    Is this still a problem with Drupal 10 or later?

    If you are no longer having this issue, please close this issue and if possible, leave a comment describing how you fixed it, as it may help others with the same issue.

    Thanks!

  • Status changed to Closed: outdated about 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Since there hasn't been a follow up in 6+ months going to close out. If still a bug please reopen.

Production build 0.71.5 2024