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.

Issue fork oidc-3412523

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

Jonasanne created an issue. See original summary.

jonasanne’s picture

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.

jelle_s’s picture

Status: Active » Needs work
  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.

matthijs’s picture

Thanks for your merge request!

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

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

viren18febs’s picture

Status: Needs work » Needs review
StatusFileSize
new2.63 KB

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...
matthijs’s picture

Status: Needs review » Fixed

Patch committed, thanks for the effort!

Status: Fixed » Closed (fixed)

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