Drupal 10 error

Created on 18 July 2024, 5 months ago

Hello.
Please tell me why this error might occur.

TypeError: Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField::__construct(): Argument #1 ($entity_type_manager) must be of type Drupal\Core\Entity\EntityTypeManagerInterface, Drupal\Core\Session\AccountProxy given, called in */core/lib/Drupal/Component/DependencyInjection/Container.php on line 261 in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->__construct() (line 33 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

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

Merge Requests

Comments & Activities

  • Issue created by @stayr
  • Status changed to Needs review 5 months ago
  • Hi @stayr,

    You received this error due to a type hint mismatch. The error occurs when the type of the argument being passed to the constructor does not match the expected type. I reviewed your code and identified that you need to add some lines to the 'services.yml' file.

    Please refer to the attached patch for details.

    Thanks!

  • After patch error

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • After patch error

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • Status changed to Active 5 months ago
  • After patch error

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • Status changed to Needs review 5 months ago
  • After patch error

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • After patch error

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • Status changed to Active 5 months ago
  • After patch error

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • Assigned to stayr
  • Error after path

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • Error after path

    Error: Call to a member function getAccount() on null in Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField->getContext() (line 60 of modules/contrib/user_ref_field_cache_context/src/CacheContext/UserReferenceField.php).

  • Does anyone have any other ideas?

  • I'm not knowledgeable on patch creation. But here are the changes I made to fix this issue:

    In the patch above, I had to remove the lines:
    class: Drupal\user_ref_field_cache_context\CacheContext\UserReferenceField
    - arguments: ['@current_user']

    In the file /src/CacheContext/UserReferenceField.php:

    60c60
    < $current_user_id = \Drupal::currentUser()->id();
    ---
    > $current_user_id = $this->user->getAccount()->id();
    62c62
    < $user = $this->entityTypeManager->getStorage('user')->load($current_user_id);
    ---
    > $user = $this->entityTypeManager()->getStorage('user')->load($current_user_id);

  • 🇮🇹Italy p4trizio Como

    Patch attached

  • First commit to issue fork.
  • Merge request !3Drupal 10 error → (Merged) created by mandclu
  • 🇨🇦Canada mandclu

    We shouldn't move away from using dependency injection. Please review MR!3 and let me know if this resolves the original error.

  • 🇮🇹Italy p4trizio Como

    Thank you Martin
    MR3 works nicely

  • Pipeline finished with Skipped
    6 days ago
    #372354
    • mandclu committed ebac842e on 1.0.x
      Issue #3462348 by mandclu, p4trizio, ananya.k: Drupal 10 error
      
  • 🇨🇦Canada mandclu

    Great! Merged in, and I will roll a new release.

Production build 0.71.5 2024