Motivation and problem summary

When logged in as a user with blank email address

  1. Page that has newsletter attached #2892047: User without email leads to Query condition 'simplenews_subscriber.mail IN ()' cannot be empty => exception as below.
  2. Newsletters page on user account => same exception as below.
  3. 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.
  4. "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.

  1. It solves the problem of losing hidden&forced subscriptions if the user is initially created without an email.
  2. 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

Issue fork simplenews-3031919

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

AdamPS created an issue. See original summary.

adamps’s picture

Issue tags: +Needs tests
adamps’s picture

Issue summary: View changes
StatusFileSize
new610 bytes
adamps’s picture

Issue summary: View changes
marcoliver’s picture

Hi,

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

adamps’s picture

Issue summary: View changes

Thanks @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.

  1. It might avoid a lot of code if ($account->getEmail()) - I have noticed many more places than in the initial IS
  2. It solves the problem of losing hidden&forced subscriptions if the user is initially created without an email.
dhirendra.mishra’s picture

Assigned: Unassigned » dhirendra.mishra
Status: Active » Needs work

I am working on this

dhirendra.mishra’s picture

Assigned: dhirendra.mishra » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.83 KB
new1.76 KB

Please find the uploaded interdiff and patch file.

Status: Needs review » Needs work

The last submitted patch, 8: simplenews-3028084-8-user-without-email.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

adamps’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
Issue summary: View changes

Thanks @dhirendra.mishra however please see comment #6. I have now updated the IS to be clearer - sorry for the confusion.

marcoliver’s picture

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

msti’s picture

Like above, but this patch applies for 8.x-1.0 beta2

adamps’s picture

Version: 8.x-2.x-dev » 3.x-dev
suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new1.18 KB

A re-rolled patch against the latest 3.x. Please review.

adamps’s picture

Status: Needs review » Needs work

Thanks however I think the comments from #6 still have not been answered so it still needs work please

AdamPS credited Berdir.

adamps’s picture

Version: 3.x-dev » 4.x-dev
StatusFileSize
new1.26 KB

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

andypost’s picture

StatusFileSize
new605 bytes
new1.29 KB

It needs one more check to pass tests

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new585 bytes
new1.27 KB

Proper fix

adamps’s picture

Status: Needs review » Needs work

Thanks. Back to needs work because several of the cases in the IS are still failing AFAICS. Also would be great to have tests.

aditiup’s picture

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

azizos’s picture

@Aditiup Could you please provide a patch file?

Here’s an example of how to create one: git diff > filename.patch

aditiup’s picture

StatusFileSize
new74.95 KB

azizos’s picture

@Aditiup Please do not mark your MR as a draft, as it will be automatically blocked.

azizos’s picture

Status: Needs work » Needs review

@Aditiup I will try to test it ASAP and set RTBC.

stefan.korn’s picture

Status: Needs review » Needs work

I 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"

stefan.korn’s picture

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

AdamPS changed the visibility of the branch 3031919-bugs-if-user to hidden.

adamps’s picture

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

nick.murza’s picture

StatusFileSize
new1.8 KB

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

nick.murza’s picture

StatusFileSize
new1.8 KB
nick.murza’s picture

StatusFileSize
new1.66 KB
nick.murza’s picture

StatusFileSize
new1.66 KB
ressa’s picture

Priority: Normal » Major

But on my end, I cannot create any user with blank email address in the first place, because this is already failing.

Yes, 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":

Trigger a PHP error through the user interface, but only under rare circumstances or affecting only a small percentage of all users, even if there is a workaround.

From https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-...

ressa’s picture

mohammed motar’s picture

StatusFileSize
new2 KB

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

ressa’s picture

Thanks @mohammed motar, perhaps you can include an interdiff between #19 patch and your patch?

mohammed motar’s picture

StatusFileSize
new1.87 KB
ressa’s picture

Perfect, thanks!

lmoeni made their first commit to this issue’s fork.

lmoeni’s picture

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

lmoeni’s picture

Status: Needs work » Needs review
adamps’s picture

Status: Needs review » Needs work

Thanks I made some comments in the MR. I agree, we also need some code in simplenews_user_insert().

lmoeni’s picture

Status: Needs work » Needs review

I implemented the changes. I hope this is better now. Can you take a look again?

adamps’s picture

Status: Needs review » Needs work

Thanks it's good. Setting back to "needs work" because still some cases aren't covered.

lmoeni’s picture

I 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"?

adamps’s picture

Great thanks.

How can I recreate "1. Page that has newsletter attached"?

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 to Subscriber::loadByUid().

loon’s picture

Created patch from MR for 4.x-dev

lmoeni’s picture

Status: Needs work » Needs review

I rebased everything to the latest 4.x commit. Can someone review it?

adamps’s picture

Status: Needs review » Needs work

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

alyaj2a’s picture

The comment #52 work it for me! Thanks!

lmoeni’s picture

Status: Needs work » Needs review

I added a new test function with some test cases:

  1. Create new user without email address.
  2. Edit user without email address.
  3. Access subscription form via account page as user without email.
  4. Access subscription form via account page as user with email.
  5. Create, edit and view newsletter issue.

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.