In USER_FIELDS event subscriber, receiving error when getting first name from social auth user

Created on 13 October 2022, about 2 years ago
Updated 14 April 2023, over 1 year ago

Problem/Motivation

In a custom SocialAuthEvents::USER_FIELDS event subscriber, I attempt to get the first name of the social auth user and store it in a separate field. But I receive this error:

Typed property Drupal\social_auth\User\SocialAuthUser::$firstName must not be accessed before initialization

Steps to reproduce

Create a custom SocialAuthEvents::USER_FIELDS event subscriber and attempt to access the social auth user first name. For example,

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[SocialAuthEvents::USER_FIELDS][] = ['onUserFields'];
    return $events;
  }

  /**
   * Populates name fields before user creation.
   *
   * @var \Drupal\social_auth\Event\UserFieldsEvent $event
   *   The event to respond to.
   */
  public function onUserFields(UserFieldsEvent $event) {
    $fields = $event->getUserFields();

    $user = $event->getSocialAuthUser();
    $first_name = $user->getFirstName();

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States pianomansam

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024