Safeguard against missing policy config entries

Created on 9 March 2023, over 1 year ago
Updated 21 October 2023, 8 months ago

Problem/Motivation

If a mailer policy entry is missing a setting, then warnings are generated. This can occur for any EmailAdjuster that accesses a key in $this->configuration without handling a missing value.

This cannot occur in normal situations:

  1. The module default config includes all the settings
  2. The policy form will always save all the settings
  3. If a new setting is introduced, there is always an update hook

However it can occur if you do something strange:

  1. Forget to run an update hook
  2. Run the update hook, then import older config that has not been updated
  3. Edit config using command line or custom code that doesn't respect the required settings

Steps to reproduce

Install webform 6.1.4
Install Symfony Mailer 1.2
Add email handler to the webform
Submit the form as an anonymous user
Get notices and 2 warnings:
```Notice: Undefined index: swiftmailer in Drupal\symfony_mailer\Plugin\EmailAdjuster\WrapAndConvertEmailAdjuster->build() (line 81 of modules/contrib/symfony_mailer/src/Plugin/EmailAdjuster/WrapAndConvertEmailAdjuster.php).```

Proposed resolution

  1. We could say the code is working as designed, and even remove any existing handling of missing values - this is what Drupal Core almost always does (based on search for '#default_value' => $this->configuration)
  2. We could add in handling of missing values everywhere - however it will be very easy to forget
  3. We could try and do something smart to handle/fix missing values globally without having to write code each time

Workaround

Save the related policy in the GUI (without changing anything), see #4.

πŸ“Œ Task
Status

Closed: cannot reproduce

Version

1.2

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada amykhailova

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

Comments & Activities

  • Issue created by @amykhailova
  • πŸ‡¨πŸ‡¦Canada amykhailova

    Here is the patch that worked for us.

  • πŸ‡¬πŸ‡§United Kingdom AdamPS

    I can't reproduce this. How do you get $this->configuration['swiftmailer'] or $this->configuration['plain'] to be missing?

  • Status changed to Active about 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom intrafusion Edinburgh, UK

    I too am getting this error a lot in the logs and the cause is related to upgrading from previous versions, etc.

    At some point in the past I upgraded from an early beta version of Drupal Symfony Mailer which didn't include the swiftmailer emulation. By simply re-saving the effected policy (*All*) in my case this has gone away

  • πŸ‡¬πŸ‡§United Kingdom AdamPS

    Thanks @intrafusion.

    When you upgraded, this missing values should have been fixed automatically by the database update, see symfony_mailer_update_10006(). Perhaps the update was missed, or configuration was imported from an "old" version into a "new" version.

    There have been various similar bugs to this one with other policy elements (email adjusters). In the past I said it wasn't a bug because it shouldn't happen in normal cases. However people keep hitting these problems, so maybe we should do something. If so, then let's fix it for all of them at once, and I would like to consider the best way to fix it.

  • πŸ‡¬πŸ‡§United Kingdom intrafusion Edinburgh, UK

    Thanks @AdamPS, I originally installed 1.0.0-alpha3 and updated several times along the way (but no real idea when and what would have been updated)

    I agree totally with what you're saying about symfony_mailer_update_10006() but could this have been missed when moving between 1.0/1.1 and 1.2 releases?

  • πŸ‡¬πŸ‡§United Kingdom AdamPS

    It should not be possible to miss an update, no matter what sequence of releases you take or skip. However it can go wrong if you run the update hook, then import older config that has not been updated (because the hook won't run again).

  • πŸ‡¬πŸ‡§United Kingdom AdamPS

    OK there is a bug - in settingsForm() the configuration values may be missing (when adding a policy element) so should be safeguarded. Mostly the error gets hidden somehow by AJAX, however some people are hitting it for the body field on πŸ› Error creating new policy body field Fixed . I'll fix this on the other issue.

  • Status changed to Closed: cannot reproduce 8 months ago
  • πŸ‡¬πŸ‡§United Kingdom AdamPS
Production build 0.69.0 2024