When creating a new user in Drupal, an error occurs in the UserMailUniqueValidator class.

Created on 5 January 2024, 6 months ago
Updated 23 January 2024, 5 months ago

Problem/Motivation

The issue arises when attempting to create a new user in Drupal. The following error occurs:

Error: Typed property Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator::$entityFieldManager must not be accessed before initialization in Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldValueValidator->validate() (line 55 of core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UniqueFieldValueValidator.php).

Steps to reproduce

Create a new user

Proposed resolution

The proposed resolution is to ensure that the entityFieldManager and entityTypeManager is properly initialized within the constructor of UserMailUniqueValidator. This involves initializing the entityFieldManager and entityTypeManager property in the class constructor.

🐛 Bug report
Status

Fixed

Version

2.2

Component

Code

Created by

🇧🇪Belgium Jonasanne

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

Comments & Activities

  • Issue created by @Jonasanne
  • 🇧🇪Belgium Jonasanne

    In this code modification, I enhanced the UserMailUniqueValidator class in the UserMailUniqueValidator.php file. The changes involve refactoring the constructor to leverage property promotion, which is a feature introduced in PHP 8.0. This helps simplify the code by declaring and initializing class properties directly within the constructor parameters.

    Here's a breakdown of the changes:
    Property Promotion in Constructor:

    I replaced the individual property declarations and assignments in the constructor with property promotion.
    Introduced property promotion for the AuthmapInterface, OpenidConnectRealmManagerInterface, EntityFieldManagerInterface, and EntityTypeManagerInterface properties.

    Constructor Parameter Removal:
    Removed the unnecessary parameters from the constructor, as property promotion takes care of both declaration and initialization.

    Dependency Injection Enhancement:
    Updated the create method to include the new dependencies (EntityFieldManagerInterface and EntityTypeManagerInterface) when instantiating the class.

  • Status changed to Needs work 6 months ago
  • 🇧🇪Belgium Jelle_S Antwerp, Belgium
    1. +++ b/src/Plugin/Validation/Constraint/UserMailUniqueValidator.php
      @@ -36,9 +24,7 @@ class UserMailUniqueValidator extends UniqueFieldValueValidator implements Conta
      +  public function __construct(protected AuthmapInterface $authmap, protected OpenidConnectRealmManagerInterface $realm_manager,protected EntityFieldManagerInterface $entityFieldManager, protected EntityTypeManagerInterface $entityTypeManager) {
      

      Code style: space after $realm_manager,
      Code style: rename $realm_manager to $realmManager
      Drupal uses snake_case for method arguments, but camelCase for properties; I believe the fact you're defining a property here has precedence over the fact that it's a constructor argument.

    2. +++ b/src/Plugin/Validation/Constraint/UserMailUniqueValidator.php
      @@ -68,7 +57,7 @@ class UserMailUniqueValidator extends UniqueFieldValueValidator implements Conta
      +        if ($this->realm_manager->hasDefinition($plugin_id)) {
      

      Revert this change based on previous remark.

  • 🇧🇪Belgium Matthijs

    Thanks for your merge request!

    I'm not a big fan constructor property promotion since it causes mixed casing for class properties, so please change that as well.

  • First commit to issue fork.
  • Status changed to Needs review 6 months ago
  • 🇮🇳India viren18febS

    I have fixed the reported issues and added a patch file please review
    Thanks

    • Matthijs committed 63fc4bba on 2.x
      Issue #3412523 by Jonasanne, viren18febS, Jelle_S, Matthijs: Fix Drupal...
  • Status changed to Fixed 6 months ago
  • 🇧🇪Belgium Matthijs

    Patch committed, thanks for the effort!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024