Motivation and problem summary
When logged in as a user with blank email address
- Page that has newsletter attached #2892047: User without email leads to Query condition 'simplenews_subscriber.mail IN ()' cannot be empty => exception as below.
- Newsletters page on user account => same exception as below.
- Subscription forms - they are shown with the text box to enter an email address as if for an anonymous user. Any subscription created is not linked to the account.
- "Newsletters" extra field on user entity => exception as below.
[error] "PHP message: Uncaught PHP Exception Drupal\Core\Database\InvalidQueryException: "Query condition 'simplenews_subscriber.mail IN ()' cannot be empty." at /[path_to_project]/docroot/core/lib/Drupal/Core/Database/Query/Condition.php line 103"
Proposed resolution
The "obvious" solution that people started to work towards is to prevent users without email from subscribing. However a better approach is to allow subscribing but of course the subscription won't activate until an email address is set.
- It solves the problem of losing hidden&forced subscriptions if the user is initially created without an email.
- It avoids a lot of code
if ($account->getEmail())- there are places than in the initial IS
Proposed tests
- Each of the 4 subscriber form variants
- Subscriber without mail isn't included in subscriber counts (e.g. node tab form) and no warning/error when sending newsletter.
- View a newsletter page
- "Newsletters" extra field on user entity
| Comment | File | Size | Author |
|---|---|---|---|
| #52 | bugs-if-user-has-blank-email-address-3031919-52.patch | 3.91 KB | loon |
| #41 | interdiff_19-39.txt | 1.87 KB | mohammed motar |
| #39 | simplenews-3031919-39.patch | 2 KB | mohammed motar |
| #36 | simplenews-3031919-30-2.patch | 1.66 KB | nick.murza |
| #30 | bugs-if-user-has-blank-email-address-3031919-030.patch | 997 bytes | stefan.korn |
Issue fork simplenews-3031919
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
adamps commentedComment #3
adamps commentedComment #4
adamps commentedComment #5
marcoliverHi,
I've attached an updated version of the patch we originally provided for https://www.drupal.org/project/simplenews/issues/3028084, which was merged into this issue.
Maybe this will help you to get to a more comprehensive solution.
Regards
Marc-Oliver
Comment #6
adamps commentedThanks @marcoliver, that's a good start. It's probably better to alter the initial form build to disable submit rather than to wait until the user submits and then report an error.
The "obvious" solution that everyone has been working towards is to prevent users without email from subscribing. I wonder if a neat alternative is to allow subscribing (showing a warning) but of course the subscription won't activate until a user address is set.
Comment #7
dhirendra.mishra commentedI am working on this
Comment #8
dhirendra.mishra commentedPlease find the uploaded interdiff and patch file.
Comment #10
adamps commentedThanks @dhirendra.mishra however please see comment #6. I have now updated the IS to be clearer - sorry for the confusion.
Comment #11
marcoliverFor the meantime until we have a working more holistic approach to the issue, I've rerolled my patch against 8.x-2.x for those in need of a quick and dirty fix.
Comment #12
mstiLike above, but this patch applies for 8.x-1.0 beta2
Comment #13
adamps commentedComment #14
suresh prabhu parkala commentedA re-rolled patch against the latest 3.x. Please review.
Comment #15
adamps commentedThanks however I think the comments from #6 still have not been answered so it still needs work please
Comment #17
adamps commentedCopied from #3402475: Subscriber::loadByMail(): Argument #1 ($mail) must be of type string, null given by Berdir. Still needs tests, and probably doesn't cover all of the cases in the IS yet.
Comment #18
andypostIt needs one more check to pass tests
Comment #19
andypostProper fix
Comment #20
adamps commentedThanks. Back to needs work because several of the cases in the IS are still failing AFAICS. Also would be great to have tests.
Comment #21
aditiup commentedHello Sir,
For the issue #3031919 I propose the following solution, I guess this might solve the problem
Changes to be made in the initial code in SubscriberForm.php
1)Added Email Field for Anonymous Users:For anonymous users, an email field is added to the form to capture their email address.
2)Handled Users Without Email Addresses:For logged-in users without an email address, a message is displayed indicating that their subscription will remain inactive until an email address is set.
3)Adjusted Subscription Logic:In the submitForm method, the subscription logic now checks if the email is provided. If not, it creates the subscription with an inactive status.
Changes to be made in the initial code in entity Subscriber.php
1)Email Field Requirements and Unique Constraint:
-Changes: The email field is defined as required (setRequired(TRUE)) and includes a uniqueness constraint (addConstraint('UniqueField', [])).
-Solution: Ensures that every subscriber entity must have a valid and unique email address. This prevents the creation of entities with empty or null email values, which could cause query conditions to fail when querying by email.
2)User and Subscription Synchronization:
-Changes: Implemented fillFromAccount() and copyToAccount() methods to synchronize data between the subscriber entity and user accounts.
-Solution: When a user subscribes with a blank email, these methods ensure that relevant user information (such as email address, preferred language, and status) is correctly synchronized between the user account and the subscriber entity. This synchronization prevents discrepancies that could lead to errors in subscription management.
3)Entity Load and Creation:
-Changes: Added loadByMail() and loadByUid() methods to facilitate loading of subscriber entities based on email or user ID.
-Solution: These methods ensure proper entity creation and retrieval, even in cases where users have blank email addresses. They provide flexibility to create new entities if none exist, ensuring robust entity management regardless of initial user data.
Changes to be made in the initial code in entity ConfirmationController.php
1)Subscriber Email Check: Added checks using $subscriber->getEmail() to ensure the subscriber has a valid email address before performing subscription actions (subscribe or unsubscribe).
2)Immediate Action Handling: Adjusted logic within the else block to include checks before calling $this->subscriptionManager->subscribe() or $this->subscriptionManager->unsubscribe() to ensure operations are only performed if the subscriber's email address exists.
Changes to be made in query condition.php
condition() Method Adjustment:
1)Within the condition() method, added a check specific to simplenews_subscriber.mail field where if the value is empty (NULL or ''), the condition is skipped to prevent the `
2)The check ensures that conditions involving simplenews_subscriber.mail are handled gracefully when the value is empty, aligning with the proposed resolution to allow subscriptions without an email but defer activation until an email is set.
Comment #22
azizos commented@Aditiup Could you please provide a patch file?
Here’s an example of how to create one:
git diff > filename.patchComment #23
aditiup commentedComment #27
azizos commented@Aditiup Please do not mark your MR as a draft, as it will be automatically blocked.
Comment #28
azizos commented@Aditiup I will try to test it ASAP and set RTBC.
Comment #29
stefan.kornI suppose patch from #23 and MR60 are currently not really a working solution for 4.x (i don't know if they maybe worked for 3.x). But 4.x is badly broken with this patch, basically whole system is dead after applying this patch. It seems this patch attempts to do a lot of refactoring beside fixing the issues with user without mail and maybe this has not kept up with latest changes in Simple news.
It seems to me, starting over here would be better. And for the refactoring part, imho one should focus on fixing the issues with user with no mail address here and do the refactoring in separate issue.
When reading the issue description, I am also asking myself, whether there are not some basic elements missing. It's about "when logged in as a user with blank email address". But on my end, I cannot create any user with blank email address in the first place, because this is already failing. So this would be something to solve before going to problems logging in with such a user.
So this surely "Needs work"
Comment #30
stefan.kornProposing this patch as a starting point, allowing to add a user without mail address and fixing #2 from issue description (by not showing the newsletters page if user has no mail address).
My use case is to have users without mail address for other purpose than simple news, OPs case was to have users add the mail address later. So this patch is not enough for the latter case obviously.
Comment #32
adamps commentedThanks I agree and I hid the MR/patch with lots of refactoring that is not part of this issue.
I believe the patch from #19 is still good. It contains some extra changes to what you have in #30. It would be great to merge these together and convert to a MR (Drupal.org is stopping using patches now).
This IS describes 4 cases so it would be good to track which ones we have done.
Comment #33
nick.murza commentedthis is my solution for this issue, I use social auth and Facebook, in some cases the email is missing, and the site crashes at the user login.
Comment #34
nick.murza commentedComment #35
nick.murza commentedComment #36
nick.murza commentedComment #37
ressaYes, if you as an administrator create a new account, you get an error, and also if you save such a user. Drupal core does not require an email address for an account.
But not all users need to have a subscription. In my opinion the patch here should first and foremost focus on accepting new accounts without an email address.
This should have Priority "Major":
From https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-...
Comment #38
ressaAdding related discussions.
Comment #39
mohammed motar commentedWe experienced an issue when updating a user and received the following error:
TypeError: Drupal\simplenews\Entity\Subscriber::loadByMail():
Argument #1 ($mail) must be of type string, null given, called in /web/modules/contrib/simplenews/simplenews.module on line 507 i Drupal\simplenews\Entity\Subscriber::loadByMail()
(rad 460 av /web/modules/contrib/simplenews/src/Entity/Subscriber.php).
We updated the patch and added change in simplenews_user_presave from #19.
Comment #40
ressaThanks @mohammed motar, perhaps you can include an interdiff between #19 patch and your patch?
Comment #41
mohammed motar commentedComment #42
ressaPerfect, thanks!
Comment #45
lmoeniI combined patches #30 and #41 in a new MR.
I used patch #41 but still got the error while creating a user. The check for the email in simplenews_user_insert() was missing.
Comment #46
lmoeniComment #47
adamps commentedThanks I made some comments in the MR. I agree, we also need some code in simplenews_user_insert().
Comment #48
lmoeniI implemented the changes. I hope this is better now. Can you take a look again?
Comment #49
adamps commentedThanks it's good. Setting back to "needs work" because still some cases aren't covered.
Comment #50
lmoeniI just commited some changes. I think this will fix "4. "Newsletters" extra field on user entity => exception as below.". The error does not appear anymore if the user create form has the newsletter field.
How can I recreate "1. Page that has newsletter attached"?
Comment #51
adamps commentedGreat thanks.
Just view the node.
I believe you will not see any bug. Here is the patch posted on the other issue. https://www.drupal.org/files/issues/2892047-query-condition-8.patch. See there was a call to
simplenews_subscriber_load_by_mail(), which was causing the bug. That call no longer exists, the code now has a call toSubscriber::loadByUid().Comment #52
loon commentedCreated patch from MR for 4.x-dev
Comment #53
lmoeniI rebased everything to the latest 4.x commit. Can someone review it?
Comment #54
adamps commentedThanks it looks good. This is an important issue, so please let's have tests for some of the cases at least - doesn't have to be all of them.
We can create a new test case function which creates a user without an email address. Then visit a few of the important pages and check the result.
For sure please let's include a test for "Your account has no email address". Then at least one other case, which would likely be a check that a certain page has the normal text rather than a crash, which was previously the case before the patch.
Back to "needs work" for the tests.
Comment #55
alyaj2a commentedThe comment #52 work it for me! Thanks!
Comment #56
lmoeniI added a new test function with some test cases:
Do we need more cases?
I put it in the SimplenewsSubscribeTest class, I hope that's okay. We can move it if there is a better place for it.